<template>
  <basic-container style="position: relative">
    <avue-crud
      :data="infoData"
      :option="options"
      :search.sync="search"
      :table-loading="visible"
      :page.sync="infoPages"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import {unemploymentInsuranceServiceCount, listAllServe} from '@/api/report/report'
import CustomLoading from "@/components/Custom-Loading/index.vue";
import {dateFormat} from "@/util/date";
import search from "@/page/index/search.vue";
import {deepClone} from "@/util/util";

const columnEnum = {
  "data1": "服务总人数",
  "data2": "推送政策总人次",
  "data3": "推送岗位总人次",
  "data4": "跟踪服务总次数",
  "data5": "服务成功总次数"
}
const pages = {total: 0, size: 10, currentPage: 1}
const baseOptions = {
  dateBtn: false,
  addBtn: false,
  editBtn: false,
  delBtn: false,
  height: "auto",
  reserveSelection: false,
  border: true,
  columnBtn: false,
  refreshBtn: false,
  menu: false,
  tip: false,
  searchMenuSpan: 6,
  selection: false,
  headerAlign: 'center',
  align: 'center',
}
export default {
  components: {CustomLoading},
  data() {
    return {
      infoPages: Object.assign({}, pages),
      infoData: [],
      visible: false,
      search: {},
      serveData: [],
      headTitle: '',
      searchServeId: '',
    }
  },
  created() {
    this.getServeList().then((res) => {
      if (res.length) {
        this.headTitle = res[0].name
        this.search.serveId = res[0].id
        this.searchServeId = res[0].id
        this.getList()
      }
    })
  },
  computed: {
    options() {
      const serveHead = [{
        label: this.headTitle,
        display: false,
        children: Object.keys(columnEnum).map((key) => ({
          label: columnEnum[key],
          prop: key,
          display: false,
        }))
      }]
      return {
        ...baseOptions,
        column: [{
          label: '服务名称',
          prop: 'serveId',
          hide: true,
          display: false,
          type: 'select',
          searchSpan: 6,
          search: true,
          searchClearable: false,
          dicData: this.serveData,
          props: {
            label: 'name',
            value: 'id'
          }
        }, {
          label: '机构名称',
          prop: 'name',
          search: true,
          searchSpan: 6,
        },
          ...serveHead,
          {
            label: '日期',
            prop: 'date',
            hide: true,
            display: false,
            type: 'month',
            format: "yyyy-MM",
            valueFormat: "yyyy-MM",
            searchSpan: 6,
            searchRange: true,
            search: true,
          }]
      }
    }
  },
  methods: {
    sizeChange(size) {
      this.infoPages.size = size
      this.getList()
    },
    currentChange(page) {
      this.infoPages.page = page
      this.getList()
    },
    searchChange(values, done) {
      const ele = this.serveData.filter((item) => values.serveId === item.id)[0]
      this.headTitle = ele.name
      this.searchServeId = values.serveId
      this.getList().then(() => done())
    },
    resetChange() {
      this.search = {serveId: this.searchServeId}
      let params = {size: 10, current: 1}
      this.getList()
    },
    getServeList() {
      return new Promise(async (resolve, reject) => {
        let resData = await listAllServe({id: '1762444035302395906'})
        if (resData.data.code === 200) {
          this.serveData = resData.data.data
          resolve(resData.data.data)
        } else {
          reject()
        }
      })
    },
    getList(values) {
      return new Promise(async (resolve, reject) => {
        this.visible = true
        let params = {
          size: this.infoPages.size,
          current: this.infoPages.currentPage,
          ...this.search,
        }
        // if (Array.isArray(this.search.date) && this.search.date.length) {
        //   const [stime, etime] = this.search.date
        //   params.stime = dateFormat(stime, "yyyy-MM-dd")
        //   params.etime = dateFormat(etime, "yyyy-MM-dd")
        //   delete params.date
        // }
        const copySearch = deepClone(params)
        let resData = await unemploymentInsuranceServiceCount(params)
        if (resData.data.code === 200) {
          const {records, current, total, size} = resData.data.data
          this.infoData = records
          this.infoPages = {...this.infoPages, currentPage: current, total, size}
          this.visible = false;
          resolve(records)
          setTimeout(() => {
            this.search = copySearch
          }, 0)
        } else {
          reject()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
