<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
              <el-form-item label="员工姓名" prop="dealReason">
                <el-input
                  v-model="queryParams.employeeName"
                  placeholder="请输入员工姓名"
                  clearable
                />
              </el-form-item>
<!--        <el-form-item label="排查日期" prop="timeRange">-->
<!--          <el-date-picker-->
<!--            v-model="queryParams.timeRange"-->
<!--            type="daterange"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            range-separator="至"-->
<!--            start-placeholder="开始日期"-->
<!--            end-placeholder="结束日期"-->
<!--            :picker-options="pickerOptions"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @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="['baseInfo:staff:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDeleteAll"
            v-hasPermi="['baseInfo:staff:remove']"
          >批量删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <br />
    <el-card>
      <el-table border stripe  v-loading="loading" ref="multipleTable" :data="disposalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed/>
        <el-table-column label="员工编号" width="100" align="center" fixed prop="employeeNum" />
        <el-table-column label="员工姓名" width="120" align="center" fixed prop="employeeName" />
        <el-table-column label="员工类型" width="100" align="center" prop="employeeType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.base_staff_type" :value="scope.row.employeeType"/>
          </template>
        </el-table-column>
        <el-table-column label="工作部门" width="120" align="center" prop="departmentName" />
        <el-table-column label="性别" width="100" align="center" prop="gender" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.base_sex" :value="scope.row.gender"/>
          </template>
        </el-table-column>
        <el-table-column label="血型" width="100" align="center" prop="bloodType" />
        <el-table-column label="身份证号" width="200" align="center" prop="idCard" />
        <el-table-column label="转帐科目" width="200" align="center" prop="transferAccount" />
        <el-table-column label="婚姻" width="100" align="center" prop="marriage" />
        <el-table-column label="职务" width="200" align="center" prop="position" />
        <el-table-column label="银行账号" width="200" align="center" prop="bankNum" />
        <el-table-column label="出生日期" width="200" align="center" prop="birthday" />
        <el-table-column label="到职日期" width="200" align="center" prop="employmentDate" />
        <el-table-column label="离职日期" width="200" align="center" prop="departureDate" />
        <el-table-column label="籍贯" width="120" align="center" prop="placeOrigin" />
        <el-table-column label="联络人" width="120" align="center" prop="contactPerson" />
        <el-table-column label="联络电话" width="150" align="center" prop="contactPhone" />
        <el-table-column label="联络地址" width="200" align="center" prop="contactAddr" />
        <el-table-column label="户籍地址" width="200" align="center" prop="registerAddress" />
        <el-table-column label="手机" width="150" align="center" prop="phone" />
        <el-table-column label="邮箱" width="150" align="center" prop="mail" />
        <el-table-column label="经历" width="200" align="center" prop="experience" />
        <el-table-column label="学历" width="200" align="center" prop="qualification" />
        <el-table-column label="备注一" width="200" align="center" prop="remark1" />
        <el-table-column label="备注二" width="200" align="center" prop="remark2" />
        <el-table-column label="操作" width="180" align="center" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['baseInfo:staff:edit']"
              style="margin-right: 8px"
            >修改</el-button>
            <el-popconfirm
              v-hasPermi="['baseInfo:staff:remove']"
              :title="'确定要删除这条数据吗？'"
              @confirm="handleDelete(scope.row)"
            >
              <el-button
                size="mini"
                type="text"
                slot="reference"
                icon="el-icon-delete"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改死淘处理方式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="isView" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工编号" prop="employeeNum">
              <el-input
                ref="employeeNum"
                v-model="form.employeeNum"
                @keydown.enter.native="goNext($event, 'employeeName')"
                placeholder="请输入员工编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="employeeName">
              <el-input
                ref="employeeName"
                v-model="form.employeeName"
                @keydown.enter.native="goNext($event, 'employeeType')"
                placeholder="请输入员工姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工类型" prop="employeeType">
              <el-select
                style="width: 100%"
                ref="employeeType"
                filterable
                v-model="form.employeeType"
                @keydown.enter.native="goNext($event, 'departmentNum')"
              >
                <el-option
                  v-for="item in dict.type.base_staff_type"
                  :value="item.value"
                  :key="item.value"
                  :label="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作部门" prop="departmentNum">
              <el-select
                style="width: 100%"
                ref="departmentNum"
                filterable
                @change="changeDepartment"
                v-model="form.departmentNum"
                @keydown.enter.native="goNext($event, 'gender')"
              >
                <el-option
                  v-for="item in departmentList"
                  :key="item.departmentNum"
                  :value="item.departmentNum"
                  :label="item.departmentName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-row>
              <el-col :span="12">
                <el-form-item label="性别" prop="gender">
                  <el-select
                    ref="gender"
                    v-model="form.gender"
                    @keydown.enter.native="goNext($event, 'bloodType')"
                    placeholder="请选择性别"
                  >
                    <el-option
                      v-for="item in dict.type.base_sex"
                      :key="item.value"
                      :value="item.value"
                      :label="item.label"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="血型" prop="bloodType">
                  <el-input
                    ref="bloodType"
                    v-model="form.bloodType"
                    placeholder="请输入血型"
                    @keydown.enter.native="goNext($event, 'idCard')"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input
                ref="idCard"
                v-model="form.idCard"
                placeholder="请输入身份证号"
                @keydown.enter.native="goNext($event, 'transferAccount')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="转帐科目" prop="transferAccount">
              <el-input
                ref="transferAccount"
                v-model="form.transferAccount"
                placeholder="请输入转帐科目"
                @keydown.enter.native="goNext($event, 'marriage')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚姻" prop="marriage">
              <el-select
                style="width: 100%"
                ref="marriage"
                v-model="form.marriage"
                placeholder="请选择婚姻"
                @keydown.enter.native="goNext($event, 'position')"
              >
                <el-option
                  v-for="item in dict.type.base_marriage"
                  :label="item.label"
                  :value="item.value"
                  :key="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="职务" prop="position">
              <el-input
                ref="position"
                v-model="form.position"
                placeholder="请输入职务"
                @keydown.enter.native="goNext($event, 'bankNum')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankNum">
              <el-input
                ref="bankNum"
                v-model="form.bankNum"
                placeholder="请银行账号"
                @keydown.enter.native="goNext($event, 'birthday')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker
                ref="birthday"
                style="width: 100%"
                v-model="form.birthday"
                placeholder="请选择出生日期"
                @keydown.enter.native="goNext($event, 'employmentDate')"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到职日期" prop="employmentDate">
              <el-date-picker
                ref="employmentDate"
                style="width: 100%"
                v-model="form.employmentDate"
                placeholder="请选择到职日期"
                @keydown.enter.native="goNext($event, 'departureDate')"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="离职日期" prop="departureDate">
              <el-date-picker
                ref="departureDate"
                style="width: 100%"
                v-model="form.departureDate"
                placeholder="请选择离职日期"
                @keydown.enter.native="goNext($event, 'placeOrigin')"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="籍贯" prop="placeOrigin">
              <el-input
                ref="placeOrigin"
                v-model="form.placeOrigin"
                placeholder="请输入籍贯"
                @keydown.enter.native="goNext($event, 'contactPerson')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联络人" prop="contactPerson">
              <el-input
                ref="contactPerson"
                v-model="form.contactPerson"
                placeholder="请输入联络人"
                @keydown.enter.native="goNext($event, 'contactPhone')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联络电话" prop="contactPhone">
              <el-input
                ref="contactPhone"
                v-model="form.contactPhone"
                placeholder="请输入联络人"
                @keydown.enter.native="goNext($event, 'contactAddr')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="联络地址" prop="contactAddr">
              <el-input
                type="textarea"
                ref="contactAddr"
                v-model="form.contactAddr"
                placeholder="请输入联络地址"
                @keydown.enter.native="goNext($event, 'registerAddress')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="户籍地址" prop="registerAddress">
              <el-input
                type="textarea"
                ref="registerAddress"
                v-model="form.registerAddress"
                placeholder="请输入户籍地址"
                @keydown.enter.native="goNext($event, 'phone')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input
                ref="phone"
                v-model="form.phone"
                placeholder="请输入手机号"
                @keydown.enter.native="goNext($event, 'mail')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="mail">
              <el-input
                ref="mail"
                v-model="form.mail"
                placeholder="请输入邮箱"
                @keydown.enter.native="goNext($event, 'experience')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经历" prop="experience">
              <el-input
                ref="experience"
                v-model="form.experience"
                placeholder="请输入经历"
                @keydown.enter.native="goNext($event, 'qualification')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历" prop="qualification">
              <el-input
                ref="qualification"
                v-model="form.qualification"
                placeholder="请输入学历"
                @keydown.enter.native="goNext($event, 'remark1')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注一" prop="remark1">
              <el-input
                type="textarea"
                ref="remark1"
                v-model="form.remark1"
                placeholder="请输入学历"
                @keydown.enter.native="goNext($event, 'contactAddr')"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注二" prop="remark2">
              <el-input
                type="textarea"
                ref="remark2"
                v-model="form.remark2"
                placeholder="请输入备注二"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentList } from "@/api/baseInfo/department";
