<template>
  <div class="app-content">
    <a-form :form="form" :model="searchForm" layout="inline" class="search_form">
      <a-form-item>
        <a-input v-model="searchForm.unitName" placeholder="请输入单位名称" style="width: 200px" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" icon="search" @click="search">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <div class="submit-btn">
      <a-button type="primary" @click="exportData">导出Excel</a-button>
    </div>
    <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false" :loading="loading">
      <template slot="stateSwitch" slot-scope="record">
        <a-tag :color="'blue'" v-if="record.auditResult==1">待审核</a-tag>
        <a-tag :color="'green'" v-if="record.auditResult==10">审核通过</a-tag>
        <a-tag :color="'red'" v-if="record.auditResult==20">审核不通过</a-tag>
      </template>
      <template slot="option" slot-scope="record">
        <a-button type="link" size="small" @click="recordClick(record,'view')">查看</a-button>
        <a-button type="link" size="small" @click="recordClick(record,'audit')" v-if="record.auditResult==1">审核</a-button>
        <a-button type="link" size="small" @click="recordClick(record,'again') " v-if="record.auditResult==10||record.auditResult==20">重新审核</a-button>
      </template>
    </a-table>
    <a-pagination v-if="pagination.total > 0" :total="pagination.total" show-size-changer show-quick-jumper v-model="pagination.pageIndex" :page-size="pagination.pageSize" :page-size-options="pagination.pageSizeOptions" @showSizeChange="showSizeChange" @change="change" :showTotal="() => `共 ${pagination.total} 条`" />
    <a-modal v-model="visibleView" :title="'查看'" width="800px" :dialog-style="{ top: '20%' }" destroyOnClose>
      <template slot="footer">
        <a-button type="primary" @click="() => visibleView = false">
          确定
        </a-button>
      </template>
      <unit-view v-model="id"></unit-view>
    </a-modal>
    <a-modal v-model="visibleAudit" :title="'审核'" width="800px" :dialog-style="{ top: '20%' }" destroyOnClose>
      <template slot="footer">
        <a-button type="primary" @click="() => this.$refs.childAudit.submit()">
          提 交
        </a-button>
      </template>
      <audit-unit v-model="id" @close="closeWindow" :auditInfo="record" ref="childAudit"></audit-unit>
    </a-modal>
  </div>
</template>

<script>
import { isEmptyParams, filterExportExcelData, tableColumnsName } from "@/views/utils/common"
import unitView from '@/views/unit/components/unitView'
import auditUnit from '@/views/unit/components/unitAudit'

export default {
  name: 'unitAudit',
  components: {
    unitView, auditUnit
  },
  data () {
    return {
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      searchForm: { unitName: '' },
      tableData: [],
      columns: [
        { title: '单位名称', dataIndex: 'unitName', align: 'center' },
        { title: '单位类型', dataIndex: 'unitTypeName', align: 'center' },
        { title: '单位地址', dataIndex: 'unitAddress', align: 'center' },
        { title: '上级单位', dataIndex: 'upUnitName', align: 'center' },
        { title: '状态', scopedSlots: { customRender: 'stateSwitch' }, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'option' }, align: 'center', fixed: 'right', width: 120, },
      ],
      pagination: { pageIndex: 1, pageSize: this.$defaultPageSize, total: 0, pageSizeOptions: this.$defaultPageSizeOptions, },
      loading: false,
      // 弹窗
      visibleView: false,
      visibleAudit: false,
      id: null,
      record: null,
    }
  },
  created () {
    this.getListByPage()
  },
  methods: {
    search () {
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    getListByPage () {//加载单位列表
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: this.pagination.pageIndex, pageSize: this.pagination.pageSize }
      this.$api.unit.getAuditListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.tableData = dataList
          this.pagination.total = total
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageIndex = current
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    closeWindow (value) {
      if (value === 'view') {
        this.id = null
        this.record = null
        this.visibleView = false
      } else if (value === 'audit') {
        this.id = null
        this.record = null
        this.visibleAudit = false
        this.getListByPage()
      }
    },
    recordClick (record, type) {
      if (type === 'view') {
        this.id = record.id
        this.visibleView = true
      } else if (type === 'audit') {
        this.id = record.id
        this.record = record
        this.visibleAudit = true
      } else if (type === 'again') {
        this.id = record.id
        this.record = record
        this.visibleAudit = true
      }
    },
    exportData () {
      this.$ToDoExcel(`单位列表`, tableColumnsName(this.columns), filterExportExcelData(this.columns, this.tableData))
    }
  }
}
</script>