<template>
  <template-page
    ref="pageRef"
    :get-list="getList"
    :export-list="exportList"
    :operation="operation()"
    :options-evens-group="optionsEvensGroup"
    :column-parsing="columnParsing"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
  >
    <Popu v-if="visible">
      <el-page-header slot="head" :content="content" @back="handleClose" />
      <PartyaForm :detail-id="detailId" :is-detail="isDetail" @updateList="handleClose" />
    </Popu>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'

import PartyaForm from './partyaForm.vue'
import {
  getFirstPartyCustomerManagementList,
  exportFirstPartyCustomerManagement,
  delFirstPartyCustomerManagement
} from '@/api/basic_data/partya'

export default {
  components: { TemplatePage, Popu, PartyaForm },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.content = '新增'
                this.visible = true
              })
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async() => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                const ids = this.recordSelected.map(v => {
                  return v.id
                })
                const idList = ids.toString()
                await delFirstPartyCustomerManagement({ idList })
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              }
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      detailId: '',
      content: '',
      isDetail: false

    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getFirstPartyCustomerManagementList(...p)
    },
    // 列表导出函数
    exportList: exportFirstPartyCustomerManagement,
    // 表格列解析渲染数据更改
    columnParsing(_item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (_h, { row, index, column }) => {
        return (
          <div class='operation-btns'>
            <el-button
              size='mini'
              type='text'
              onClick={() => {
                this.content = '编辑'
                this.visible = true
                this.detailId = row.id
              }}
            >
              编辑
            </el-button>
            <el-button
              size='mini'
              type='text'
              onClick={() => {
                this.content = '详情'
                this.visible = true
                this.isDetail = true
                this.detailId = row.id
              }}
            >
              详情
            </el-button>
            {
              <el-popconfirm
                title='确定删除吗？'
                onConfirm={() => {
                  delFirstPartyCustomerManagement({ idList: row.id }).then(res => {
                    this.$refs.pageRef.refreshList()

                    this.$successMsg('删除成功')
                  })
                }}
              >
                <el-button slot='reference' type='text' size='mini'>
                  删除
                </el-button>
              </el-popconfirm>
            }
          </div>
        )
      }
    },
    handleClose() {
      this.addOff(() => {
        this.detailId = ''
        this.isDetail = false
        this.visible = false
        this.$refs.pageRef.refreshList()
      })()
    }
  }
}
</script>

<style lang="scss" scoped></style>
