<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="name">
        <el-input v-model="queryParams.name" placeholder="请输入字典名称" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="员工编号" prop="employeeNumber">
        <el-input v-model="queryParams.employeeNumber" placeholder="请输入" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="员工状态" prop="employeeStatus">
        <el-select v-model="queryParams.employeeStatus" placeholder="员工状态" clearable style="width: 240px">
          <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </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="['system:dict:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['system:dict:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="员工ID" align="center" prop="id" />
      <el-table-column label="姓名" align="center" :show-overflow-tooltip="true">
        <template v-slot="scope">
          <router-link :to="'/employee/historyData/' + scope.row.id" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="员工编号" align="center" prop="employeeNumber" />
      <el-table-column label="入职时间" align="center" prop="entryDate" />
      
      <el-table-column label="员工状态" align="center" prop="employeeStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.employeeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="当前可用年假" align="center" prop="remainingAnnualLeave"/>
      <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="['system:dict:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:dict:delete']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-detail" @click="handleDetail(scope.row)"
                     v-hasPermi="['system:dict:detail']">详情</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工ID">
              <el-input v-model="form.id" placeholder="请输入员工ID" />
            </el-form-item>
          </el-col>
          
          <el-col :span="24">
            <el-form-item label="员工部门" prop="deptId">
              <treeselect :default-expand-level="1" v-model="form.deptId" :options="deptOptions" :normalizer="normalizer" placeholder="选择部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工职位" prop="position">
              <el-input v-model="form.position" placeholder="请选择职位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工编号" prop="employeeNumber">
              <el-input v-model="form.employeeNumber" placeholder="请输入员工编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入职时间" prop="entryDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.entryDate" type="date" placeholder="选择日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="试用期期" prop="probationEndDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.probationEndDate" type="date" placeholder="选择日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
         
          <el-col :span="12">
            <el-form-item label="员工状态" prop="employeeStatus">
              <el-select v-model="form.employeeStatus" placeholder="请选择员工状态" clearable  style="width: 100%;">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PROMOTION_ACTIVITY_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入电子邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCardNumber">
              <el-input v-model="form.idCardNumber" placeholder="请输身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" placeholder="性别" clearable  style="width: 100%;">
                <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-col>
          <el-col :span="12">
            <el-form-item label="最高学历" prop="educationLevel">
              <el-input v-model="form.educationLevel" placeholder="最高学历" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
            </el-form-item>
          </el-col>
          
        
          <el-col :span="12">
            <el-form-item label="毕业院校" prop="graduationSchool">
              <el-input v-model="form.graduationSchool" placeholder="毕业院校" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="毕业时间" prop="graduationDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.graduationDate" type="date" placeholder="选择日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡号" prop="bankCardNumber">
              <el-input v-model="form.bankCardNumber" placeholder="银行卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡" prop="bankCardName">
              <el-input v-model="form.bankCardName" placeholder="银行卡名称" />
            </el-form-item>
          </el-col>
         
          <el-col :span="12">
            <el-form-item label="年假基数" prop="baseAnnualLeave">
              <el-input v-model="form.baseAnnualLeave" placeholder="年假基准天数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剩余年假" prop="remainingAnnualLeave">
              <el-input v-model="form.remainingAnnualLeave" placeholder="剩余年假天数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职时间" prop="departureDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.departureDate" type="date" placeholder="选择日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="离职原因" prop="resignationReason">
              <el-input v-model="form.resignationReason" type="textarea" placeholder="请输入离职原因"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </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="title" :visible.sync="openDetail" width="800px" append-to-body>
      <div class="widget-wrapper">
        <div class="widget-list" v-loading="loading">
          <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工姓名</div>
        </div>
        <div class="field-component">
          <span :title="form.name">{{ form.name }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工ID</div>
        </div>
        <div class="field-component">
          <span :title="form.id">{{ form.id }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工部门</div>
        </div>
        <div class="field-component">
          <span>{{ form.deptName }}</span>
        </div>
      </div>
    
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工职位</div>
        </div>
        <div class="field-component">
          <span>{{ form.position }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工编号</div>
        </div>
        <div class="field-component">
          <span>{{ form.employeeNumber }}</span>
        </div>
      </div>

      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">入职时间</div>
        </div>
        <div class="field-component">
          <span>{{ form.entryDate }}</span>
        </div>
      </div>

      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">试用期期</div>
        </div>
        <div class="field-component">
          <span>{{ form.probationEndDate }}</span>
        </div>
      </div>

      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">员工状态</div>
        </div>
        <div class="field-component">
          <span>{{ form.employeeStatus }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">电子邮箱</div>
        </div>
        <div class="field-component">
          <span>{{ form.email }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">身份证号</div>
        </div>
        <div class="field-component">
          <span>{{ form.idCardNumber }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">性别</div>
        </div>
        <div class="field-component">
          <span>{{ form.gender }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">最高学历</div>
        </div>
        <div class="field-component">
          <span>{{ form.educationLevel }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">手机号</div>
        </div>
        <div class="field-component">
          <span>{{ form.phone }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">毕业院校</div>
        </div>
        <div class="field-component">
          <span>{{ form.graduationSchool }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">毕业时间</div>
        </div>
        <div class="field-component">
          <span >{{ form.graduationDate }}</span>
        </div>
      </div>

      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">银行卡号</div>
        </div>
        <div class="field-component">
          <span>{{ form.bankCardNumber }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">银行卡</div>
        </div>
        <div class="field-component">
          <span>{{ form.bankCardName }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">年假基数</div>
        </div>
        <div class="field-component">
          <span>{{ form.baseAnnualLeave }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">剩余年假</div>
        </div>
        <div class="field-component">
          <span>{{ form.remainingAnnualLeave }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">离职时间</div>
        </div>
        <div class="field-component">
          <span>{{ form.departureDate }}</span>
        </div>
      </div>
      <div class="fx-field x-grid-col-12">
        <div class="field-label">
          <div class="field-name">离职原因</div>
        </div>
        <div class="field-component">
          <span>{{ form.resignationReason }}</span>
        </div>
      </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee, exportEmployee } from "@/api/presonnel/employee";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listDept } from "@/api/system/dept";
import { CommonStatusEnum } from '@/utils/constants'
import { getDictDatas, DICT_TYPE } from '@/utils/dict'

export default {
  name: "SystemDictType",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 部门列表
      deptOptions: [], // 部门属性结构
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail:false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: undefined,
        employeeNumber: undefined,
        employeeStatus: undefined,
        // createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        id: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
        employeeStatus: [
          { required: true, message: "员工状态不能为空", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phone: [
          {
            pattern: /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
      },

      // 枚举
      CommonStatusEnum: CommonStatusEnum,
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS)
    };
  },
  created() {
    this.getList();
    this.getListDept();
  },
  methods: {
     /** 查询部门列表 */
     getListDept() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      listEmployee(this.queryParams).then(response => {
        this.typeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        id: undefined,
        deptId: undefined,
        position:undefined,
        employeeNumber: undefined,

        entryDate: undefined,
        probationEndDate: undefined,
        employeeStatus: CommonStatusEnum.ENABLE,
        email: undefined,
        idCardNumber: undefined,

        gender: CommonStatusEnum.ENABLE,
        educationLevel: undefined,
        phone: undefined,
        graduationSchool: undefined,
        graduationDate: undefined,

        bankCardNumber: undefined,
        bankCardName: undefined,
        baseAnnualLeave: undefined,
        remainingAnnualLeave: undefined,
        departureDate: undefined,

        resignationReason: 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 = "添加";
    },
    
      /** 详情按钮操作 */
      handleDetail(row) {
      this.reset();
      const id = row.id;
      getEmployee(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getEmployee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除字典编号为"' + ids + '"的数据项?').then(function() {
          return delEmployee(ids);
        }).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 exportEmployee(params);
      }).then(response => {
        this.$download.excel(response, '.xls');
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
