<template>
  <!-- 弹出框表单 -->
  <el-drawer
    :title="title"
    :visible.sync="open"
    :direction="direction"
    size="50%"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" style="padding: 20px;">
      <el-row>
        <Field
          type="select"
          label="负责人姓名"
          prop="staffId"
          filterable
          :disabled="editId?true:false"
          v-model="form.staffId"
          :enumData="userList"
          placeholder="请输入负责人姓名"
        />
        <Field
          label="联系电话"
          prop="phone"
          v-model="form.phone"
          placeholder="请输入联系电话"
        />
        <Field
          label="大厅"
          prop="salaId"
          type="select"
          v-model="form.salaId"
          :enumData="dict.salaId"
          placeholder="请选择大厅"
        />
        <Field
          label="部门"
          prop="deptId"
          type="select"
          v-model="form.deptId"
          :enumData="dict.deptId"
          placeholder="请选择部门"
        />
        <!-- <Field
          label="工号"
          prop="number"
          v-model="form.number"
          placeholder="请输入工号"
        /> -->
        
        <!-- <Field
          label="负责窗口数量"
          prop="windowCount"
          v-model="form.windowCount"
          placeholder="请输入负责窗口数量"
        /> -->
        <!-- <Field
          label="备注"
          prop="remark"
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
        /> -->
      </el-row>
      <div style="background-color: rgba(242, 246, 252, 1);padding: 20px;">
        
        <wintable  :editId="editId" ref="wintable" :hallId="dict.hallId" class="wintabel" @selection-change="selectionChange"/>
        
      </div>
      <div style="padding: 20px;">
        <el-button type="primary" @click="submitForm" >提交</el-button>
      </div>
    </el-form>
  </el-drawer>
</template>

<script>
import form from "@/assets/mixins/formdialog";
import wintable from "./wintable.vue";
export default {
  name: "WindowOwnerDetail",
  mixins: [form],
  components: {
    wintable
  },
  created() {
    this.changePath("window/owner");
    this.getUser()
  },
  data() {
    return {
      // 大厅
      options:[],
      //窗口
      tableData:[],
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "窗口负责人",
      // 是否显示弹出层
      open: false,
      direction: "rtl",
      toString: [],
      toDate: [],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { max: 20, message: "最多只能录入20个字符", trigger: "blur" },
        ]
      },
      userList:{},
      userarr:[],//人员数组
      checkArr:[],//选中窗口
      editId:''//编辑id
    };
  },

  methods: {
    //重写表单
    beforeSubmit(data){
      data['deptName'] = this.dict.deptId[data.deptId]
      data['salaName'] = this.dict.salaId[data.salaId]
      data['staffName'] = this.userList[data.staffId]
      try {
        const userObj = this.userarr.find(v=>v.id == data.staffId) //工号
        data['number'] = userObj.workNum
        // 窗口列表
        const checkArr = []
        this.checkArr.forEach(v=>{
          checkArr.push({
            windowId:v.id,
            windowName:v.name,
            windowCode:v.fromnum,
            building:v.building,
            level:v.level
          })
        })
        data['windowOwnerDetailList'] = checkArr
      } catch (error) {}
      return data
    },
    // 选中窗口
    selectionChange(v){
      this.checkArr = v
    },
    cascaderChange(val) {
      if (this.deptList.length > 0) {
        this.form.deptId = this.deptList[this.deptList.length - 1];
      } else {
        this.form.deptId = "";
      }
      console.log(this.form.deptId, "当前选择");
    },
    // 查询人员
    getUser(){
      this.$post("/staff/list",{page:1,size:-1}).then(res=>{
        const {code,data} = res
        if(code == 1){
          this.userarr = data.data
          const arr = {}
          data.data.forEach(element => {
            arr[element.id] = element.name
          });
          this.userList = arr
        }
      })
    },
    onSubmitSearch(){

    },
    /** 编辑 */
    edit(row) {
      this.editId = row.staffId
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "window/owner/edit";
      this.getData();
      this.pageInfo.type = "edit";
      this.title = "修改窗口负责人";
    },
    /** 新增 */
    add(row) {
      this.editId = ''
      this.reset();
      this.urls.currUrl = "window/owner/add";
      this.getData();
      this.pageInfo.type = "add";
      this.title = "新增窗口负责人";
    },
    /** 查看*/
    view(row) {
      this.editId = row.staffId
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "window/owner/view";
      this.getData();
      this.pageInfo.type = "view";
      this.title = "窗口负责人详细";
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
    },
    /**获取数据后弹框 */
    afterRender(data) {
      this.open = true;
    },

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

    // 表单重置
    reset() {
      this.form = {
        deptId: 0,
        deptName: "",
        name: "",
        number: "",
        phone: "",
        windowCount: null,
        remark: "",
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
<style lang="less">
.wintabel .table-body{
  height: 500px;
  overflow-y: auto;
}
</style>
