<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="打卡人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择打卡人" >
          <el-option
            v-for="dict in userList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId"
          />
        </el-select>
        <!--<el-input-->
          <!--v-model="queryParams.nickname"-->
          <!--placeholder="请输入打卡人"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      </el-form-item>
      <el-form-item label="打卡时间" prop="clockTime">
        <el-date-picker
          v-model="queryParams.soudata"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.soudata"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择打卡时间">-->
        <!--</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="primary"-->
          <!--plain-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['system:in:add']"-->
        <!--&gt;新增</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="['system:in:edit']"-->
        <!--&gt;修改</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="['system:in:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:in:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="打卡时间" align="center" prop="id" />-->
      <el-table-column label="打卡人" align="center" width="200" prop="nickname" />
      <el-table-column label="部门" align="center" prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="岗位" align="center" prop="post" :show-overflow-tooltip="true"/>
      <el-table-column label="打卡时间" align="center" prop="clockTime" width="180">

      </el-table-column>
      <!--<el-table-column label="经度" align="center" prop="longitude" />-->
      <!--<el-table-column label="纬度" align="center" prop="latitude" />-->
      <el-table-column label="打卡地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handledetail(scope.row)"
          >打卡位置</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改考勤打卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body destroy-on-close :close-on-click-modal="false" :before-close="openClose">
      <div style="width: 100%;">
        <div id="map" style="width: 100%;height: 500px;border: 1px solid"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIn, getIn, delIn, addIn, updateIn, exportIn } from "@/api/system/in";
import { EditorMap } from "@/utils/mapClass/map";
import { mapGetters } from "vuex";
import { listUser } from "@/api/system/user";

export default {
  name: "In",
  components: {
  },
  data() {
    return {
      detailmap:{},
      // 遮罩层
      loading: true,
      userList:[],
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤打卡表格数据
      inList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      soudata:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        soudata: null,
        begindata:'',
        enddata:''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getuserlist();
  },
  computed: {
    ...mapGetters(["systemSetting"])
  },
  methods: {
    getuserlist(){
      listUser({pageNum:1,pageSize:100}).then(response => {
        this.userList = response.rows;
        console.log(this.userList)
      })
    },

    /** 查询考勤打卡列表 */
    getList() {
      if(this.queryParams.soudata!=null){
        var data = this.queryParams.soudata;
        this.queryParams.begindata = data[0].getFullYear()+'-'+(data[0].getMonth()+1)+'-'+data[0].getDate();
        this.queryParams.enddata = data[1].getFullYear()+'-'+(data[1].getMonth()+1)+'-'+data[1].getDate()+" 23:59:59";
      }else{
        this.queryParams.begindata = null;
        this.queryParams.enddata = null;
      }
      this.loading = true;
      listIn(this.queryParams).then(response => {
        this.inList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        clockTime: null,
        longitude: null,
        latitude: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.soudata=null;
      this.queryParams.begindata = '';
      this.queryParams.enddata = '';
      this.resetForm("queryForm");
      this.handleQuery();
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考勤打卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIn(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤打卡";
      });
    },
    //地图初始化
    initMap(){
      const path = eval(this.systemSetting.map_center);
      this.gaoMap = new EditorMap(
        "map",
        {
          center: path,
          zoom: 12,
        },
        this
      );
    },
    handledetail(row){
      this.title = row.nickname+"("+row.clockTime+")"
      this.detailmap = row;
      this.open = true;
      this.$nextTick(() => {
        this.initMap();
        this.addLocation({longitude: row.longitude, latitude: row.latitude,nickName:row.address,iconType:"industryTaskInspect"});
      })
    },
    //位置上图
    addLocation(data){
      this.gaoMap.allDevice["industryTaskInspect"]?.forEach((item) =>{
        this.gaoMap.map.remove(item);
    })
      this.gaoMap.control= 2;
      this.gaoMap.addDevice(data,null);
      this.gaoMap.map.panTo([data.longitude, data.latitude]);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIn(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIn(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除考勤打卡编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delIn(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考勤打卡数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportIn(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    openClose(){
      this.open = false;
      this.gaoMap.map.destroy();
      this.gaoMap = null;
    }
  }
};
</script>
