<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="90vw"
    top="153px"
    custom-class="dialog-abnormal-list"
  >
    <edit-table
      v-if="visible"
      v-model="tableData"
      :table-prop="tableProp"
      :fields="fields"
      :show-search-form="true"
      row-key="pripid"
      :search-form-prop="searchFormProp"
      :search-loader="searchLoader"
      :selected.sync="selected"
    />
    <div slot="footer">
      <el-button type="primary" plain @click="visible = false">取消</el-button>
      <el-button type="primary" @click="confirm">办理</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { forceDeregisterPageByRegOrg, forceSpePageByRegOrg, pageForceResByRegOrg, getIsUserProp } from '@/api/force'
export default {
  props: {
    type: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      isExSearch: false,
      tableData: [],
      selected: [],
      params: '',
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          selection: true,
          tableSelectionProps: {
            reserveSelection: true,
            selectable: (row, index) => {
              return this.selected.length === 0 || this.selected[0] === row
            }
          },
          disableSelectAll: false,
          showIndex: true,
          indexWidth: '100px',
          showPagination: true,
          tableProps: {
            headerCellStyle: { background: '#F5F5F5!important', color: '#333333!important', padding: '0px' },
            headerRowStyle: { height: '48px' },
            rowStyle: { height: '48px' },
            cellStyle: { padding: '16px 0', color: '#333' }
          }
        }
      }
    }
  },
  computed: {
    title() {
      return '拟强制注销信息'
    },
    fields() {
      return [
        {
          type: 'input',
          label: '统一社会信用代码/注册号',
          prop: 'searchUscc',
          span: 8,
          tableProps: {
            showOverflowTooltip: true,
            width: '200px',
            formatter: (row, column, cellValue, index) => {
              return `${row.uniscId || row.regNO || ''}`
            }
          },
          showInTable: this.params !== 'force'
        },
        {
          type: 'input',
          label: ({ meta }) => {
            return meta.isSearchForm ? '主体名称（模糊）' : '主体名称'
          },
          prop: 'entName',
          span: 8,
          tableProps: {
            showOverflowTooltip: true,
            width: '280px',
            formatter: (row, column, cellValue, index) => {
              return `${row.entName || ''}`
            }
          },
          showInTable: this.params !== 'force'
        },
        {
          type: 'input',
          label: '公告内容',
          prop: 'noticeContent',
          span: 8,
          tableProps: {
            showOverflowTooltip: true,
            width: '400px',
            formatter: (row, column, cellValue, index) => {
              return `${row.noticeContent || ''}`
            }
          },
          showInTable: this.params === 'force',
          showInForm: false
        },
        {
          type: 'buttons',
          showInTable: false,
          hiddenLabel: true,
          label: '搜索操作',
          prop: '搜索操作',
          span: 8,
          formItemProp: {
            labelWidth: '0px'
          },
          buttons: [
            {
              label: (scope) => {
                return this.isExSearch ? '收起' : '展开'
              },
              rightIcon: (scope) => {
                return this.isExSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
              },
              type: 'text',
              handler: (scope) => {
                // eslint-disable-next-line vue/no-side-effects-in-computed-properties
                this.isExSearch = !this.isExSearch
              }
            },
            {
              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
                })
              }
            }
          ]
        },
        {
          type: 'select',
          label: '拟强制注销登记法定事由',
          prop: 'forceRra',
          span: 8,
          tableProps: {
            showOverflowTooltip: true,
            width: '280px'
          },
          optionsBaseCode: 'C13042',
          showInForm: () => {
            return this.isExSearch
          }
        },
        {
          type: 'date-picker',
          label: '公告期自',
          prop: 'noticeFrom',
          tableProps: {
            showOverflowTooltip: true,
            valueFormat: 'yyyy-MM-dd'
          },
          showInForm: false
        },
        {
          type: 'date-picker',
          label: '公告期至',
          prop: 'noticeTo',
          tableProps: {
            showOverflowTooltip: true,
            valueFormat: 'yyyy-MM-dd'
          },
          showInForm: false
        },
        {
          type: 'select',
          label: '拟强制注销结果',
          prop: 'forceRes',
          span: 8,
          tableProps: {
            showOverflowTooltip: true
          },
          optionsBaseCode: 'C13044',
          showInForm: false
        },
        {
          type: 'input',
          label: '公告期至剩余天数',
          prop: 'noticeToRemain',
          span: 8,
          tableProps: {
            showOverflowTooltip: true,
            width: '150px',
            formatter: (row, column, cellValue, index) => {
              // 计算剩余天数
              const noticeToDate = new Date(row.noticeTo)
              const today = new Date()
              const diffTime = noticeToDate - today
              const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
              return diffDays >= 0 ? `${diffDays} 天` : '已过期'
            }
          },
          showInForm: false // 不作为搜索条件
        }
      ]
    }
  },
  created() {
    // 判断是否是管理员
    getIsUserProp().then(res => {
      this.data = {
        ...this.data,
        userProp: res.userProp
      }
    })
  },
  methods: {
    open(params) {
      // 可以接收传递的参数
      this.params = params || {}
      this.selected = []
      this.visible = true
    },
    searchLoader(pageParam, parseSearchFormData, instance) {
      if (this.params === 'remove' || this.params === 'recovery') {
        // 直接按照拟强制注销结果的值来设置，这样后面可以直接作为参数查询，不用再转换
        const forceRemType = this.params === 'remove' ? '1' : this.params === 'recovery' ? '4' : ''
        return forceSpePageByRegOrg({
          size: pageParam.size,
          current: pageParam.current,
          entity: {
            ...parseSearchFormData,
            forceRemType: forceRemType
          }
        }).then((data) => {
          return {
            tableData: data.dataPage.records,
            total: data.dataPage.total
          }
        })
      } else if (this.params === 'liccan') {
        return pageForceResByRegOrg({
          size: pageParam.size,
          current: pageParam.current,
          entity: {
            ...parseSearchFormData
          }
        }).then((data) => {
          return {
            tableData: data.dataPage.records,
            total: data.dataPage.total
          }
        })
      } else if (this.params === 'force') {
        return forceDeregisterPageByRegOrg({
          size: pageParam.size,
          current: pageParam.current,
          entity: {
            ...parseSearchFormData,
            soon: '0'
          }
        }).then((data) => {
          return {
            tableData: data.dataPage.records,
            total: data.dataPage.total
          }
        })
      } else if (this.params === 'soon') {
        this.params = 'force'
        return forceDeregisterPageByRegOrg({
          size: pageParam.size,
          current: pageParam.current,
          entity: {
            ...parseSearchFormData,
            soon: '1'
          }
        }).then((data) => {
          return {
            tableData: data.dataPage.records,
            total: data.dataPage.total
          }
        })
      }
    },
    confirm() {
      this.$emit('confirm', this.selected)
      this.visible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.no-data-message {
  color: red;
  text-align: center; /* 可选：让文字居中 */
  font-size: 16px;     /* 可选：设置字体大小 */
  margin-top: 20px;    /* 可选：增加顶部间距 */
}
</style>
