<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="70%">
        <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="设备的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="deviceInBuilding" v-model="form.deviceInBuilding" type="select" :enumData="dict.deviceInBuilding" placeholder="请选择所属楼栋"/>
                            <Field :span="20" label="所属楼层" prop="deviceInFloor" v-model="form.deviceInFloor" type="select" :enumData="dict.deviceInFloor" 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="设备图片" prop="devicePhotoPath" v-model="form.devicePhotoPath" type="textarea" placeholder="请输入设备图片"/>
                             <Field :span="20" label="设备状态 " prop="deviceStatus" v-model="form.deviceStatus" type="select" :enumData="dict.deviceStatus" placeholder="请选择设备状态 "/>
                            <Field :span="20" label="启用状态 " prop="enabled" v-model="form.enabled" type="select" :enumData="dict.enabled" placeholder="请选择启用状态 "/>
                            <Field :span="20" label="备注" prop="deviceRemark" v-model="form.deviceRemark" type="textarea" placeholder="请输入备注"/>




              <Field :span="20" label="选择工作人员" prop="roomId" v-model="form.workmanId" :multiple="true" type="select" :enumData="dict.workmanId" placeholder="请选择人员"/>


              <Field :span="20" label="轮播图片"><fileUpload v-model="filePaths" :limit="limit" :fileType="fileType"  :fileName="form.fileName" @getFileName="getFileName"  prePath="/file/fileupload"/> <el-link :underline="false" type="warning">建议图片分辨率大小为1000*1200</el-link></Field>



            </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 {
              filePaths:"",
              fileNames:"",
              limit:10,
              deviceFileEntityList: [],
              deviceWorkmanEntityList:[],
              fileType:['png', 'jpg', 'jpeg'],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "设备管理",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                    "resolution",
                    "deviceInBuilding",
                    "deviceInFloor",
                    "deviceStatus",
                    "enabled",
                ],
              toArrays:[
                "workmanId"
                  ],
                toDate:[],
                // 表单校验
                rules: {
                    deviceName: [
                        {required: true,message: "请输入设备名称", trigger: "blur" },
                        {max: 20,message: "最多只能录入20个字符",trigger: "blur",},
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
          getFileName(fileName) {
            this.fileNames = fileName;
          },
            /** 编辑 */
            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;
            },

          // 渲染前置处理
            beforeRender(data) {
              console.log("data",data)
              this.fileNames = ""
              this.filePaths = ""
              if (data.entity.deviceFileEntityList && data.entity.deviceFileEntityList.length > 0) {
                this.fileNames=data.entity.deviceFileEntityList.map(i=>i.fileName+"").join(",")
                this.filePaths=data.entity.deviceFileEntityList.map(i=>i.filePath+"").join(",")
              }
              console.log("filePaths:"+this.filePaths)
              //处理选择的人员
              if (data.entity.deviceWorkmanEntityList && data.entity.deviceWorkmanEntityList.length > 0) {
                data.entity.workmanId=data.entity.deviceWorkmanEntityList.map(i=>i.workmanId+"").join(",")

              }
              console.log("afterdata",data)
              return data;
            },

            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },


          beforeSubmit(data) {
            // data.deviceName=this.dict.deviceId[data.deviceId]
            console.log(this.filePaths)
            console.log(this.fileNames)
            let deviceFileEntityList=[];
            var filePathList = this.filePaths.split(",");
            var fileNameList = this.fileNames.split(",");
            for (let i = 0; i < filePathList.length; i++) {
              let obj={}
              obj.filePath=filePathList[i]
              obj.fileName=fileNameList[i]
              deviceFileEntityList.push(obj)
            }
            data.deviceFileEntityList=deviceFileEntityList;

            let deviceWorkmanEntityList=[];
            if(data.workmanId){
              for (let i = 0; i < data.workmanId.length; i++) {
                let obj={}
                obj.workmanId=data.workmanId[i]
                deviceWorkmanEntityList.push(obj)
              }
            }
            data.deviceWorkmanEntityList=deviceWorkmanEntityList;
            return data
          },

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

            // 表单重置
            reset() {
                this.form = {
                    deviceName : "",
                    roomId : null,
                    roomName : "",
                    deviceCode : "",
                    deviceMac : "",
                    siteId : 1,
                    siteCode : "",
                    siteName : "",
                    productCode : "",
                    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>
