<template>
  <div class="page">
    <el-tabs v-model="type" type="card">
      <el-tab-pane label="恢复登记申请待办" name="todo" />
      <el-tab-pane label="恢复登记申请记录" name="history" />
    </el-tabs>
    <edit-table
      ref="EditTable"
      v-model="data"
      :table-prop="tableProp"
      :fields="fields"
      :show-search-form="true"
      row-key="id"
      :search-form-prop="searchFormProp"
      :search-loader="searchLoader"
    >
      <template v-slot:table-top="EditTable">
        <div v-if="$route.meta.type === 'todo'" style="margin-bottom: 12px;">
          <el-button
            style="background: #32BBD6;"
            type="primary"
            @click="$refs.speList.open('recovery')"
          >
            个案主体办理
          </el-button>
        </div>
      </template>
      <template v-slot:bus-status="{ row, field, formatter }">
        <span
          :class="{
            ['bus-status-'+row[field.prop]]: true
          }"
        >
          {{ formatter(row[field.prop]) }}
        </span>
      </template>
    </edit-table>
    <SpeList ref="speList" :type="$route.meta.type" @confirm="onSelectEnt" />
    <Doc ref="doc" />
  </div>
</template>

<script>
import {
  getOrgunits
} from '@/api/abnormal'
import { isTaskCreater } from '@/api'
import {
  xrForceDeregisterRemGetApprovalFormByBizSeqNo,
  xrForceDeregisterRemList,
  xrForceDeregisterRemProcessControl,
  xrForceDeregisterRemSaveIvdAcceptNew
} from '@/api/force'
import SpeList from '../component/SpeList.vue'
import Doc from './doc.vue'

