<template>
  <section class="list">
    <ai-list>
      <ai-title slot="title" title="打卡管理" isShowBottomBorder>
        <template #rightBtn>
          <div flex>
            <ai-download url="/app/appwechatsigninfo/export" :params="{...search}" :instance="instance" fileName="打卡导出文件"/>
            <ai-dialog-btn dialogTitle="打卡提醒设置" width="500px" :submit="submit" @close="form={}" @open="getSetting">
              <el-button slot="btn" type="primary">设置</el-button>
              <el-form size="small" label-width="120px" ref="SettingForm" :model="form">
                <el-form-item label="早提醒时间">
                  <el-time-picker v-model="form.morningTime" placeholder="请选择" value-format="HH:mm:ss"/>
                </el-form-item>
                <el-form-item label="晚提醒时间">
                  <el-time-picker v-model="form.nightTime" placeholder="请选择" value-format="HH:mm:ss"/>
                </el-form-item>
                <el-form-item label="打卡间隔时间">
                  <el-input v-model="form.doInterval" placeholder="请填写打卡间隔时间"/>
                </el-form-item>
              </el-form>
            </ai-dialog-btn>
          </div>
        </template>
      </ai-title>
      <template #left>
        <ai-tree-menu title="组织部门" @search="handleSearchTree">
          <el-tree ref="DeptTree" :data="treeData" :props="{label:'name'}" :filter-node-method="(v,data)=>data.name.indexOf(v)>-1"
                   @node-click="handleSelectDept"/>
        </ai-tree-menu>
      </template>
      <template #blank>
        <div flex class="gap-16">
          <ai-bar v-for="(v,key) in sta" :key="key" card class="fill" :title="key"><b class="staNum" v-text="v"/></ai-bar>
        </div>
        <ai-card panel>
          <ai-search-bar>
            <template #left>
              <ai-search label="打卡时间">
                <el-date-picker v-model="search.createDateStart" size="small" placeholder="开始时间" value-format="yyyy-MM-dd" @change="reloadTable"/>
                <el-date-picker v-model="search.createDateEnd" size="small" placeholder="结束时间" value-format="yyyy-MM-dd" @change="reloadTable"/>
              </ai-search>
              <ai-select placeholder="打卡状态" v-model="search.status" :selectList="dict.getDict('wxSignStatus')" @change="reloadTable"/>
            </template>
            <template #right>
              <el-input size="small" placeholder="搜索用户" v-model="search.wxUserName" clearable @change="page.current=1,getTableData()"/>
            </template>
          </ai-search-bar>
          <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                    @getList="getTableData" :col-configs="colConfigs" :dict="dict"/>
        </ai-card>
      </template>
    </ai-list>
  </section>
</template>

<script>
import AiTreeMenu from "dui/packages/layout/AiTreeMenu";

export default {
  name: "list",
  components: {AiTreeMenu},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  data() {
    return {
      search: {name: "", createDateStart: "", createDateEnd: "", departmentId: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {prop: "wxUserName", label: "用户"},
        {prop: "departmentName", label: "部门"},
        {prop: "createDate", label: "打卡日期"},
        {prop: "createTime", label: "打卡时间"},
        {prop: "times", label: "打卡次数"},
        {prop: "status", label: "状态", dict: "wxSignStatus"}
      ],
      treeData: [],
      form: {},
      sta: {}
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/app/appwechatsigninfo/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data.records
          this.page.total = res.data.total
        }
      })
    },
    getDepartments() {
      this.instance.post("/app/wxcp/wxdepartment/listByUser").then(res => {
        if (res?.data) {
          this.treeData = this.$arr2tree(res.data, {parent: 'parentid'})
        }
      })
    },
    getSetting() {
      this.instance.post("/app/appwechatsignconfig/queryDetailById").then(res => {
        if (res?.data) {
          this.form = res.data
        }
      })
    },
    getStaData() {
      const {departmentId} = this.search
      this.instance.post("/app/appwechatsigninfo/querySignStatistic", null, {
        params: {departmentId}
      }).then(res => {
        if (res?.data) {
          this.sta = res.data
        }
      })
    },
    handleSearchTree(name) {
      this.$refs.DeptTree.filter(name)
    },
    reloadTable() {
      this.page.current = 1
      this.getTableData()
    },
    handleSelectDept(data) {
      this.search.departmentId = data.id
      this.reloadTable()
      this.getStaData()
    },
    submit() {
      return this.$refs.SettingForm.validate()
      .then(() => this.instance.post("/app/appwechatsignconfig/addOrUpdate", this.form))
      .then(res => {
        if (res?.code == 0) {
          return this.$message.success("提交成功")
        }
      })
    }
  },
  created() {
    this.getTableData()
    this.getDepartments()
    this.getStaData()
  }
}
</script>

<style lang="scss" scoped>
.list {
  height: 100%;

  .staNum {
    font-size: 20px;
  }
}
</style>
