<template>
  <div class="page">
    <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:table-top>
        <div style="margin-bottom: 16px;">
          <el-button
            type="primary"
            @click="addTask"
          >
            添加任务名单
          </el-button>
          <el-button
            type="primary"
            :disabled="selected.length === 0"
            @click="startForceNotice"
          >
            发起拟强制注销公告
          </el-button>
        </div>
      </template>
    </edit-table>
  </div>
</template>

<script>
import {
  announcementTaskDeleteId,
  announcementTaskPage,
  announcementTaskDetailListTaskListId
} from '@/api/auto-baseapi'
import { xrForceDeregisterSpeSaveIvdAcceptNew } from '@/api/force'

export default {
  data() {
    return {
      data: [],
      selected: [],
      searchFormProp: {
        formConfig: {
          labelWidth: '100px',
          labelPosition: 'right',
          gutter: 32
        }
      },
      tableProp: {
        emitLoadOnCreate: true,
        tableConfig: {
          selection: true,
          tableSelectionProps: {
            reserveSelection: true
          },
          showIndex: true,
          indexWidth: '80px',
          handlerWidth: '150px',
          showPagination: true,
          rowKey: 'id',
          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',
            handler: (scope, { row }) => {
              this.viewDetail(row)
            }
          },
          {
            label: '删除',
            type: 'text',
            handler: (scope, { row }) => {
              this.deleteTask(row)
            }
          }
        ]
      },
      fields: [
        {
          type: 'input',
          label: '任务名称',
          prop: 'taskName',
          span: 6,
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '创建单位',
          prop: 'createUnitName',
          span: 6,
          tableProps: {
            showOverflowTooltip: true
          }
        },
        {
          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: 'createByName',
          showInForm: false,
          tableProps: {
            width: '120px',
            showOverflowTooltip: true
          }
        },
        {
          type: 'input',
          label: '名单创建时间',
          prop: 'createTime',
          showInForm: false,
          tableProps: {
            width: '180px',
            showOverflowTooltip: true
          }
        }
      ]
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$refs.EditTable.search({ reset: true })
    })
  },
  methods: {
    searchLoader(pageParam, parseSearchFormData) {
      return announcementTaskPage({
        size: pageParam.size,
        current: pageParam.current,
        entity: {
          ...parseSearchFormData
        }
      }).then((res) => {
        return {
          tableData: res.data.records,
          total: res.data.total
        }
      })
    },
    addTask() {
      this.$router.push({
        path: '/forceNotice/taskList/detail',
        query: { mode: 'add' }
      })
    },
    viewDetail(row) {
      this.$router.push({
        path: '/forceNotice/taskList/detail',
        query: { id: row.id }
      })
    },
    deleteTask(row) {
      this.$confirm('是否确定删除该任务名单？', '提示', {
        type: 'warning',
        confirmHandlerPromise: () => {
          return announcementTaskDeleteId({ id: row.id }).then((res) => {
            if (res.code === 0) {
              this.$message.success('删除成功')
              this.$refs.EditTable.search()
            }
          })
        }
      })
    },
    startForceNotice() {
      if (this.selected.length === 0) {
        this.$message.warning('请至少勾选一个任务名单')
        return
      }
      this.$confirm(`已选择 ${this.selected.length} 个任务名单，发起拟强制注销公告后，当前名单无法修改，是否发起公告？`, '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmHandlerPromise: () => {
          const promises = this.selected.map(task =>
            announcementTaskDetailListTaskListId({ taskListId: task.id })
          )
          return Promise.all(promises).then((results) => {
            const allEntList = results.filter(res => res.code === 0).map(res => res.data).flat()
            if (allEntList.length === 0) {
              this.$message.warning('所选名单中没有企业，无法发起')
              return Promise.reject()
            }
            return xrForceDeregisterSpeSaveIvdAcceptNew({
              pripIdList: allEntList.map(item => item.pripid),
              busType: 'inclusion'
            }).then((data) => {
              this.$message.success('发起拟强制注销公告成功')
              return this.$router.push({
                path: '/forceNotice/inclusion/todo/handle',
                query: { bizSeqNo: data.bizSeqId }
              })
            })
          })
        }
      })
    }
  }
}
</script>

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