export default {
  components: {
    SpeList,
    Doc
  },
  data() {
    return {
      orgs: [],
      type: this.$route.meta.type,
      authItemMap: [],
      data: [],
      isExSearch: false,
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          showIndex: true,
          indexWidth: '100px',
          handlerWidth: '120px',
          showPagination: true,
          tableProps: {
            headerCellStyle: { background: '#F5F5F5!important', color: '#333333!important', padding: '0px' },
            headerRowStyle: { height: '48px' },
            rowStyle: { height: '48px' },
            cellStyle: { padding: '16px 0', color: '#333' }
          }
        },
        buttons: [
          {
            label: '办理',
            type: 'text',
            show: () => {
              return this.type === 'todo'
            },
            handler: (scope, { row }) => {
              this.handle(row)
            }
          },
          {
            label: '查看',
            type: 'text',
            show: () => {
              return this.type === 'history'
            },
            handler: (scope, { row }) => {
              this.handle(row)
            }
          },
          {
            label: '打印文书',
            type: 'text',
            show: (scope, { row }) => {
              return this.type === 'history'
            },
            handler: (scope, { row }) => {
              this.print(row)
            }
          },
          {
            label: '作废',
            type: 'text',
            show: () => {
              return this.type === 'todo'
            },
            handler: (scope, { row }) => {
              this.cancel(row)
            }
          }
        ]
      },
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      fields: [
        {
          type: 'input',
          label: '统一社会信用代码/注册号',
          prop: 'searchUscc',
          span: 6,
          tableSort: 1,
          tableProps: {
            showOverflowTooltip: true,
            formatter: (row, column, cellValue, index) => {
              return `${row.uniscId || row.regNo}`
            }
          }
        },
        {
          type: 'input',
          label: '主体名称',
          prop: 'entName',
          span: 6,
          tableProps: {
            width: '300px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'date-picker',
          label: '发起日期',
          prop: 'applyTime',
          tableSort: 4,
          span: 6,
          showInForm: false,
          showInTable: () => {
            return this.type === 'todo'
          }
        },
        {
          type: 'date-picker',
          label: '强制注销结果日期',
          prop: 'forceResDate',
          tableSort: 4,
          span: 6,
          showInForm: false,
          showInTable: () => {
            return this.type === 'history'
          }
        },
        {
          type: (scope) => {
            const { isSearchForm } = scope.meta
            return isSearchForm ? 'select' : 'slot'
          },
          slotName: 'bus-status',
          label: '业务状态',
          prop: 'status',
          span: 6,
          showInForm: true,
          options: () => {
            return [
              { label: '待受理', value: '0' },
              { label: '待审核', value: '1' },
              { label: '待审批', value: '2' },
              { label: '已通过', value: '3' },
              { label: '未通过', value: '4' },
              { label: '已作废', value: '5' },
              { label: '转办', value: '6' }
            ]
          }
        },
        {
          type: 'buttons',
          showInTable: false,
          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
                })
              }
            }
          ]
        },
        {
          type: 'input',
          label: '决定机关',
          prop: 'judauthCn',
          showInTable: () => {
            return this.type === 'history'
          },
          showInForm: false
        }
      ],
      selected: []
    }
  },
  watch: {
    type(val) {
      this.$router.replace({
        path: `/forceNotice/recovery/${val}`
      })
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$refs.EditTable.search({ reset: true })
      vm.type = vm.$route.meta.type
    })
  },
  created() {
    this.getOrgunits()
  },
  methods: {
    getOrgunits() {
      getOrgunits().then(res => {
        this.orgs = res
      })
    },
    onSelectEnt(speList) {
      if (speList.length > 0) {
        // 获取选中的第一条记录
        const item = speList[0]
        this.$confirm('是否确认启动终止强制注销业务', '提示', {
          type: 'info',
          customClass: 'type-1',
          confirmHandlerPromise: () => {
            return xrForceDeregisterRemSaveIvdAcceptNew({
              xrForceDeregisterSpe: item,
              forceRemType: '0'
            }).then((data) => {
              return this.$router.push({
                path: `/forceNotice/recovery/${this.$route.meta.type}/handle`,
                query: {
                  bizSeqNo: data.bizSeqId
                }
              })
            })
          }
        })
      } else {
        this.$message.warning(`请选择至少一个主体`)
      }
    },
    searchLoader(pageParam, parseSearchFormData, instance) {
      return xrForceDeregisterRemList({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData,
          busType: this.$route.meta.type,
          forceRemType: '0'
        }
      }).then((data) => {
        return {
          tableData: data.records,
          total: data.total
        }
      })
    },
    handle(row) {
      if (row.status === '6') {
        this.$confirm(`该业务由${row.openName}转到本单位，转办原因：${row.transferReason}`, '提示', {
          type: 'info',
          customClass: 'type-1',
          confirmHandlerPromise: () => {
            this.$message.error('业务创建失败:未知错误')
          }
        })
      } else {
        return this.$router.push({
          path: `/forceNotice/recovery/${this.type}/handle`,
          query: {
            bizSeqNo: row.bizSeqNo
          }
        })
      }
    },
    print(row) {
      xrForceDeregisterRemGetApprovalFormByBizSeqNo({
        bizSeqNo: row.bizSeqNo,
        forceRemId: row.forceRemId
      }).then((data = {}) => {
        this.$refs.doc.open(data)
      })
    },
    cancel(row) {
      isTaskCreater({
        bizSeqNo: row.bizSeqNo
      }).then((data) => {
        if (data) {
          this.$confirm('是否确定删除该业务', '提示', {
            type: 'info',
            customClass: 'type-1',
            confirmHandlerPromise: () => {
              return xrForceDeregisterRemProcessControl({
                xrForceDeregisterRem: row,
                bizSeqNo: row.bizSeqNo,
                forceRemType: '0',
                nextNodeID: 'nullify',
                nextPerformerIds: [],
                opinion: {
                  opinionContent: '业务作废'
                }
              }).then((data) => {
                this.$message.success(`已完成对“${row.entName}”的业务作废！`)
                this.$router.go(0)
              })
            }
          })
        } else {
          this.$message.warning(`当前用户不是该业务的发起人，无法删除该业务`)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/abnormal-list";

.excel-import {
  margin: 0 auto;
  background: #F9F9F9;
  width: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 24px;

  .excel-tmp-download {
    margin-bottom: 17px;

    .el-button {
      color: #999999;
      margin-right: 24px;
    }
  }

  .filelist-info {
    width: calc(100% - 48px);
    font-size: 16px;
  }
}

.bus-status-0 {
  color: #2A8CE3;
}

.bus-status-1 {
  color: #32BBD6;
}

.bus-status-2 {
  color: #5E7AEB;
}

.bus-status-3 {
  color: #48B86E;
}

.bus-status-4 {
  color: #F4222D;
}
</style>
