<template>
  <div class="page">
    <edit-table
      ref="EditTable"
      v-model="data"
      :table-prop="tableProp"
      :fields="fields"
      :show-search-form="true"
      row-key="id"
      :search-form-prop="searchFormProp"
      :form-prop="formProp"
      :search-loader="searchLoader"
      @update:selected="(value)=>{selected = value}"
    >
      <template v-slot:divider="scope">
        <div class="divider">
          <span class="text">{{ scope.getPropValue(scope.field.label) }}</span>
        </div>
      </template>
    </edit-table>
  </div>
</template>

<script>
import { expelledAnnouncementList } from '@/api/除名公告.js'
export default {
  data() {
    return {
      data: [],
      selected: [],
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          showIndex: true,
          selection: false,
          indexWidth: '80px',
          handlerWidth: '120px',
          showPagination: true,
          tableSelectionProps: {
            selectable: (row, index) => {
              return row.status === '1'
            }
          },
          tableProps: {
            headerCellStyle: { background: '#8cc3fb!important', color: '#fff!important', padding: '0px' },
            headerRowStyle: { height: '48px' },
            rowStyle: { height: '48px' },
            cellStyle: { padding: '16px 0', color: '#333' },
            rowClassName: (row) => {
              if (row.rowIndex % 2 === 1) {
                return 'warning-row'
              }
            }
          }
        }
        // buttons: [
        //   {
        //     label: '查看详情',
        //     type: 'text',
        //     show: true,
        //     handler: (scope, { row }) => {
        //       this.$refs.EditTable.editReady(row)
        //     }
        //   }
        // ]
      },
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      formProp: {
        formConfig: {
          labelWidth: '210px',
          labelPosition: 'right',
          gutter: 32,
          buttonPosition: 'right'
        }
      },
      fields: [
        {
          type: 'input',
          label: '业务号',
          prop: 'auditNo',
          span: 6,
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          },
          showInTable: false
        },
        {
          type: 'input',
          label: '市场主体名称',
          prop: 'entName',
          span: 6,
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          },
          showInTable: false
        },
        {
          type: 'input',
          label: '统一社会信用代码',
          prop: 'uscc',
          span: 6,
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          },
          showInTable: false
        },
        {
          type: 'buttons',
          showInTable: false,
          showInForm: ({ meta }) => {
            return meta.isSearchForm === true
          },
          hiddenLabel: true,
          label: '搜索操作',
          prop: '搜索操作',
          span: 6,
          formItemProp: {
            labelWidth: '0px'
          },
          buttons: [
            {
              label: '查询',
              type: 'primary',
              handler: (scope) => {
                const { meta } = scope
                const { _editTable } = meta
                const { editTableInstance } = _editTable
                editTableInstance.search()
              }
            },
            {
              label: '重置',
              type: 'primary',
              buttonProps: {
                plain: true
              },
              handler: (scope) => {
                const { meta } = scope
                const { _editTable } = meta
                const { editTableInstance } = _editTable
                editTableInstance.search({
                  reset: true,
                  resetData: {
                    status: '1'
                  }
                })
              }
            }
          ]
        },
        {
          type: 'input',
          label: '业务号',
          prop: 'auditNo',
          showInForm: false
        },
        // {
        //   type: 'input',
        //   label: '除名状态',
        //   prop: 'isRemove',
        //   showInForm: false
        // },
        {
          type: 'input',
          label: '决定意见',
          prop: 'auditOpinion',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '告知书编号',
          prop: 'noticeNo',
          showInForm: false
        },
        {
          type: 'input',
          label: '市场主体名称',
          prop: 'entName',
          showInForm: false
        },
        {
          type: 'input',
          label: '统一社会信用代码',
          prop: 'uscc',
          showInForm: false
        },
        {
          type: 'input',
          label: '法定代表人（负责人、经营者）',
          prop: 'lerepName',
          showInForm: false
        }
      ]
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      // 使用 $nextTick 确保组件完全渲染后再调用
      vm.$nextTick(() => {
        if (vm.$refs.EditTable) {
          vm.$refs.EditTable.search({ reset: true })
        }
      })
    })
  },
  methods: {
    searchLoader(pageParam, parseSearchFormData, instance) {
      return expelledAnnouncementList({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData,
          bizType: '2',
          states: ['10', '11', '12', '13', '91', '92', '93']
        }
      }).then((data) => {
        // 确保每条记录都有必要的属性
        const records = data.data.records.map(record => ({
          ...record,
          _isAdd: record._isAdd || false
        }))
        console.log(records)
        return {
          tableData: records,
          total: data.data.total
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/abnormal-list.scss";
.page {
  background: #Fff;
  margin: 20px;
  padding: 20px;
}
</style>
