<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :morePlan="morePlan"
    :operationColumnWidth="200"
    :operation="operation()"
  >
    <Popu v-if="visible">
      <el-page-header slot="head" content="" @back="handleClose" />
      <InsuranceContractForm
        :item="item"
        :type="showType"
        @success="
          () => {
            handleClose()
            $refs.pageRef.refreshList()
          }
        "
      />
    </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 InsuranceContractForm from './InsuranceContractForm.vue'
import operation_mixin from '@/components/template/operation_mixin.js'

import { insureList, insureListExport, insureOnOff, insureDelete } from '@/api/InsuranceManagement.js'
export default {
  components: { TemplatePage, Popu, InsuranceContractForm },
  mixins: [import_mixin, add_callback_mixin, operation_mixin],
  data() {
    return {
      morePlan: [],
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              isRole: this.$restrict('add'),

              click: this.addOn(() => {
                this.item = null
                this.showType = 0
                this.visible = true
              })
            }
          ]
        ]
      ],
      // 关闭新增弹窗
      handleClose: this.addOff(() => {
        this.visible = false
        this.item = null
        this.showType = null
      }),
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      visible: false,
      item: null,
      showType: null
    }
  },
  methods: {
    // 列表请求函数
    getList: insureList,
    // 导出
    exportList: insureListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      if (item.jname === 'status') {
        defaultData.render = (h, { row, index, column }) => {
          return (
            <div class="operation-btns">
              <el-switch
                value={row.status}
                onInput={value => (row.status = value)}
                active-value="有效"
                inactive-value="无效"
                onChange={() => {
                  insureOnOff({
                    id: row.id,
                    status: row.status === '有效' ? 'ON' : 'OFF'
                  }).then(res => {
                    this.$successMsg('状态更改成功')
                  })
                }}
              ></el-switch>
              <span style="margin-left:10px">{row.status}</span>
            </div>
          )
        }
      }
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return this.operationBtn({
        edit: {
          conditions: ({ row, index, column }) => {
            return true
          },
          click: ({ row, index, column }) => {
            this.item = row
            this.visible = true
            this.showType = 1
          }
        },
        detail: {
          conditions: ({ row, index, column }) => {
            return true
          },
          click: ({ row, index, column }) => {
            this.item = row
            this.visible = true
            this.showType = 2
          }
        },
        del: {
          conditions: ({ row, index, column }) => {
            return true
          },
          prompt: '确定删除?',
          click: ({ row, index, column }) => {
            insureDelete({
              id: row.id
            })
              .then(res => {
                this.$refs.pageRef.refreshList()
                this.$message({
                  type: 'success',
                  message: `删除成功!`
                })
              })
              .catch(err => {
                console.log(err)
              })
          }
        }
      })
    }
  }
}
</script>

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