<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            size="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <Field :span="20" label="房间名称" prop="roomName" v-model="form.roomName" placeholder="请输入房间名称"/>
                            <Field :span="20" label="房间编码" prop="roomCode" v-model="form.roomCode" placeholder="请输入房间编码"/>
                            <Field :span="20" label="房间类型" prop="roomType" v-model="form.roomType"  type="select" :enumData="dict.roomType" placeholder="请选择房间类型"/>
                            <Field :span="20" label="关联设备" prop="deviceId" v-model="deviceIds" type="select" :multiple="true" :enumData="dict.deviceIds" placeholder="请选择绑定的电子门牌的设备ID"/>
                            <Field :span="20" label="备注" prop="remark" v-model="form.remark" 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: "RoomDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("room")
        },
        data() {
            return {
              roomDeviceList: [],
              deviceIds:[],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "房间管理",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                    "deviceId",
                    "roomType",
                ],
                toDate:[
                ],
              toArrays:[],
                // 表单校验
                rules: {
                    roomName: [
                        {required: true,message: "请输入房间名称", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    roomType: [
                        {required: true,message: "请输入房间类型", trigger: "blur" },
                    ],
                  deviceId: [
                    {required: true,message: "请选择关联设备", trigger: "blur" },
                  ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="room/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改房间管理";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "room/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增房间管理";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="room/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "房间管理详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
          // 渲染前置处理
          beforeRender(data) {
              this.deviceIds=[]
          if(data.entity.roomDeviceList&&data.entity.roomDeviceList.length>0){
            this.deviceIds=data.entity.roomDeviceList.map(i=>i.deviceId+"")
          }

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

          beforeSubmit(data) {
           // data.deviceName=this.dict.deviceId[data.deviceId]
            console.log(this.deviceIds)
            let roomDeviceList=this.deviceIds.map(i=>{
              let obj={}
              obj.deviceId=i
              return obj
            })
            data.roomDeviceList=roomDeviceList;
            return data
          },

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

            // 表单重置
            reset() {
                this.form = {
                    roomName : "",
                    roomCode : "",
                    roomType : 0,
                    deviceId : null,
                    deviceName : "",
                    countPerson : null,
                    remark : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
