<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            size="50%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <Field :span="20" label="设备名称" prop="deviceName" v-model="form.deviceName" placeholder="请输入设备名称"/>
            <Field :span="20" label="所属房间"  prop="roomId" v-model="form.roomId" :enumData="dict.roomId" type="select" placeholder="请输入所属房间id"/>
            <Field :span="20" label="设备的MAC地址" prop="deviceMac" v-model="form.deviceMac" placeholder="请输入设备的MAC地址"/>
            <Field :span="20" label="分辨率" prop="resolution" v-model="form.resolution" type="select" :enumData="dict.resolution" placeholder="请选择分辨率"/>
            <Field :span="20" label="负责人" prop="leadingOfficial" v-model="form.leadingOfficial" placeholder="请输入负责人"/>
            <Field :span="20" label="设备位置" prop="deviceAddr" v-model="form.deviceAddr" placeholder="请输入设备位置"/>
            <Field :span="20" label="联系电话" prop="leadingOfficialTelephone" v-model="form.leadingOfficialTelephone" placeholder="请输入联系电话"/>
            <Field :span="20" label="设备访问ip" prop="ip" v-model="form.ip" placeholder="请输入设备访问ip"/>
            <Field :span="20" label="端口" prop="port" v-model="form.port" placeholder="请输入端口"/>
            <Field :span="20" label="备注" prop="deviceRemark" v-model="form.deviceRemark" type="textarea" placeholder="请输入备注"/>

          </el-row>

            <form-buttons @submit='submitForm' v-if="pageInfo.type!='view'" noCancelBtn />
        </el-form>

    </el-drawer>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    export default {
        name: "DeviceDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("device")
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "设备管理",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                    "resolution",
                    "deviceInBuilding",
                    "deviceInFloor",
                    "deviceStatus",
                    "enabled",
                    "roomId"
                ],
                toDate:[
                    "defectsLiabilityPeriod",
                    "onlineTime",
                    "offlineTime",
                ],
                // 表单校验
                rules: {
                    deviceName: [
                        {required: true,message: "请输入设备名称", trigger: "blur" },
                        {max: 20,message: "最多只能录入20个字符",trigger: "blur",},
                    ],
                  deviceMac: [
                    {required: true,message: "请输入设备Mac地址", trigger: "blur" },
                    {max: 20,message: "最多只能录入20个字符",trigger: "blur",},
                  ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                },urls: {
                currUrl: "/device/list",
                editUrl: "/device/edit",
                saveUrl: "/device/save",
              },
            };
        },

        methods: {
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="device/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改设备管理";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "device/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增设备管理";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="device/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "设备管理详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

          beforeSubmit(data) {
            data.roomName=this.dict.roomId[data.roomId]
            return data
          },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    deviceName : "",
                    roomId : null,
                    roomName : "",
                    deviceCode : "",
                    deviceMac : "",
                    siteId : 1,
                    siteCode : "",
                    siteName : "",
                    productCode : "hysdzmp",
                    productName : "会议室电子门牌",
                    deviceFirmname : "",
                    resolution : 0,
                    deviceInBuilding : null,
                    deviceInFloor : null,
                    defectsLiabilityPeriod : null,
                    leadingOfficial : "",
                    deviceAddr : "",
                    leadingOfficialTelephone : "",
                    devicePhotoPath : "",
                    ip : "",
                    centernum : "",
                    port : "",
                    deviceStatus : 0,
                    enabled : 1,
                    deviceRemark : "",
                    onlineTime : null,
                    offlineTime : null,
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
