<template>
  <el-dialog
    :visible.sync="dialogVisible"
    append-to-body
    title="考勤情况"
    top="0"
    width="1200px"
  >
    <el-form
      inline
      :model="searchForm"
      style="padding: 10px; background: #0e3c75; margin-bottom: 10px"
    >
      <el-form-item label="考勤时间">
        <el-date-picker
          v-model="searchForm.date"
          type="date"
          value-format="yyyy-MM-dd"
          clearable
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="岗位类型">
        <el-select v-model="searchForm.position_type" clearable>
          <el-option
            v-for="(option, index) in positionTypeData"
            :key="index"
            :value="option.value"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item 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 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 label="姓名">
        <el-input
          v-model="searchForm.person_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
      @row-click="showCheckInDetail"
    >
      <el-table-column label="考勤拍照" align="center" width="90">
        <template slot-scope="scope">
          <el-image :src="scope.row.face_img_url" class="previewImg" />
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="name" align="center">
      </el-table-column>
      <el-table-column label="所属单位" prop="company_name" align="center">
      </el-table-column>
      <el-table-column
        label="所在岗位"
        prop="position_type_name"
        align="center"
      >
      </el-table-column>
      <el-table-column label="工种" prop="work_types_name" align="center">
        <template slot-scope="scope">
          {{ scope.row.work_types_name.join(",") }}
        </template>
      </el-table-column>
      <el-table-column
        label="最早入场时间"
        prop="in_record_time"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="最近出场时间"
        prop="out_record_time"
        align="center"
      >
      </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>
    <CheckInDetail
      :itemId="personId"
      :visible="singleCheckInVisible"
      @changeVisible="(val) => (singleCheckInVisible = val)"
    ></CheckInDetail>
  </el-dialog>
</template>
<script>
import CheckInDetail from "./CheckInDetail";
import { getCheckInListByDate, getPersonListConfigData } from "@/api/index";
export default {
  name: "CheckInList",
  components: { CheckInDetail },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    currentIn: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      currentPage: 1,
      total: 0,
      searchForm: {
        date: this.getTime(new Date().getTime()),
      },
      pageSize: 10,
      tableData: [],
      unitTypeData: [],
      positionTypeData: [],
      workTypeData: [],
      singleCheckInVisible: false,
      personId: "",
    };
  },
  mounted() {
    getPersonListConfigData().then((res) => {
      this.unitTypeData = res.data.unitTypeData;
      this.positionTypeData = [
        ...res.data.normalPositionTypeData,
        ...res.data.managerPositionTypeData,
      ]; //这里两个需要合并
      this.workTypeData = res.data.workTypeData;
    });
  },
  methods: {
    showCheckInDetail(row) {
      this.personId = row.person_id;
      this.singleCheckInVisible = true;
    },
    handleCurrentChange(current) {
      getCheckInListByDate({
        ...this.searchForm,
        current_in: this.currentIn ? 1 : "",
        limit: this.pageSize,
        page: current,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = current;
      });
    },
    handleSizeChange(size) {
      getCheckInListByDate({
        ...this.searchForm,
        current_in: this.currentIn ? 1 : "",
        limit: size,
        page: 1,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = 1;
      });
    },
    searchRecords() {
      getCheckInListByDate({
        ...this.searchForm,
        current_in: this.currentIn ? 1 : "",
        limit: this.pageSize,
        page: 1,
      }).then((res) => {
        this.total = res.data.total;
        this.tableData = res.data.data;
        this.currentPage = 1;
      });
    },
    getTime(ts) {
      let date = new Date(ts);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month > 9 ? month : "0" + month;
      day = day < 10 ? "0" + day : day;
      let today = year + "-" + month + "-" + day;
      return `${today}`;
    },
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("changeVisible", val);
      },
    },
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.searchForm = {
          date: this.getTime(new Date().getTime()),
        };
        this.searchRecords();
      } else {
        this.tableData = [];
        this.currentPage = 1;
        this.pageSize = 10;
      }
    },
  },
};
</script>
<style lang="less" scoped>
@import "@/assets/common/element.less";
.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;
}
::v-deep .el-date-editor {
  width: 140px !important;
}
::v-deep .el-table__row {
  cursor: pointer;
}
</style>