<template>
  <div style="padding: 0 20px">

    <div ref="lxHeader">
      <lx-header icon="md-apps" text="审核管理" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
        <slot>
          <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;">
            <Input v-model="select.keyword" style="width: 200px;margin-right: 10px;" placeholder="搜索" />
            <Button type="primary" @click="getindex">查询</Button>
            <Button icon="ios-add" type="primary" style="margin-left: 10px;"
              @click="exportExcel(new Date().getTime().toString())">导出</Button>
            <!-- <Button icon="ios-add" type="primary" style="margin-left: 10px;"
              @click="$refs['check'].isShow = true,$refs['check'].type = 'add'">添加</Button> -->
            <!-- <Button icon="ios-add" type="primary" style="margin-left: 10px;"
              @click="$refs['imports'].show()">导入</Button> -->
          </div>
        </slot>
      </lx-header>
    </div>

    <xy-table :list="list" :total="total" @pageSizeChange="e => select.pageSize = e" @pageIndexChange="pageChange"
      :table-item="table">
      <template v-slot:btns>
        <el-table-column fixed="right" label="操作" width="260" header-align="center">
          <template slot-scope="scope">
            <Button v-if="scope.row.work_status=='待审核'" style="margin-left: 10px;" type="primary" size="small" @click="checkRecord(scope.row.id)">审核</Button>
            <Poptip transfer confirm title="确认要删除吗？" @on-ok="deleteRecord(scope.row)">
              <Button type="primary" style="margin-left: 10px;" size="small" ghost>删除</Button>
            </Poptip>
          </template>
        </el-table-column>
      </template>
    </xy-table>
    <check ref="check" @refresh="getindex"></check>


  </div>
</template>

<script>
  import {
    index,
    destroy
  } from "@/api/system/baseForm.js"
  import check from "@/views/checks/components/check.vue"
  import {
    Message
  } from 'element-ui'
  import * as XLSX from "xlsx";
  import { saveAs } from "file-saver";
  import {
    index as fieldIndex
  } from "@/api/system/customFormField";
  import {
    getparameter
  } from "@/api/system/dictionary";
  export default {
    components: {
      check
    },
    data() {
      return {
        select: {
          pageSize: 10,
          pageIndex: 1,
          keyword: ""
        },
        rukuType: '',
        customForm: {
          customFormId: 21,
          tableName: "workflows"
        },
        form: [],
        total: 0,
        list: [],
        table: [{
            label: '序号',
            type: 'index',
            fixed: 'left',
            width: 80
          },
          {
            label: "申请日期",
            width: 120,
            prop: 'work_date',
            align: 'center',
            fixed: 'left'
          },
          {
            label: "申请状态",
            width: 120,
            prop: 'work_status',
            align: 'center',
          },
          {
            label: "申请业务",
            width: 120,
            prop: 'guanlianbiaoming',
            align: 'center'
          },
          {
            label: "申请人",
            width: 120,
            prop: 'name',
            align: 'center'
          },
          {
            label: "所属街道",
            width: 120,
            prop: 'street',
            align: 'center'
          },
          {
            label: "身份证",
            width: 180,
            prop: 'idcard',
            align: 'center'
          },
          {
            label: "残疾证",
            width: 180,
            prop: 'number',
            align: 'center'
          },
          {
            label: "联系电话",
            width: 120,
            prop: 'mobile',
            align: 'center'
          },
          {
            label: "居住地",
            width: 240,
            prop: 'reside',
            align: 'left'
          }

        ]
      }
    },
    methods: {
      async getindex() {
        const res = await index({
          page_size: this.select.pageSize,
          page: this.select.pageIndex,
          table_name: this.customForm.tableName,
          filter: [{
            "key": "name",
            "op": "like",
            "value": this.select.keyword
          }],
        })
        this.list = res.data
        this.total = res.total
      },

      pageChange(e) {
        this.select.pageIndex = e
      },
      checkRecord(id, personid) {
        this.$refs['check'].id = id
        this.$refs['check'].person_id = personid
        this.$refs['check'].isShow = true
      },
      editorRecord(id, type) {
        this.$refs['addViewRecord'].id = id
        this.$refs['addViewRecord'].type = type
        this.$refs['addViewRecord'].isShow = true
      },
      deleteRecord(row) {
        destroy({
          id: row.id,
          table_name: this.customForm.tableName
        }).then(res => {
          Message({
            type: 'success',
            message: '删除成功'
          })
          this.getindex()
        })
      },
      // 获取表信息
      async getField() {
        // console.log(this.$route.meta.params, "---", this.$route)
        // return
        // if (this.$route.meta.params?.custom_form) {
        //   let decode = decodeURIComponent(this.$route.meta.params?.custom_form)
        //   try {
        //     let custom_form = JSON.parse(decode)
        //     this.customForm.customFormId = custom_form.custom_form_id
        //     this.customForm.tableName = custom_form.table_name
        //     console.log("123", this.customForm)
        //     // this.select.table_name = custom_form.table_name
        //   } catch (err) {
        //     console.warn(err)
        //   }
        // }
        const res = await fieldIndex({
          page: 1,
          page_size: 999,
          custom_form_id: this.customForm.customFormId,
          sort_name: 'sort',
          sort_type: 'asc',
        })
        if (res.data && res.data instanceof Array) {
          res.data.forEach(i => {
            if (i.field) {
              if (
                (i.edit_input === "checkbox" || i.edit_input === "radio" || i.edit_input === "select") &&
                i.parameter_id
              ) {
                getparameter({
                  id: i.parameter_id
                }).then((res) => {
                  i._paramters = res.detail ?? [];
                });
              }
              if (
                (i.edit_input === "checkbox" || i.edit_input === "radio" || i.edit_input === "select") &&
                i.link_table_name
              ) {
                index({
                  page: 1,
                  page_size: 999,
                  table_name: i.link_table_name,
                }).then((res) => {
                  i._paramters = res.data ?? [];
                });
              }
            }
          })
        }
        this.form = res.data
      },
      async exportExcel(sheetName) {
        const res = await index(Object.assign(this.select,{ page: 1,page_size: 99999,table_name: this.customForm.tableName}))
        if(res.data){
          let headers = this.form.map(i => {
            return {
              key: i.field,
              title: i.name
            }
          })
          const data = res.data.map(row => headers.map(header => row[header.key]));
          data.unshift(headers.map(header => header.title));
          const wb = XLSX.utils.book_new();
          const ws = XLSX.utils.aoa_to_sheet(data);
          XLSX.utils.book_append_sheet(wb, ws, sheetName);
          const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${sheetName}.xlsx`);
        }
      },
    },
    mounted() {
      this.getindex()
      this.getField()
    }

  }
</script>

<style scoped lang="scss">
</style>
