<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>
      <template v-slot:table-top="btns">
        <div style="margin-bottom: 12px;">
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </edit-table>
  </div>
</template>

<script>
import { listTodoNoticeInfo } from '@/api/除名公告'
export default {
  data() {
    return {
      data: [],
      selected: [],
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          showIndex: true,
          selection: true,
          indexWidth: '80px',
          handlerWidth: '120px',
          showPagination: true,
          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'
              }
            }
          }
        }
      },
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      formProp: {
        formConfig: {
          labelWidth: '210px',
          labelPosition: 'right',
          gutter: 32,
          buttonPosition: 'right'
        }
      },
      fields: [
        {
          type: 'input',
          label: '业务号',
          prop: 'bizNo',
          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: 'bizNo',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        },
        {
          type: 'input',
          label: '除名决定的事实',
          prop: 'truth',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '除名决定的理由',
          prop: 'reason',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '除名决定的依据',
          prop: 'law',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '除名决定的后果',
          prop: 'result',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          },
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '告知书编号',
          prop: 'noticeNo',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        },
        {
          type: 'input',
          label: '市场主体名称',
          prop: 'entName',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        },
        {
          type: 'input',
          label: '统一社会信用代码',
          prop: 'uscc',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        },
        {
          type: 'input',
          label: '法定代表人（负责人、经营者）',
          prop: 'lerepName',
          required: true,
          showInForm: ({ meta }) => {
            return meta.isForm === true
          }
        }
      ]
    }
  },
  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 listTodoNoticeInfo({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData
        }
      }).then((data) => {
        // 确保每条记录都有必要的属性
        const records = data.data.records.map(record => ({
          ...record,
          _isAdd: record._isAdd || false
        }))
        console.log(records)
        return {
          tableData: records,
          total: data.data.total
        }
      })
    },
    confirm() {
      if (!this.selected || this.selected.length === 0) {
        return this.$message.error('请至少勾选一个主体名单')
      }
      this.$emit('confirm', this.selected)
    }
  }
}
</script>

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