<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="离职日期" prop="resignationDate">
        <el-date-picker clearable
                        v-model="queryParams.resignationDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择离职日期">
        </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="['system:offboarding:add']"
        >新增
        </el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:offboarding:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:offboarding:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="offboardingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="姓名" align="center" prop="nickName"/>
      <el-table-column label="离职日期" align="center" prop="resignationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resignationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职面谈" align="center" prop="exitInterview">
        <template slot-scope="scope">
          <el-tag :type="scope.row.exitInterview===1?'success':'warning'">
            {{ scope.row.exitInterview === 1 ? '完成' : '未完成' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工作交接" align="center" prop="handoverCompleted">
        <template slot-scope="scope">
          <el-tag :type="scope.row.handoverCompleted===1?'success':'warning'">
            {{ scope.row.handoverCompleted === 1 ? '完成' : '未完成' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="离职原因" align="center" prop="exitReason"/>
      <el-table-column label="最终工资" align="center" prop="finalSalary"/>
      <el-table-column label="离职申请书" align="center" prop="resignationApplicationSubmitted">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.resignationApplicationSubmitted === 0">
            未提交
          </el-tag>
          <div v-else>
            <!-- 附件列表 -->
            <file-preview v-model="scope.row.accessory" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="离职证明" align="center" prop="exitCertificateIssued">
        <template slot-scope="scope">
          <el-tag :type="scope.row.exitCertificateIssued===1?'success':'warning'">
            {{ scope.row.exitCertificateIssued === 1 ? '已开据' : '未开据' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="离职状态" align="center" prop="offboardingConfirmed">
        <template slot-scope="scope">
          <el-tag :type="scope.row.exitCertificateIssued===1?'success':'warning'">
            {{ scope.row.exitCertificateIssued === 1 ? '已离职' : '手续未完善' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.dataBackupCompleted!==1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:offboarding:edit']"
          >离职跟进
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:offboarding:remove']"
          >删除
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            v-if="scope.row.offboardingConfirmed===1&&scope.row.dataBackupCompleted!==1"
            @click="handleClose(scope.row)"
            v-hasPermi="['system:offboarding:remove']"
          >离职完成
          </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"
    />
  </div>
</template>

<script>
import {listOffboarding, getOffboarding, delOffboarding, addOffboarding, updateOffboarding} from "@/api/oa/offboarding";
import {delUser} from "@/api/system/user";
import FilePreview from "@/components/FilePreview/index.vue";

export default {
  name: "Offboarding",
  components: {FilePreview},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离职管理表格数据
      offboardingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        resignationDate: undefined,
        exitInterview: undefined,
        handoverCompleted: undefined,
        exitReason: undefined,
        finalSalary: undefined,
        resignationApplicationSubmitted: undefined,
        dataBackupCompleted: undefined,
        handoverArranged: undefined,
        salarySettled: undefined,
        benefitsSettled: undefined,
        exitCertificateIssued: undefined,
        offboardingConfirmed: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  computed: {
    realSrc() {
      if (!this.src) {
        return;
      }
      let real_src = this.src.split(",")[0];
      return real_src;
    },
  },
  methods: {

    // 获取文件名称
    getFileName(name) {
      console.log(name)
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 操作离职删除oa账号
    handleClose(row) {
      this.$confirm('此操作将永久删除该办公系统账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delUser(row.userId).then(response => {
          row.dataBackupCompleted = 1;
          updateOffboarding(row).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          })

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    /** 查询离职管理列表 */
    getList() {
      this.loading = true;
      listOffboarding(this.queryParams).then(response => {
        this.offboardingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        offboardingId: undefined,
        userId: undefined,
        resignationDate: undefined,
        exitInterview: undefined,
        handoverCompleted: undefined,
        exitReason: undefined,
        finalSalary: undefined,
        resignationApplicationSubmitted: undefined,
        dataBackupCompleted: undefined,
        handoverArranged: undefined,
        salarySettled: undefined,
        benefitsSettled: undefined,
        exitCertificateIssued: undefined,
        offboardingConfirmed: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.offboardingId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/people/addOffboarding')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/people/updateOffboarding/' + row.offboardingId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.offboardingId != null) {
            updateOffboarding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addOffboarding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const offboardingIds = row.offboardingId || this.ids;
      this.$modal.confirm('是否确认删除离职管理编号为"' + offboardingIds + '"的数据项？').then(() => {
        this.loading = true;
        return delOffboarding(offboardingIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/offboarding/export', {
        ...this.queryParams
      }, `offboarding_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
