<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择小区" clearable>
          <el-option v-for="item in communityList" :key="item.communityId" :value="item.communityId" :label="item.communityName" />
        </el-select>
      </el-form-item>
      <el-form-item label="打卡人员" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入打卡人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打卡时间" prop="clockTime">
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.clockTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择打卡时间">-->
<!--        </el-date-picker>-->
        <el-date-picker
          v-model="clockTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
<!--      <el-form-item label="角色" prop="clockUserRole">-->
<!--        <el-select v-model="queryParams.clockUserRole" placeholder="请选择角色" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.wx_clock_user_role"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </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="['business:clock:add']"-->
<!--        >新增</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="['business:clock:edit']"-->
<!--        >修改</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="['business:clock:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:clock:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clockList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键" align="center" prop="clockId" v-if="false"/>-->
      <el-table-column label="打卡人员" align="center" prop="accountName" />
      <el-table-column label="小区名称" align="center" prop="communityName" />
      <el-table-column label="打卡地址" align="center" prop="clockAddress" />
      <el-table-column label="打卡图片" align="center" prop="clockImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.clockImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="打卡时间" align="center" prop="clockTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.clockTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡人角色" align="center" prop="clockUserRole">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.account_type" :value="scope.row.clockUserRole"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:clock:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:clock:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleInfo(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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="打卡人员ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入打卡人员ID" />
        </el-form-item>
        <el-form-item label="打卡人员" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入打卡人员" />
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="小区名称" prop="communityName">
          <el-input v-model="form.communityName" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="打卡地址" prop="clockAddress">
          <el-input v-model="form.clockAddress" placeholder="请输入打卡地址" />
        </el-form-item>
        <el-form-item label="打卡图片" prop="clockImg">
          <el-input v-model="form.clockImg" placeholder="请输入打卡图片" />
        </el-form-item>
        <el-form-item label="打卡时间" prop="clockTime">
          <el-date-picker clearable
            v-model="form.clockTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡人角色" prop="clockUserRole">
          <el-select v-model="form.clockUserRole" placeholder="请选择打卡人角色">
            <el-option
              v-for="dict in dict.type.wx_clock_user_role"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </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>

    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <info :accountId="accountId"/>
    </el-dialog>
  </div>
</template>

<script>
import { listClock, getClock, delClock, addClock, updateClock } from "@/api/business/clock";
import {listCommunityAll} from "@/api/business/community";
import Info from "./info/index.vue";

export default {
  name: "Clock",
  dicts: ['account_type'],
  components: {Info},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打卡表格数据
      clockList: [],
      communityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountId: undefined,
        accountName: undefined,
        communityId: undefined,
        communityName: undefined,
        clockTime: undefined,
        clockUserRole: undefined
      },
      // 表单参数
      form: {},
      clockTime: [],
      accountId: null,
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "打卡人员ID不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "打卡人员不能为空", trigger: "blur" }
        ],
        communityId: [
          { required: true, message: "小区ID不能为空", trigger: "blur" }
        ],
        communityName: [
          { required: true, message: "小区名称不能为空", trigger: "blur" }
        ],
        clockAddress: [
          { required: true, message: "打卡地址不能为空", trigger: "blur" }
        ],
        clockImg: [
          { required: true, message: "打卡图片不能为空", trigger: "blur" }
        ],
        clockTime: [
          { required: true, message: "打卡时间不能为空", trigger: "blur" }
        ],
        clockUserRole: [
          { required: true, message: "打卡人角色不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCommunityListAll();
  },
  methods: {
    /** 查询打卡列表 */
    getList() {
      this.loading = true;
      if (null != this.clockTime && this.clockTime.length > 0) {
        this.queryParams.clockStartTime = this.clockTime[0];
        this.queryParams.clockEndTime = this.clockTime[1];
      }else {
        this.queryParams.clockStartTime = undefined;
        this.queryParams.clockEndTime = undefined;
      }
      listClock(this.queryParams).then(response => {
        this.clockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询全部小区列表 */
    getCommunityListAll() {
      listCommunityAll().then(response => {
        this.communityList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clockId: undefined,
        userId: undefined,
        userName: undefined,
        communityId: undefined,
        communityName: undefined,
        clockAddress: undefined,
        clockImg: undefined,
        clockTime: undefined,
        clockUserRole: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.clockTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clockId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleInfo(row) {
      this.reset();
      this.open1 = true;
      this.title = "打卡记录";
      this.accountId = row.accountId
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const clockId = row.clockId || this.ids
      getClock(clockId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改打卡";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.clockId != null) {
            updateClock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addClock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clockIds = row.clockId || this.ids;
      this.$modal.confirm('是否确认删除打卡编号为"' + clockIds + '"的数据项？').then(() => {
        this.loading = true;
        return delClock(clockIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/clock/export', {
        ...this.queryParams
      }, `clock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
