<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>
        <div v-if="$route.meta.type === 'todo'" style="margin-bottom: 12px;">
          <el-button
            style="background: #32BBD6;"
            type="primary"
            @click="$refs.speList.open('force')"
          >
            个案办理
          </el-button>
          <el-button
            style="background: #FF8C00;"
            type="primary"
            @click="$refs.speList.open('soon')"
          >
            公告期即将届满名单
          </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 Doc from './doc.vue'
import { isTaskCreater } from '@/api'
import {
  xrForceDeregisterGetApprovalFormByBizSeqNo,
  xrForceDeregisterSpeList, xrForceDeregisterSpeProcessControl,
  xrForceDeregisterSpeSaveIvdAcceptNew,
  getOrgunits
} from '@/api/force'
import SpeList from '../component/SpeList.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: {
            width: '180px',
            showOverflowTooltip: true,
            formatter: (row, column, cellValue, index) => {
              return `${row.uniscId || row.regNo}`
            }
          },
          showInTable: () => {
            return this.type === 'history'
          }
        },
        {
          type: 'input',
          label: '主体名称',
          prop: 'entName',
          span: 6,
          tableProps: {
            width: '200px',
            showOverflowTooltip: true,
            formatter: (row, column, cellValue, index) => {
              return `${row.busName || row.entName}`
            }
          },
          showInTable: () => {
            return this.type === 'history'
          }
        },
        {
          type: 'input',
          label: '受理号',
          prop: 'acceptNo',
          span: 6,
          tableProps: {
            showOverflowTooltip: true
          },
          showInTable: () => {
            return this.type === 'todo'
          },
          showInForm: false
        },
        {
          type: 'input',
          label: '业务名称',
          prop: 'busName',
          span: 6,
          tableProps: {
            showOverflowTooltip: true
          },
          showInTable: () => {
            return this.type === 'todo'
          },
          showInForm: false
        },
        {
          type: 'date-picker',
          label: '公告期自',
          prop: 'noticeFrom',
          tableSort: 4,
          span: 6,
          showInForm: false
        },
        {
          type: 'date-picker',
          label: '公告期至',
          prop: 'noticeTo',
          tableSort: 4,
          span: 6,
          showInForm: false
        },
        {
          type: 'select',
          label: '法定事由',
          prop: 'forceRra',
          span: 6,
          tableProps: {
            showOverflowTooltip: true
          },
          optionsBaseCode: 'C13042'
        },
        {
          type: 'buttons',
          showInTable: false,
          hiddenLabel: true,
          label: '搜索操作',
          prop: '搜索操作',
          span: 6,
          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) => {
                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: (scope) => {
            const { isSearchForm } = scope.meta
            return isSearchForm ? 'select' : 'slot'
          },
          slotName: 'bus-status',
          label: '业务状态',
          prop: 'status',
          span: 6,
          showInForm: () => {
            return this.isExSearch
          },
          options: () => {
            return [
              { label: '待受理', value: '0' },
              { label: '待审核', value: '1' },
              { label: '待审批', value: '2' },
              { label: '已通过', value: '3' },
              { label: '未通过', value: '4' },
              { label: '已作废', value: '5' },
              { label: '转办', value: '6' }
            ]
          }
        }
      ],
      selected: []
    }
  },
  watch: {
    type(val) {
      this.$router.replace({
        path: `/forceNotice/force/${val}`
      })
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$refs.EditTable.search({ reset: true })
      vm.type = vm.$route.meta.type
    })
  },
  created() {
    this.getOrgunits()
  },
  methods: {
    goToExpiringSoonList() {
      // 跳转到当前页面，并携带参数标识为即将届满名单
      this.$router.push({
        path: `/forceNotice/force/todo`,
        query: { expiringSoon: true }
      })
    },
    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 xrForceDeregisterSpeSaveIvdAcceptNew({
              xrForceDeregisterSpe: item,
              busType: 'force'
            }).then((data) => {
              return this.$router.push({
                path: `/forceNotice/force/${this.$route.meta.type}/handle`,
                query: {
                  bizSeqNo: data.bizSeqId
                }
              })
            })
          }
        })
      } else {
        this.$message.warning(`请选择至少一个主体`)
      }
    },
    searchLoader(pageParam, parseSearchFormData, instance) {
      return xrForceDeregisterSpeList({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData,
          busType: this.$route.meta.type,
          forceType: '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/force/${this.type}/handle`,
          query: {
            bizSeqNo: row.bizSeqNo
          }
        })
      }
    },
    print(row) {
      xrForceDeregisterGetApprovalFormByBizSeqNo({
        bizSeqNo: row.bizSeqNo,
        forceSpeId: row.forceSpeId
      }).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 xrForceDeregisterSpeProcessControl({
                xrForceDeregisterSpe: row, // 传递选中行的数据作为 xrForceDeregisterSpe 参数
                forceType: '0',
                bizSeqNo: row.bizSeqNo,
                nextNodeID: 'nullify',
                nextPerformerIds: [],
                opinion: {
                  opinionContent: '业务作废'
                }
              }).then((data) => {
                this.$message.success(`已完成对“${row.inv}”的业务作废！`)
                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;
}

.el-button--primary {
  margin-right: 12px;
}

</style>
