<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"
      @update:selected="(value)=>{selected = value}"
    >
      <template v-slot:bus-status="{ row, field, formatter }">
        <span
          :class="{
            ['bus-status-'+row[field.prop]]: true
          }"
        >
          {{ formatter(row[field.prop]) }}
        </span>
      </template>
    </edit-table>

    <EntList ref="entlist" :type="$route.meta.type" @confirm="onSelectEnt" />
    <Doc ref="doc" />
  </div>
</template>

<script>
import Doc from './doc.vue'
import EntList from './component/EntList.vue'
import {
  xrForceDeregisterSpeSaveIvdAcceptNew,
  xrForceDeregisterSpeProcessControl, xrForceDeregisterGetApprovalFormByBizSeqNo, isTaskCreater, getOrgunits
} from '@/api/force'
import { tsbizrevlistGetPenaltyCaseInfoPage } from '@/api/auto-baseapi'
import { getOrgTreeList } from '@/api/system'

export default {
  components: {
    Doc,
    EntList
  },
  data() {
    return {
      orgs: [],
      type: this.$route.meta.type,
      authItemMap: [],
      data: [],
      orgTreeList: [],
      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)
            }
          }
        ]
      },
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      fields: [
        // 搜索条件字段
        {
          type: 'input',
          label: '案件名称',
          prop: 'casename',
          span: 6,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '案由',
          prop: 'caseintroduction',
          span: 6,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '违法行为',
          prop: 'illegality',
          span: 6,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'cascader',
          label: '处罚决定机关',
          prop: 'penaltyorg',
          cnProp: 'penaltyorgCn',
          span: 6,
          formProps: {
            props: { checkStrictly: true, value: 'id', label: 'label', children: 'children' },
            'show-all-levels': false,
            clearable: true,
            filterable: true
          },
          options: () => this.orgTreeList,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'date-picker',
          label: '处罚决定日期',
          prop: 'penaltydate',
          span: 6,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        },
        {
          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: 'input',
          label: '处罚内容',
          prop: 'penalty',
          span: 6,
          showInForm: false,
          tableProps: {
            width: '200px',
            showOverflowTooltip: true
          }
        },
        // 表格列字段
        {
          type: 'input',
          label: '案件编号',
          prop: 'caseid',
          span: 6,
          showInForm: false,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        }
      ],
      selected: []
    }
  },
  watch: {
    type(val) {
      this.$router.replace({
        path: `/revoke/batchRevokeForceNoticeDuplicatelist/${val}`
      })
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$refs.EditTable.search({ reset: true })
      vm.type = vm.$route.meta.type
    })
  },
  created() {
    this.getOrgunits()
    this.loadOrgTreeList()
  },
  methods: {
    loadOrgTreeList() {
      getOrgTreeList().then(res => {
        if (res.code === 0) {
          this.orgTreeList = res.data || []
        }
      })
    },
    getOrgunits() {
      getOrgunits().then(res => {
        this.orgs = res
      })
    },
    searchLoader(pageParam, parseSearchFormData, instance) {
      return tsbizrevlistGetPenaltyCaseInfoPage({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          isDeal: this.type === 'history' ? '1' : '0',
          ...parseSearchFormData
        }
      }).then((data) => {
        return {
          tableData: data.data.records,
          total: data.data.total
        }
      })
    },
    onSelectEnt(entlist) {
      if (entlist.length > 0) {
        this.$confirm('是否确认启动拟强制注销业务', '提示', {
          type: 'info',
          customClass: 'type-1',
          confirmHandlerPromise: () => {
            return xrForceDeregisterSpeSaveIvdAcceptNew({
              pripIdList: entlist.map((item) => {
                return item.pripid
              }),
              busType: 'inclusion'
            }).then((data) => {
              return this.$router.push({
                path: `/forceNotice/inclusion/${this.$route.meta.type}/handle`,
                query: {
                  bizSeqNo: data.bizSeqId
                }
              })
            })
          }
        })
      } else {
        this.$message.warning(`请选择至少一个主体`)
      }
    },
    handle(row) {
      return this.$router.push({
        path: `/revoke/batchRevokeForceNoticeDuplicate/handle`,
        query: {
          caseid: row.caseid,
          view: this.type === 'history' ? '1' : '0'
        }
      })
    },
    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,
                forceType: '1',
                bizSeqNo: row.bizSeqNo,
                nextNodeID: 'nullify',
                nextPerformerIds: [],
                opinion: {
                  opinionContent: '业务作废'
                }
              }).then((data) => {
                this.$message.success(`已完成对"${row.busName}"的业务作废！`)
                this.$router.go(0)
              })
            }
          })
        } else {
          this.$message.warning(`当前用户不是该业务的发起人，无法删除该业务`)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/abnormal-list";
.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>
