<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :class="['treeselectForm',{borderRed:!validTree}]" label="组织架构" prop="deptIds">
        <tree @treeselectFn="treeselectFn" :treeval="queryParams.deptIds" :ismultiple="true"></tree>
      </el-form-item>
      <el-form-item label="人员" prop="userId">
        <user :userId="queryParams.userId" :orgIds="queryParams.deptIds" @userIdFn="userIdFn"></user>
      </el-form-item>
      <el-form-item label="打卡类型" prop="checkinType">
        <el-select size="mini" v-model="queryParams.checkinType" clearable placeholder="请选择打卡类型">
          <el-option
            v-for="item in checkinTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起止时间" prop="datetimerange">
        <el-date-picker
          style="width: 300px;"
          v-model="datetimerange"
          type="datetimerange"
          align="right"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="datetimeRangeChange">
        </el-date-picker>
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:checkin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkinList" @row-dblclick="rowDblclickFn" row-class-name="cursorP">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="组织架构" align="center" prop="deptName" />
      <el-table-column label="人员编号" align="center" prop="userName" />
      <el-table-column label="人员" align="center" prop="nickName" />
      <el-table-column label="打卡类型" align="center" prop="checkinType" />
      <el-table-column label="异常类型" align="center" prop="exceptionType" />
      <el-table-column label="打卡时间" align="center" prop="checkinTime" width="150" />
<!--      <el-table-column label="经度" align="center" prop="lat" >-->
<!--        <template slot-scope="scope">-->
<!--          <div>{{scope.row.lng/1000000}}</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="纬度" align="center" prop="lng" >-->
<!--        <template slot-scope="scope">-->
<!--          <div>{{scope.row.lat/1000000}}</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="打卡地点" align="center" prop="locationTitle" width="180" />
      <el-table-column label="打卡地点详情" align="center" prop="locationDetail" width="250" :show-overflow-tooltip="true"/>
    </el-table>

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

    <!--    考勤地理位置-->
    <el-dialog title="考勤地理位置" :visible.sync="mapVisible" width="90%" append-to-body>
      <mymap :center="mapcenter" :mapVisible="mapVisible" @centerFn="centerFn"></mymap>
    </el-dialog>
  </div>
</template>

<script>
import { dbListCheckin, getCheckin, delCheckin, addCheckin, updateCheckin } from "@/api/sfa/checkin";
import tree from "@/views/components/sfa/treeselectParent";
import user from "@/views/components/sfa/user";
import mymap from '@/views/components/sfa/mapPosition'
export default {
  name: "Checkin",
  components: { user,tree,mymap},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打卡记录表格数据
      checkinList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
          checkinType:'',
      },
        datetimerange:[],
        checkinTypeOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      validTree:true,
        // 地图
        mapVisible:false,
        mapcenter:{
            lng:null,
            lat:null,
        },
    };
  },
  created() {
      this.getDicts("checkin_type").then(response => {
          this.checkinTypeOptions = response.data;
      });
    this.getList();
  },
  methods: {
      treeselectFn(val){
          this.queryParams.deptIds = val;
      },
      userIdFn(val){
          this.queryParams.userId = val;
      },
      userIdFocus(){
          if(this.queryParams.deptIds == null){
              this.$refs['queryParams'].validate((valid) => {
                  if (valid) {
                      this.validTree = true;
                  }else{
                      if(this.queryParams.deptIds==null){
                          this.validTree = false;
                      }else{
                          this.validTree = true;
                      }
                  }
              })
          }else{
              this.validTree = true;
          }
      },
      datetimeRangeChange(val){
          this.$forceUpdate()
          if(val == undefined || val == null){
              this.datetimerange = [];
              this.queryParams.startTime = '';
              this.queryParams.endTime = '';
          }else{
              this.queryParams.startTime = val[0];
              this.queryParams.endTime = val[1];
          }
      },
    /** 查询打卡记录列表 */
    getList() {
      this.loading = true;
      dbListCheckin(this.queryParams).then(response => {
        this.checkinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.datetimerange = [];
        this.datetimeRangeChange(this.datetimerange);
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/checkin/exportCheckin', {
        ...this.queryParams
      }, `打卡记录_${new Date().getTime()}.xlsx`)
    },
      rowDblclickFn(row, column, event){
          this.mapVisible = true;
          this.mapcenter.lng = row.lng/1000000;
          this.mapcenter.lat = row.lat/1000000;
      },
      centerFn(val){
          this.mapcenter = val;
      },
  }
};
</script>
<style>
  .cursorP{
    cursor: pointer;
  }
</style>
