<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="roleType"
          type="select"
          v-model="form.roleType"
          :enumData="dict.roleType"
          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="inspect"
          type="checkbox"
          v-model="form.inspect"
          :enumData="inspectObj"
        />

        <!-- <el-form-item label="是否允许巡查" prop="inspect">
          <el-checkbox v-model="form.inspect">允许</el-checkbox>
        </el-form-item> -->
        <!-- <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"
          :checkPeopleArr="checkPeopleArr"
          @selection-change="selectionChange"
          :staffList="dict.staffList"
          v-if="refresh"
        />
      </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 {
      refresh: true,
      inspectObj: {
        1: "允许",
      },
      // 大厅
      options: [],
      //窗口
      tableData: [],
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "窗口负责人",
      // 是否显示弹出层
      open: false,
      direction: "rtl",
      toString: [],
      toDate: [],
      // 表单校验
      rules: {
        staffId: [{ required: true, message: "请选择负责人", trigger: "blur" }],
      },
      userList: {},
      userarr: [], //人员数组
      checkArr: [], //选中窗口
      checkPeopleArr: [], //选中窗口
      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) {}
      let arr = Array.from(new Set(this.$refs.wintable.checkAllList));
      console.log(this.checkAllList);
      data.staffIdList = arr ? arr : [];
      return data;
    },
    // 选中窗口
    selectionChange(v) {
      // this.checkArr = v;
      // this.checkPeopleArr = v;
      console.log(v, "vvvvvv");
    },
    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 + "-" + element.deptName;
          });
          this.userList = arr;
        }
      });
    },
    /** 编辑 */
    edit(row) {
      this.refresh = false;
      this.editId = row.staffId;
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "window/owner/edit";
      this.getData();
      this.checkPeopleArr = [];
      this.checkPeopleArr = row.staffIdList;
      this.pageInfo.type = "edit";
      this.title = "修改窗口负责人";
    },
    /** 新增 */
    add(row) {
      this.refresh = false;
      this.editId = "";
      this.reset();
      this.urls.currUrl = "window/owner/add";
      this.getData();
      this.checkPeopleArr = [];
      this.checkPeopleArr = row.staffIdList;
      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.refresh = true;
      this.open = true;
    },

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

    // 表单重置
    reset() {
      this.form = {
        deptName: "",
        inspect: 0,
        number: "",
        phone: "",
        remark: "",
        roleType: 0,
        salaName: "",
        staffIds: "",
        staffName: "",
        windowCount: 0,
        staffIdList: [],
        windowOwnerDetailList: [],
      };
      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>
