<template>
  <div class="page">
    <easy-form
      ref="EasyForm"
      v-model="data"
      :fields="fields"
      :form-config="formConfig"
      :buttons="buttons"
    >
      <template v-slot:divider="scope">
        <div class="divider">
          <span class="text">{{ scope.getPropValue(scope.field.label) }}</span>
        </div>
      </template>
      <template v-slot:entListTable>
        <easy-table
          ref="EntListTable"
          v-model="entListTableData"
          :fields="entListFields"
          :table-config="entListTableConfig"
          :emit-load-on-create="true"
          :on-load="loadEntListData"
        >
          <template v-slot:entNameSlot="scope">
            <a class="ent-name-link" @click="goToEntDetail(scope.row)">{{ scope.row.entname || '' }}</a>
          </template>
        </easy-table>
      </template>
    </easy-form>
  </div>
</template>

<script>
import { createGlobalLoading, parseTime2 } from '@/utils'
import {
  tsbizrevlistGetPenaltyCaseInfoPage,
  tsbizrevlistUnrevokedList,
  tsbizrevlistDirectRevoke
} from '@/api/auto-baseapi'
import { getOrgTreeList } from '@/api/system'

export default {
  data() {
    return {
      parseTime2,
      data: {},
      caseId: '',
      orgTreeList: [],
      entListTableData: [],
      entListFields: [
        {
          type: 'input',
          label: '主体名称',
          prop: 'entname',
          tableProps: {
            showOverflowTooltip: true,
            width: '300px'
          }
        },
        {
          type: 'input',
          label: '企业类型',
          prop: 'enttype',
          options: [
            { label: '个体户', value: '9910' },
            { label: '个人独资企业', value: '4540' },
            { label: '有限责任公司(自然人投资或控股)', value: '1130' },
            { label: '有限责任公司(自然人投资或控股的法人独资)', value: '2153' },
            { label: '有限责任公司(自然人独资)', value: '1151' }
          ],
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '注册号',
          prop: 'regno',
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '登记机关',
          prop: 'regorg',
          optionsBaseCode: 'getDataType-registration',
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '统一社会信用代码',
          prop: 'uniscid',
          tableProps: {
            showOverflowTooltip: true,
            width: '220px'
          }
        }
      ],
      entListTableConfig: {
        selection: false,
        showIndex: true,
        indexWidth: '100px',
        showPagination: false,
        tableProps: {
          headerCellStyle: { background: '#F5F5F5!important', color: '#333333!important', padding: '0px' },
          headerRowStyle: { height: '48px' },
          rowStyle: { height: '48px' },
          cellStyle: { padding: '16px 0', color: '#333' }
        }
      },
      formConfig: {
        labelPosition: 'right',
        buttonPosition: 'right',
        gutter: 0,
        isView: () => {
          return true
        }
      },
      fields: [
        {
          type: 'divider',
          label: '基本信息'
        },
        {
          type: 'input',
          label: '接收数据时的对接记录ID',
          prop: 'acceptid',
          span: 12
        },
        {
          type: 'input',
          label: '案件编号',
          prop: 'caseid',
          span: 12
        },
        {
          type: 'input',
          label: '案件名称',
          prop: 'casename',
          span: 24
        },
        {
          type: 'textarea',
          label: '案由',
          prop: 'caseintroduction',
          span: 24
        },
        {
          type: 'textarea',
          label: '违法行为',
          prop: 'illegality',
          span: 24
        },
        {
          type: 'textarea',
          label: '处罚内容',
          prop: 'penalty',
          span: 24
        },
        {
          type: 'date-picker',
          label: '处罚决定日期',
          prop: 'penaltydate',
          span: 12
        },
        {
          type: 'cascader',
          label: '处罚决定机关',
          prop: 'penaltyorg',
          cnProp: 'penaltyorgCn',
          span: 12,
          formProps: {
            props: { checkStrictly: true, value: 'id', label: 'label', children: 'children' },
            'show-all-levels': false,
            clearable: true,
            filterable: true
          },
          options: () => this.orgTreeList
        },
        {
          type: 'date-picker',
          label: '创建时间',
          prop: 'createTime',
          span: 12
        },
        {
          type: 'date-picker',
          label: '更新时间',
          prop: 'updateTime',
          span: 12
        },
        {
          type: 'divider',
          label: '主体列表'
        },
        {
          type: 'slot',
          slotName: 'entListTable',
          label: '主体列表',
          hiddenLabel: true,
          prop: 'entList',
          span: 24
        }
      ],
      buttons: [
        {
          label: '返回',
          type: 'primary',
          show: true,
          buttonProps: {
            plain: true
          },
          handler: (scope) => {
            this.$router.back()
          }
        },
        {
          label: '提交',
          type: 'primary',
          show: () => {
            return this.$route.query.view !== '1'
          },
          handler: () => {
            if (!this.entListTableData || this.entListTableData.length === 0) {
              this.$message.warning('主体列表不能为空')
              return
            }
            this.$confirm('确定要进行批量吊销操作吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              const pripids = this.entListTableData.map(item => item.pripid)
              return tsbizrevlistDirectRevoke({
                pripids
              }).then(res => {
                if (res.code === 0) {
                  this.$message.success('操作成功')
                  this.$router.back()
                }
              })
            })
          }
        }
      ]
    }
  },
  created() {
    this.caseId = this.$route.query.caseid
    getOrgTreeList().then(res => {
      if (res.code === 0) {
        this.orgTreeList = res.data || []
      }
    })
    createGlobalLoading(() => {
      return Promise.all([
        tsbizrevlistGetPenaltyCaseInfoPage({
          current: 1,
          size: 1,
          entity: { caseid: this.caseId }
        }).then((res) => {
          if (res.data && res.data.records && res.data.records.length > 0) {
            this.data = {
              ...this.data,
              ...res.data.records[0]
            }
          }
        })
      ])
    })
  },
  methods: {
    goToEntDetail(row) {
      this.$router.push({ path: '/comprehensive/details', query: { pripid: row.pripid }})
    },
    loadEntListData() {
      return tsbizrevlistUnrevokedList({
        entity: { caseid: this.caseId }
      }).then((res) => {
        const records = res.data?.records || []
        return {
          tableData: records,
          total: records.length
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/abnormal-handle";
.ent-name-link {
  color: #1A77FF;
  cursor: pointer;
  &:hover {
    text-decoration: underline;
  }
}
</style>