import { getStaffById, getStaffPage, addStaff, editStaff, delStaff } from "@/api/baseInfo/staff";
import { getFindByKey } from "@/utils";

export default {
  name: "Staff",
  dicts: ['base_staff_type', 'base_sex', 'base_marriage'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      disposalList: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      },
      pickerOptions: {
        disabledDate(time) {
          // 获取当前日期
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 将时间设置为当天的 00:00:00

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
      // 表单参数
      form: {},
      rules: {
        employeeNum: [{required: true, message: '请输入员工编号', trigger: 'blur'}],
        employeeName: [{required: true, message: '请输入员工姓名', trigger: 'blur'}],
        employeeType: [{required: true, message: '请选择员工类型', trigger: 'change'}]
      },
      departmentList: []
    }
  },
  watch: {
    "queryParams.timeRange": {
      handler(newVal) {
        if (newVal) {
          this.queryParams.startTime = newVal[0]
          this.queryParams.endTime = newVal[1]
        } else {
          this.queryParams.startTime = null
          this.queryParams.endTime = null
        }
      },
      deep: true
    },
  },
  created() {
    this.initDepartment()
    this.getList();
  },
  methods: {
    changeDepartment(val) {
      let item = getFindByKey(this.departmentList, val, 'departmentNum')
      if(item) {
        this.form.departmentName = item.departmentName
      }
    },
    goNext(event, nextRef) {
      event.preventDefault();
      this.$refs[nextRef].focus();
    },
    /**  工作部门 **/
    initDepartment() {
      getDepartmentList({}).then(res => {
        this.departmentList = res.data
      })
    },
    /** 查询检验项目列表 */
    getList() {
      getStaffPage(this.queryParams).then(res => {
        this.disposalList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        employeeName: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isView = false;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getStaffById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = false;
        this.title = "编辑"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getStaffById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = true;
        this.title = "查看"
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        employeeNum: null,
        employeeName: null,
        employeeType: null,
        departmentNum: null,
        departmentName: null,
        gender: null,
        bloodType: null,
        idCard: null,
        transferAccount: null,
        marriage: null,
        position: null,
        bankNum: null,
        birthday: null,
        employmentDate: null,
        departureDate: null,
        placeOrigin: null,
        contactPerson: null,
        contactPhone: null,
        contactAddr: null,
        registerAddress: null,
        phone: null,
        mail: null,
        experience: null,
        qualification: null,
        remark1: null,
        remark2: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 编辑
            editStaff(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            // 新增
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      let params = {
        ids: ids
      }
      delStaff(params).then(res => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 删除按钮操作 */
    handleDeleteAll() {
      const ids = this.ids;
      this.$modal.confirm('确定要删除这条数据吗？').then(() => {
        let params = {
          ids: ids.join(',')
        }
        delStaff(params).then(res => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          this.$refs.multipleTable.clearSelection();
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>

