<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <div class="tips">
      <p>
        国内地点经纬度参考：<a
          href="https://lbs.amap.com/tools/picker"
          target="_blank"
          >https://lbs.amap.com/tools/picker</a
        >
      </p>
      <p>
        国外地点经纬度参考：<a
          href="https://www.google.com/maps"
          target="_blank"
          >https://www.google.com/maps</a
        >
      </p>
    </div>

    <el-table v-loading="loading" :data="areaList">
      <el-table-column label="序号" type="index" />
      <el-table-column label="地点" prop="name"> </el-table-column>
      <el-table-column label="经纬度" width="180">
        <template slot-scope="scope">
          <span> {{ scope.row.lng }}, {{ scope.row.lat }} </span>
        </template>
      </el-table-column>
      <el-table-column label="打卡半径(米)" align="center" prop="radius">
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="editArea(scope.row)"
            >编辑</el-button
          >

          <el-button size="mini" type="text" @click="handleDelete(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="460px" append-to-body>
      <el-form ref="form" :rules="rules" :model="formData" label-position="top">
        <el-form-item label="地点名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入地点名称" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model.trim="formData.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model.trim="formData.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="打卡半径(米)" prop="radius">
          <el-input
            v-model.number="formData.radius"
            placeholder="请输入打卡半径(米)"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList, addConfig, deleteConfig } from "@/api/oa/attendance/config";

export default {
  name: "AttendanceAreaConfig",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },

      // 表单校验
      rules: {
        name: [{ required: true, message: "地点不能为空", trigger: "blur" }],
        lng: [{ required: true, message: "经度不能为空", trigger: "blur" }],
        lat: [{ required: true, message: "维度不能为空", trigger: "blur" }],
        radius: [
          { required: true, message: "打卡半径不能为空", trigger: "blur" },
        ],
      },

      // 表单数据
      formData: {
        name: "",
        lng: "",
        lat: "",
        radius: 100,
      },

      // 区域列表
      areaList: [],

      // 模态框状态
      showDeleteModal: false,
    };
  },
  computed: {},
  mounted() {
    this.getList();
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.formData.id != null) {
            addConfig(this.formData)
              .then((res) => {
                if (res.code == 200) {
                  this.open = false;
                  this.getList();
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addConfig(this.formData)
              .then((res) => {
                if (res.code == 200) {
                  this.open = false;
                  this.getList();
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    getList() {
      this.loading = true;
      queryList().then((response) => {
        this.areaList = response;
        this.loading = false;
      });
    },

    // 编辑区域
    editArea(area) {
      this.title = "编辑";
      this.formData = JSON.parse(JSON.stringify(area));
      this.open = true;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.name + '"的数据项？')
        .then(function () {
          return deleteConfig(row.id);
        })
        .then(() => {
          this.getList();
        })
        .catch(() => {});
    },

    reset() {
      this.formData = {
        name: "",
        lng: "",
        lat: "",
        radius: 100,
      };
      this.resetForm("form");
    },
  },
};
</script>

<style lang="scss" scoped>
.tips {
  background-color: #f2f6fc;
  padding: 15px;
  margin-bottom: 10px;
  p {
    margin: 0;
    a {
      color: #409eff;
    }
  }
}
</style>
