<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="离职时间">
        <el-date-picker
          v-model="queryParams.changeTimeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 240px"
        />
      </el-form-item>
      <!-- <el-form-item label="异动原因" prop="changeReason">
        <el-input
          v-model="queryParams.changeReason"
          placeholder="请输入异动原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="负责人" prop="changeHandler">
        <el-input v-model="queryParams.changeHandler" placeholder="请输入负责人" clearable
          @keyup.enter.native="handleQuery" />
      </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="warning" plain icon="el-icon-edit" size="mini" @click="handleSupplement">离职</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeEntryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="异动记录主键ID" align="center" prop="changeId" v-if="true"/> -->
      <el-table-column label="员工姓名" align="center" prop="wmsEmployeeInfo.name" />
      <el-table-column label="岗位工种" align="center" prop="wmsEmployeeInfo.jobType" />
      <el-table-column label="离职时间" align="center" prop="changeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职原因" align="center" prop="changeReason" />
      <el-table-column label="负责人" align="center" prop="changeHandler" />
      <!-- <el-table-column label="附件" align="center" prop="attachment" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" 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)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 员工离职记录对话框 -->
    <el-dialog :title="supplementTitle" :visible.sync="supplementOpen" width="500px" append-to-body>
      <el-form ref="supplementForm" :model="supplementForm" :rules="supplementRules" label-width="80px">
        <el-form-item label="离职员工" prop="infoId">
          <EmployeeSelector v-model="supplementForm.infoId" keyField="infoId" placeholder="请选择离职员工" />
        </el-form-item>
        <el-form-item label="离职时间" prop="changeTime">
          <el-date-picker clearable v-model="supplementForm.changeTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择离职时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职原因" prop="changeReason">
          <el-input v-model="supplementForm.changeReason" placeholder="请输入离职原因" />
        </el-form-item>
        <el-form-item label="负责人" prop="changeHandler">
          <el-input v-model="supplementForm.changeHandler" placeholder="请输入负责人" disabled />
        </el-form-item>
        <el-form-item label="附件" prop="attachment">
          <file-upload v-model="supplementAttachment" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="supplementForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitSupplementForm">确 定</el-button>
        <el-button @click="cancelSupplement">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看员工离职记录对话框 -->
    <el-dialog title="查看员工离职记录" :visible.sync="viewOpen" width="500px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="员工姓名">
          {{ viewRecord.wmsEmployeeInfo && viewRecord.wmsEmployeeInfo.name }}
        </el-descriptions-item>
        <el-descriptions-item label="离职时间">
          {{ parseTime(viewRecord.changeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </el-descriptions-item>
        <el-descriptions-item label="离职原因">
          {{ viewRecord.changeReason }}
        </el-descriptions-item>
        <el-descriptions-item label="负责人">
          {{ viewRecord.changeHandler }}
        </el-descriptions-item>
        <el-descriptions-item label="附件">
          <file-list :oss-ids="viewRecord.attachment"></file-list>
        </el-descriptions-item>
        <el-descriptions-item label="备注">
          {{ viewRecord.remark }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployeeChange, getEmployeeChange, delEmployeeChange, updateEmployeeChange, addEmployeeChange, employeeEntry, employeeLeave } from "@/api/wms/employeeChange";
import { listDept } from "@/api/wms/dept";
import EmployeeSelector from "@/components/EmployeeSelector";
import FileList from "@/components/FileList";

export default {
  name: "EmployeeLeave",
  components: {
    EmployeeSelector,
    FileList
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工离职表格数据
      employeeEntryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoId: undefined,
        changeType: 1,
        changeTime: undefined,
        changeStartTime: undefined,
        changeEndTime: undefined,
        changeTimeRange: [],
        changeReason: undefined,
        changeHandler: undefined,
        attachment: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 附件
      attachment: undefined,
      // 部门列表
      deptList: [],
      // 补录对话框相关
      supplementTitle: "补录员工离职记录",
      supplementOpen: false,
      supplementForm: {
        changeId: undefined,
        infoId: undefined,
        changeType: 1,
        changeTime: undefined,
        changeReason: undefined,
        changeHandler: undefined,
        attachment: undefined,
        remark: undefined
      },
      supplementRules: {
      },
      supplementAttachment: undefined,
      // 查看记录对话框相关
      viewOpen: false,
      viewRecord: {
        changeId: undefined,
        infoId: undefined,
        changeType: undefined,
        changeTime: undefined,
        changeReason: undefined,
        changeHandler: undefined,
        attachment: undefined,
        remark: undefined
      }
    };
  },
  dicts: ['hrm_employee_education'],
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 查询员工离职记录列表 */
    getList() {
      this.loading = true;
      listEmployeeChange(this.queryParams).then(response => {
        this.employeeEntryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDeptList() {
      listDept().then(response => {
        this.deptList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消补录按钮操作
    cancelSupplement() {
      this.supplementOpen = false;
      this.resetSupplementForm();
    },
    // 表单重置
    reset() {
      this.form = {
        infoId: undefined,
        serialNumber: undefined,
        dept: undefined,
        jobType: undefined,
        name: undefined,
        idCard: undefined,
        age: undefined,
        gender: undefined,
        education: undefined,
        homeAddress: undefined,
        phone: undefined,
        entryTime: undefined,
        emergencyContact: undefined,
        relationship: undefined,
        emergencyContactPhone: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined,
        infoRemark: undefined
      };
      this.attachment = undefined;
      this.resetForm("form");
    },
    // 补录表单重置
    resetSupplementForm() {
      this.supplementForm = {
        changeId: undefined,
        infoId: undefined,
        changeType: 1,
        changeTime: undefined,
        changeReason: undefined,
        changeHandler: undefined,
        attachment: undefined,
        remark: undefined
      };
      this.supplementAttachment = undefined;
      this.resetForm("supplementForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 处理时间范围
      if (this.queryParams.changeTimeRange && this.queryParams.changeTimeRange.length === 2) {
        this.queryParams.changeStartTime = this.queryParams.changeTimeRange[0];
        this.queryParams.changeEndTime = this.queryParams.changeTimeRange[1];
      } else {
        this.queryParams.changeStartTime = undefined;
        this.queryParams.changeEndTime = undefined;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.changeTimeRange = [];
      this.queryParams.changeStartTime = undefined;
      this.queryParams.changeEndTime = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.changeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.changeHandler = this.$store.getters.nickName;
      this.open = true;
      this.title = "员工离职";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.resetSupplementForm();
      const changeId = row.changeId || this.ids
      getEmployeeChange(changeId).then(response => {
        this.loading = false;
        this.supplementForm = response.data;
        this.supplementAttachment = response.data.attachment;
        this.supplementOpen = true;
        this.supplementTitle = "修改员工离职记录";
      });
    },
    /** 补录按钮操作 */
    handleSupplement() {
      this.resetSupplementForm();
      this.supplementForm.changeHandler = this.$store.getters.nickName;
      this.supplementOpen = true;
      this.supplementTitle = "补录员工离职记录";
    },
    /** 提交补录表单 */
    submitSupplementForm() {
      this.$refs["supplementForm"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.supplementForm.changeId != null) {
            updateEmployeeChange(this.supplementForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.supplementOpen = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            employeeLeave({
              changeType: 1,
              infoId: this.supplementForm.infoId,
              changeTime: this.supplementForm.changeTime,
              changeHandler: this.supplementForm.changeHandler,
              changeReason: this.supplementForm.changeReason,
              attachment: this.supplementAttachment,
              remark: this.supplementForm.remark
            }).then(response => {
              this.$modal.msgSuccess("员工离职记录补录成功");
              this.supplementOpen = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const changeIds = row.changeId || this.ids;
      this.$modal.confirm('是否确认删除员工离职记录编号为"' + changeIds + '"的数据项？').then(() => {
        this.loading = true;
        return delEmployeeChange(changeIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/employeeChange/export', {
        ...this.queryParams
      }, `employeeChange_${new Date().getTime()}.xlsx`)
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.viewRecord = row;
      this.viewOpen = true;
    }
  }
};
</script>
