<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px"
      style="position: relative;">
      <el-form-item label="项目名称" prop="projectId">
        <el-select :disabled="prjDisabled" v-model="queryParams.projectId" placeholder="请选择项目" clearable
          @change="doQuerySub()">
          <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.projectName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="总包单位" prop="subDeptId">
        <el-select v-model="queryParams.subDeptId" placeholder="请选择总包单位" clearable>
          <el-option v-for="(item, index) in depts" :key="index" :label="item.deptName" :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分包商类型" prop="companyTypeId">
        <el-select v-model="queryParams.companyTypeId" placeholder="请选择分包商类型" clearable>
          <el-option value="" label="所有"></el-option>
          <el-option value="101" label="总包人员"></el-option>
          <el-option value="102" label="监理人员"></el-option>
          <el-option value="103" label="劳务人员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="" prop="state">
        <el-radio-group v-model="queryParams.state" size="mini">
          <el-radio-button label="">全部人员</el-radio-button>
          <el-radio-button label="0">在岗</el-radio-button>
          <el-radio-button label="1">已退场</el-radio-button>

        </el-radio-group>
      </el-form-item>

      <el-form-item style="position: absolute;right:0px;">
        <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" v-if="1 == 2">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['project:attendanceWorker: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="['project:attendanceWorker: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="['project:attendanceWorker: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="['project:attendanceWorker:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceWorkerList" @selection-change="handleSelectionChange">

      <el-table-column label="照片" align="center" prop="id">
        <template slot-scope="{row}">
          <el-image :src="row.recentPhoto" :preview-src-list="[row.recentPhoto]" style="height:60px" />
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="性别" align="center" prop="attendanceType">
        <template slot-scope="{row}">{{ row.gender == 1 ? '女' : '男' }}</template>
      </el-table-column>
      <el-table-column label="进场时间" align="center" prop="inTime" v-if="1 == 2">
        <template slot-scope="{row}">{{ row.enterDate | formatDate }}</template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{row}">{{ row.state == 1 ? '已退场' : '进场' }}</template>
      </el-table-column>
      <el-table-column label="分包商名称" align="center" prop="companyName">
        <template slot-scope="{row}">{{ row.companyName || row.remark }}</template>
      </el-table-column>
      <el-table-column label="所属班组" align="center" prop="groupName" />
      <el-table-column label="岗位/工种" align="center" prop="workTypeName" />
    </el-table>

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


  </div>
</template>

<script>
import { queryAttendanceWorker, listJgwAttendanceWorker, getAttendanceWorker, delAttendanceWorker, addAttendanceWorker, updateAttendanceWorker, queryAttendanceData } from "@/api/project/attendanceWorker";
import { listAttendanceConfig } from '@/api/project/attendanceConfig.js'
export default {
  name: "AttendanceWorker",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤人员基本属性表格数据
      attendanceWorkerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        companyTypeId:"",
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        subDeptId: null,
        name:null,
        state: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectOptions: [],
      depts: [],
      prjDisabled: false,
      cfgList: [],
    };
  },
  created() {
    this.$api.publics.getMyProjectList({}).then((response) => {
      this.projectOptions = response.rows;
    });
    listAttendanceConfig({ pageNum: 1, pageSize: 100 }).then(d => {
      this.cfgList = d.rows || [];
    });
  },
  methods: {
    initMe(prj) {
      this.queryParams.projectId = prj.id;
      this.prjDisabled = true;
      this.doQuerySub(true);
    },
    doQuerySub(init) {
      this.queryParams.state = "";
      let tmps = this.prjDept2 && this.prjDept2[this.queryParams.projectId] ? this.prjDept2[this.queryParams.projectId] || [] : [];
      if (tmps.length > 0) {
        this.depts = tmps;
        if (tmps.length == 1) {
          this.queryParams.subDeptId = tmps[0].deptId;
        } else {
          this.queryParams.subDeptId = '';
        }
        if (init) {
          this.getList();
        }
        return;
      }
      this.$api.publics.queryUnitList({
        projectId: this.queryParams.projectId,
        unitTypes: "2".split(","),
      }).then((d) => {
        let objs = d.rows || [];
        if (!this.prjDept2) {
          this.prjDept2 = {};
        }
        this.prjDept2[this.queryParams.projectId] = objs;
        this.depts = objs;
        if (objs.length == 1) {
          this.queryParams.subDeptId = objs[0].deptId;
        } else {
          this.queryParams.subDeptId = '';
        }
        if (init) {
          this.getList();
        }
      });
    },
    /** 查询考勤人员基本属性列表 */
    getList() {      
      this.loading = true;
      let tmps = this.cfgList.filter(d => d.projectId == this.queryParams.projectId && d.subDeptId == this.queryParams.subDeptId);
      let ajax = queryAttendanceWorker;
      if (tmps.length > 0 && tmps[0].vendorsCode == 'jgw') {
        ajax = listJgwAttendanceWorker;
      }
      ajax(this.queryParams).then(response => {
        this.attendanceWorkerList = (response.rows || []).map(it => {
          it.enterDate = it.enterDate ? new Date(it.enterDate * 1) : null;
          return it;
        });
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    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.open = true;
      this.title = "添加考勤人员基本属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getAttendanceWorker(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤人员基本属性";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考勤人员基本属性编号为"' + ids + '"的数据项？').then(function () {
        return delAttendanceWorker(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/attendanceWorker/export', {
        ...this.queryParams
      }, `attendanceWorker_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
