<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="人员姓名" prop="empName">
        <el-input
          v-model="queryParams.empName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="empGender">
        <el-select
          v-model="queryParams.empGender"
          placeholder="请选择性别"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardno">
        <el-input
          v-model="queryParams.idcardno"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员出生日期" prop="empBirthday">
        <el-date-picker
          clearable
          v-model="queryParams.empBirthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择人员出生日期"
        />
      </el-form-item>
      <el-form-item label="特种证书编号" prop="numberCode">
        <el-input
          v-model="queryParams.numberCode"
          placeholder="请输入特种证书编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颁证时间" prop="issueDate">
        <el-date-picker
          clearable
          v-model="queryParams.issueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择颁证时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['backend:fire-special:create']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['backend:fire-special:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属单位" align="center" prop="orgName" />
      <el-table-column label="人员姓名" align="center" prop="empName" />
      <el-table-column label="性别" align="center" prop="empGender" />
      <el-table-column label="身份证号" align="center" prop="idcardno" />
      <el-table-column label="人员出生日期" align="center" prop="empBirthday">
        <template v-slot="scope">
          {{ scope.row.empBirthday.split(" ")[0] }}
        </template>
      </el-table-column>
      <el-table-column label="特种证书编号" align="center" prop="numberCode" />
      <el-table-column label="颁证时间" align="center" prop="issueDate" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['backend:fire-special:update']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['backend:fire-special:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      v-dialogDrag
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="所属单位" prop="orgIds">
          <el-input
            v-model="form.orgIds"
            @focus="handleChoice"
            placeholder="请选择所属单位"
            clearable
          />
          <!-- <el-select v-model="form.orgId" placeholder="请选择所属单位">
            <el-option label="请选择字典生成" value="" />
          </el-select> -->
        </el-form-item>
        <el-form-item label="人员姓名" prop="empName">
          <el-input v-model="form.empName" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="特种证书编号" prop="numberCode">
          <el-input
            v-model="form.numberCode"
            placeholder="请输入特种证书编号"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcardno">
          <el-input v-model="form.idcardno" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="性别" prop="empGender">
          <el-select v-model="form.empGender" placeholder="请选择性别">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)" :key="parseInt(dict.value)"
                  :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="人员出生日期" prop="empBirthday">
          <el-date-picker
            clearable
            v-model="form.empBirthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择人员出生日期"
          />
        </el-form-item>
        <el-form-item label="颁证时间" prop="issueDate">
          <el-date-picker
            clearable
            v-model="form.issueDate"
            type="date"
            value-format="timestamp"
            placeholder="选择颁证时间"
          />
        </el-form-item>
        <el-form-item label="附件">
          <fileUpload v-model="form.att" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分配分管单位 -->
    <el-dialog
      title="分配分管单位"
      :visible.sync="openOrg"
      width="85vw"
      append-to-body
      v-if="openOrg"
    >
      <choiceSelectCopy
        ref="choiceSelect"
        :userId="form.id"
        :type="'tackinfo'"
      ></choiceSelectCopy>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrg">确 定</el-button>
        <el-button @click="cancelOrg">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createFireSpecial,
  updateFireSpecial,
  deleteFireSpecial,
  getFireSpecial,
  getFireSpecialPage,
  exportFireSpecialExcel,
} from "@/api/backend/fireSpecial";
import FileUpload from "@/components/FileUpload";
import choiceSelectCopy from "@/components/choiceSelectCopy";
export default {
  name: "FireSpecial",
  components: {
    FileUpload,
    choiceSelectCopy,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特种作业人员列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        empName: null,
        empGender: null,
        idcardno: null,
        empBirthday: null,
        empBirthday: [],
        numberCode: null,
        issueDate: null,
        issueDate: [],
      },
      // 表单参数
      form: { orgId: "", orgIds: "" },
      // 表单校验
      rules: {
        empName: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        idcardno: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        numberCode: [
          { required: true, message: "特种证书编号不能为空", trigger: "blur" }
        ],
      },
      openOrg: false,
      data:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleChoice() {
      // 打开弹窗==========
      this.openOrg = true;
    },
    /** 查询列表 */
    getList() {
      // this.loading = true;
      // 执行查询
      getFireSpecialPage(this.queryParams).then((response) => {
        this.list = response.data?response.data.list:[];
        this.total = response.data?response.data.total:0;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 单位信息确认按钮 */
    submitOrg() {
      console.log("this.orgType :>> ", this.$refs["choiceSelect"].confirmMSg());
      this.data = this.$refs["choiceSelect"].confirmMSg();
      this.form.orgIds = this.data.orgName;
      // 关闭弹窗
      this.openOrg = false;
      // this.orgshow = true
    },
    // 取消按钮（分管单位）
    cancelOrg() {
      this.openOrg = false;
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        orgId: undefined,
        empName: undefined,
        empGender: undefined,
        idcardno: undefined,
        empBirthday: undefined,
        numberCode: undefined,
        issueDate: undefined,
        att: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加特种作业人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getFireSpecial(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.form.orgIds = response.data.orgName
        this.data.dwid=row.orgId
        this.title = "修改特种作业人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.form.orgIds) {
        return this.$modal.msgError("请选择所属单位")
      }
      this.form.orgId = this.data.dwid
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateFireSpecial(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createFireSpecial(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除特种作业人员编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteFireSpecial(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有特种作业人员数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportFireSpecialExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "特种作业人员.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
