<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item
            width="100"
            prop="deviceName"
            :label="$t('dataEnquiry.zdmc')"
          >
            <el-input
              v-model="queryForm.deviceName"
              size="small"
              :placeholder="$t('dataEnquiry.qsrzdmc')"
            ></el-input>
          </el-form-item>
          <el-form-item
            width="100"
            prop="signUser"
            :label="$t('clock.dkr')"
            label="打卡人"
          >
            <el-input
              v-model="queryForm.signUser"
              size="small"
              :placeholder="$t('clock.qsrdkr')"
            ></el-input>
          </el-form-item>
          <el-form-item width="130" prop="type" :label="$t('dataEnquiry.zt')">
            <el-select
              v-model="queryForm.type"
              filterable
              :placeholder="$t('dataEnquiry.qxz')"
            >
              <el-option
                v-for="item in typeData"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="84px">
        <vab-query-form>
          <el-form-item width="100" prop="time" :label="$t('defalult.xzrq')">
            <el-date-picker
              v-model="queryForm.time"
              size="small"
              type="daterange"
              align="right"
              :start-placeholder="$t('defalult.ksrq')"
              :end-placeholder="$t('defalult.jsrq')"
              @change="changeTime"
            ></el-date-picker>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-form-item width="100">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
        <el-button type="primary" size="small" @click="exportData">
          {{ $t('defalult.dc') }}
        </el-button>
      </el-form-item>
    </div>
    <el-table
      id="table"
      v-loading="listLoading"
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="deviceName"
        :label="$t('dataEnquiry.zdmc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="date"
        :label="$t('defalult.rq')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type"
        :label="$t('clock.dklx')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inTime"
        :label="$t('clock.qdsj')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="outTime"
        :label="$t('clock.qtsj')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="signUser"
        :label="$t('clock.dkr')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column prop="location" :label="$t('clock.dkdd')" align="center">
        <template #default="scope">
          <span v-show="!scope.row.editFlag">
            {{ scope.row.location }}
          </span>
          <el-input
            v-show="scope.row.editFlag"
            v-model="scope.row.location"
            @blur="editClock(scope.row)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        align="center"
        width="150"
      >
        <template #default="{ row }">
          <el-button type="text" @click="trajectory(row)">
            {{ $t('clock.gj') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <trajectory ref="trajectory"></trajectory>
  </div>
</template>

<script>
import { getClockData, doEdit, doDelete, doExport } from '@/api/clock';
import { getDate, getPastTime } from '@/common/times';
import ajax from '@/api/download';
import trajectory from './trajectory';
export default {
  components: {
    trajectory,
  },
  data() {
    return {
      tableData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceName: '',
        signUser: '',
        start: '',
        end: '',
        time: '',
        projectId: '',
        type: 2,
      },
      typeData: [
        {
          label: 'all',
          value: 2,
        },
        {
          label: '站点打卡',
          value: 0,
        },
        {
          label: '外出打卡',
          value: 1,
        },
      ],
      form: {
        id: '',
        location: '',
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.setDefaultTime();
    setTimeout(() => {
      this.fetchData();
    }, 1000);
  },
  methods: {
    changeTime() {
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    // 轨迹
    trajectory(row) {
      console.log(row);
      this.$refs['trajectory'].loadScript({ userId: row.userId });
    },
    setDefaultTime() {
      this.queryForm.time = getPastTime(1);
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    async fetchData() {
      const { data } = await getClockData(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
    },
    //搜索
    search() {
      this.fetchData();
    },
    handleEdit(row) {
      // v.value = JSON.parse(JSON.stringify(row));
      row.editFlag = true;
    },
    //修改
    async editClock(row) {
      const that = this;
      this.$confirm('你确定要编辑当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.form.id = row.id;
          this.form.location = row.location;
          const { msg } = doEdit(this.form);
          this.$notify({
            title: '操作成功',
            type: 'success',
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消编辑',
          });
        });
    },
    //导出
    async exportData() {
      const url = await doExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '打卡记录.xls' });
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    //删除
    deletes(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deletes([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
