<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="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用工类型" prop="employeeType">
        <el-select v-model="queryParams.employeeType" placeholder="请选择用工类型" clearable>
          <el-option
            v-for="dict in dict.type.use_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['employee:employee:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['employee:employee:edit']"-->
<!--        >修改</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="['employee:employee: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="['employee:employee:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column hidden label="id" align="center" prop="id" />-->
      <el-table-column label="用工类型" align="center" prop="employeeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.use_type" :value="scope.row.employeeType"/>
        </template>
      </el-table-column>
      <el-table-column label="名字" align="center" prop="realName" />
      <el-table-column label="手机" align="center" prop="phone" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="用工单位" align="center" prop="useUnit" />
      <el-table-column label="面试日期" align="center" prop="interviewDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.interviewDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职日期" align="center" prop="entryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="员工单价" align="center" prop="employeePrice" />
      <el-table-column label="供应商单价" align="center" prop="supplierPrice" />

      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employee:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['employee:employee: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"
    />

    <!-- 添加或修改员工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true">
        <el-form-item label="用工类型" prop="employeeType" label-width="105px" style="width: 100%">
          <el-select style="width: 200px;" v-model="form.employeeType" placeholder="请选择用工类型">
            <el-option
              v-for="dict in dict.type.use_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="名字" prop="realName" label-width="105px">
          <el-input v-model="form.realName" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="手机" prop="phone" label-width="105px">
          <el-input v-model="form.phone" placeholder="请输入手机" :disabled="phoneInputDisable"/>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard" label-width="105px">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="性别" prop="sex"  label-width="105px">
          <el-select v-model="form.sex" placeholder="请选择性别" style="width: 200px;" >
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nation" label-width="105px">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="年龄" prop="age" label-width="105px">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="用工单位" prop="useUnit" label-width="105px">
          <el-input v-model="form.useUnit" placeholder="请输入用工单位" />
        </el-form-item>
        <el-form-item label="面试日期" prop="interviewDate" label-width="105px" >
          <el-date-picker clearable style="width: 200px;"
            v-model="form.interviewDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择面试日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate" label-width="105px">
          <el-date-picker clearable style="width: 200px;"
            v-model="form.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商" prop="supplier" label-width="105px">
          <el-input v-model="form.supplier" placeholder="请输入供应商" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="员工单价" prop="employeePrice" label-width="105px">
          <el-input v-model="form.employeePrice" placeholder="请输入员工单价" style="width: 200px;"/> 元
        </el-form-item>
        <el-form-item label="供应商单价" prop="supplierPrice" label-width="87px">
          <el-input v-model="form.supplierPrice" placeholder="请输入供应商单价" style="width: 200px;"/> 元
        </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>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee } from "@/api/employee/employee";

export default {
  name: "Employee",
  dicts: ['use_type', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realName: null,
        phone: null,
        idCard: null,
        employeeType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "名字不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证不能为空", trigger: "blur" }
        ],
        employeeType: [
          { required: true, message: "用工类型不能为空", trigger: "change" }
        ],
        phoneInputDisable: true,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        realName: null,
        phone: null,
        idCard: null,
        sex: null,
        nation: null,
        age: null,
        useUnit: null,
        interviewDate: null,
        entryDate: null,
        supplier: null,
        employeePrice: null,
        supplierPrice: null,
        employeeType: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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.title = "添加员工";
      this.phoneInputDisable = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.phoneInputDisable = true;
      this.reset();
      const id = row.id || this.ids
      getEmployee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.phone.length != 11){
            this.$modal.msgError("手机号位数不正确");
            return
          }
          if (this.form.idCard.length != 18){
            this.$modal.msgError("身份证号位数不正确");
            return
          }
          if (this.form.id != null) {
            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('确认要删除【'+ row.realName +'】吗?').then(function() {
        return delEmployee(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employee/employee/export', {
        ...this.queryParams
      }, `employee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


