<template>
  <el-dialog
    :visible.sync="dialogVisible"
    append-to-body
    :title="type == 2 ? '普通工人列表' : '关键岗位人员列表'"
    top="0"
    width="1200px"
  >
    <el-form
      inline
      :model="searchForm"
      style="padding: 10px; background: #0e3c75; margin-bottom: 10px"
    >
      <el-form-item v-if="type == 2" label="班组">
        <el-select v-model="searchForm.team_id" clearable>
          <el-option
            v-for="(option, index) in teamData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="type == 2" label="工种">
        <el-select v-model="searchForm.work_type" clearable>
          <el-option
            v-for="(option, index) in workTypeData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="type == 1" label="单位类型">
        <el-select v-model="searchForm.company_type" clearable>
          <el-option
            v-for="(option, index) in unitTypeData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="type == 1" label="岗位类型">
        <el-select v-model="searchForm.position_type" clearable>
          <el-option
            v-for="(option, index) in managerPositionTypeData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="searchForm.sex" clearable>
          <el-option
            v-for="(option, index) in sexData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="在岗状态">
        <el-select v-model="searchForm.duty_status" clearable>
          <el-option
            v-for="(option, index) in dutyStatusData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="searchForm.keywords" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button
          @click="searchRecords"
          style="background: rgba(19, 168, 195, 1); color: #fff; border: none"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="tableData" height="550px" stripe>
      <el-table-column
        label="姓名"
        prop="person_name"
        align="center"
      ></el-table-column>
      <el-table-column
        v-if="type == 2"
        label="所属参见单位"
        prop="company_name"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.company_type_name_arr.join(",") }}/{{
            scope.row.company_name
          }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 2"
        label="所在班组"
        prop="team_name_arr"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.team_name_arr.join(",") }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 2"
        prop="work_types_name"
        label="工种"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.work_types_name.join(",") }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 1"
        label="所属单位类型"
        prop="company_type_name_arr"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.company_type_name_arr.join(",") }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="type == 1"
        label="所属单位"
        prop="company_name"
        align="center"
      ></el-table-column>
      <el-table-column
        v-if="type == 1"
        prop="position_type_name"
        label="岗位类型"
        align="center"
      >
      </el-table-column>
      <el-table-column label="性别" prop="sex_name" align="center" width="90">
      </el-table-column>
      <el-table-column label="联系电话" prop="format_mobile" align="center">
      </el-table-column>
      <el-table-column label="在岗状态" align="center" width="90">
        <template slot-scope="scope">
          <span
            :style="{
              color: scope.row.duty_status == 1 ? '#5CFF6B' : '#C6DEF9',
            }"
            >{{ scope.row.duty_status_name }}</span
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-dialog>
</template>
<script>
import { getPersonList, getPersonListConfigData } from "@/api/index";
export default {
  name: "PersonList",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    type: {
      type: Number,
      default: 1,
    },
    workType: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      currentPage: 1,
      total: 0,
      searchForm: {},
      pageSize: 10,
      tableData: [],
      teamData: [],
      workTypeData: [],
      unitTypeData: [],
      managerPositionTypeData: [],
      sexData: [],
      dutyStatusData: [],
    };
  },
  mounted() {
    getPersonListConfigData().then((res) => {
      this.teamData = res.data.teamData;
      this.unitTypeData = res.data.unitTypeData;
      this.managerPositionTypeData = res.data.managerPositionTypeData; // 普通工人不需要岗位类型 管理人员需要
      this.sexData = res.data.sexData;
      this.dutyStatusData = res.data.dutyStatusData;
      this.workTypeData = res.data.workTypeData;
    });
  },
  methods: {
    handleCurrentChange(current) {
      getPersonList({
        ...this.searchForm,
        limit: this.pageSize,
        type: this.type,
        page: current,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = current;
      });
    },
    handleSizeChange(size) {
      getPersonList({
        ...this.searchForm,
        type: this.type,
        limit: size,
        page: 1,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = 1;
      });
    },
    searchRecords() {
      getPersonList({
        ...this.searchForm,
        type: this.type,
        limit: this.pageSize,
        page: 1,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = 1;
      });
    },
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("changeVisible", val);
      },
    },
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        if (this.workType && this.type == 2) {
          this.searchForm.work_type = this.workType;
        }
        this.searchRecords();
      } else {
        this.tableData = [];
        this.currentPage = 1;
        this.pageSize = 10;
        this.searchForm = {};
      }
    },
  },
};
</script>
<style lang="less" scoped>
.step-area {
  padding: 24px;
  background: #0e3b72;
}
.dialog-title {
  line-height: 44px;
  background: #0e3b72;
  text-align: center;
  font-size: 16px;
  color: #24f0b8;
  margin-bottom: 2px;
  margin-top: 12px;
}
.dialog-form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .dialog-item {
    width: calc(50% - 50px);
    margin-bottom: 2px;
    background: #114483;
    padding: 12px 24px;
    color: #c6def9;
    font-size: 14px;
    &.inline {
      width: 100%;
    }
    &:last-child {
      margin-bottom: 0;
    }
  }
  &.border {
    border-top: 10px solid #0e3b72;
  }
}
.imgs {
  width: 100px;
  height: 100px;
  margin-right: 12px;
  vertical-align: middle;
}

#scroll-dialog {
  max-height: 800px;
  overflow-y: auto;
}

::v-deep .el-step__icon.is-text {
  border: none !important;
  i {
    font-size: 24px;
  }
}
::v-deep .el-step__head.is-finish {
  color: #24f0b8;
  border-color: #24f0b8;
}
::v-deep .el-step__head.is-process {
  color: #c6def9;
}
.previewImg {
  width: 50px;
  height: 50px;
}
.construction-title {
  text-align: center;
  color: #fff;
  font-size: 14px;
  padding: 10px 20px 30px;
}
::v-deep .el-select {
  width: 120px;
}
</style>