<!--
@name: 保险管理
@author: 张泽贤
@time: 2022-03-15
-->
<template>
  <div class="pages">
      <p-card  class="full-card-panel" v-if="!editType" :loading="spinning" :alt="$route.meta.title">
        <p-form-model slot="title" layout="inline" ref="formTarget" :model="form" class="plg-grid-from">
          <p-form-model-item label="收益对象类型">
            <p-select style="width: 120px" v-model="form.beneficiaryType" :options="options.BENEFICIARY_TYPE" allowClear />
          </p-form-model-item>
          <p-form-model-item label="保单号">
            <p-input v-model="form.code" placeholder="请输入保单号" />
          </p-form-model-item>
          <p-form-model-item :label="objectLabel">
            <p-input v-model="form.objectName" :placeholder="'请输入' + objectLabel" />
          </p-form-model-item>
          <p-form-model-item :wrapper-col="{ span: 24 }" style="text-align: right">
            <a-space>
              <p-button type="primary" @clickSync="onSearch" icon="search">查询</p-button>
              <p-button @click="onReset">重置</p-button>
              <p-button type="primary" @click="onSeniorSearch">高级查询</p-button>
            </a-space>
          </p-form-model-item>
        </p-form-model>

        <a-space slot="extra">
          <p-button type="primary" @click="toEdit('add')" icon="plus">新增</p-button>
          <p-button type="primary" @click="onStatus(1)" :disabled="!selectedRowKeys.length">启用</p-button>
          <p-button type="danger" @click="onStatus(0)" :disabled="!selectedRowKeys.length">停用</p-button>
          <p-button type="danger" @click="onDel" :disabled="!selectedRowKeys.length">删除</p-button>
        </a-space>
        <p-table
          ref="masterTable"
          dragle
          :id="$route.name + '_mainTable'"
          bordered
          row-key="id"
          hasRowSelectCheck
          :data-source.sync="dataSource"
          :scroll="{ x: true, y: 300 }"
          :remote-url="listRemoteUrl"
          :remoteData="listParams"
          :rowSelectType="2"
          :selectedRowKeys.sync="selectedRowKeys"
        >
          <p-table-column data-index="code" title="保单号" :width="120" />
          <p-table-column data-index="companyName" title="保险公司" :width="120" />
          <p-table-column data-index="typeName" title="保险类型" :width="120" />
          <p-table-column data-index="policyholder" title="投保人" :width="120" />
          <p-table-column data-index="insuredDate" title="投保日期" :width="120" />
          <p-table-column data-index="beneficiaryTypeName" title="受益对象类型" :width="120" />
          <p-table-column data-index="objectCode" title="对象编码" :width="120" />
          <p-table-column data-index="objectName" title="对象名称" :width="120" />
          <p-table-column data-index="startTime" title="保险期限始" :width="120" />
          <p-table-column data-index="endTime" title="保险期限止" :width="120" />
          <p-table-column data-index="statusName" title="状态" :width="120" />
          <p-table-column data-index="memo" title="备注" :width="120" />
          <p-table-column data-index="createperson" title="创建人" :width="120" />
          <p-table-column data-index="createtime" title="创建时间" :width="120" />
          <p-table-column data-index="updateperson" title="修改人" :width="120" />
          <p-table-column data-index="updatetime" title="修改时间" :width="120" />
          <p-table-column key="action" title="操作" :width="180" fixed="right">
            <template slot-scope="text, record">
              <a-space>
                <a @click.stop="toEdit('view', record)">查看</a>
                <a @click.stop="toEdit('edit', record)">修改</a>
                <p-switch checked-children="启用" un-checked-children="停用" @change="changeSwitch(record)" :checked="record.status === 1 ? true : false" />
                <a @click.stop="toDel(record)">删除</a>
              </a-space>
            </template>
          </p-table-column>
        </p-table>
      </p-card>

    <edit v-else :editId="editId" :type="editType" :options="options" @back="editType = ''"></edit>
  </div>
</template>

<script>
import { WhetherToEnable, listRemoteUrl, Delete } from './api'
import edit from './edit'
import { initDictionary } from '@/blocks/utils'

export default {
  components: { edit },
  computed: {
    objectLabel() {
      const dictionary = {
        '': '对象名称',
        1: '车牌号',
        2: '司机姓名',
        3: '送货员姓名',
      }
      return dictionary[this.form.beneficiaryType] || '对象名称'
    },
  },
  data() {
    return {
      spinning: false,
      editType: '', // 空-列表页 add-新增 view-查看
      editData: {},
      form: {
        beneficiaryType: '',
        code: '',
        objectName: '',
      },
      // 主表格
      dataSource: [],
      selectedRowKeys: [],
      listRemoteUrl,
      listParams: {
        contractNo: '',
        contractName: '',
        fromDate: '',
        endDate: '',
      },
      options: {
        BENEFICIARY_TYPE: [],
      },
    }
  },
  mounted() {
    initDictionary([{ objectcode: 'BENEFICIARY_TYPE' }, { objectcode: 'INSURANCE_TYPE' }, { objectcode: 'INSURANCE_COMPANY' }]).then((res) => {
      this.options.BENEFICIARY_TYPE = res.BENEFICIARY_TYPE.map((item) => {
        item.value = +item.value
        return item
      })
      this.options.INSURANCE_TYPE = res.INSURANCE_TYPE.map((item) => {
        item.value = +item.value
        return item
      })
      this.options.INSURANCE_COMPANY = res.INSURANCE_COMPANY.map((item) => {
        item.value = +item.value
        return item
      })
    })
  },
  methods: {
    toEdit(type, editData = { id: '' }) {
      this.editType = type
      this.editId = editData.id
    },
    onSearch() {
      this.detailDataSource = []
      this.detailSelectedRowKeys = []
      this.listParams = { ...this.form }
    },
    onSeniorSearch() {},
    onReset() {
      this.$refs.formTarget.resetFields()
      this.form = {
        beneficiaryType: '',
        code: '',
        objectName: '',
      }
      this.onSearch()
    },
    onStatus(status) {
      const ids = this.selectedRowKeys
      if (!ids.length) {
        this.$message.error('请选择数据')
        return false
      }
      this.WhetherToEnable(status, ids)
    },
    changeSwitch(record) {
      this.WhetherToEnable(record.status === 1 ? 0 : 1, [record.id])
    },
    WhetherToEnable(status, ids) {
      let self = this
      let text = status ? '是否启用?' : '是否停用?'
      this.$confirm({
        title: text,
        onOk() {
          return new Promise((resolve, reject) => {
            WhetherToEnable({ status, ids })
              .then((res) => {
                if (res.success) {
                  self.$message.success(res.message)
                  self.onSearch()
                  resolve()
                }
              })
              .catch(() => {
                self.$message.error(res.message)
                resolve()
              })
          })
        },
      })
    },
    onDel() {
      const ids = this.selectedRowKeys
      if (!ids.length) {
        this.$message.error('请选择数据')
        return false
      }
      this.Delete(ids)
    },
    toDel(record) {
      this.Delete([record.id])
    },
    Delete(ids) {
      let self = this
      this.$confirm({
        title: '是否删除?',
        onOk() {
          return new Promise((resolve, reject) => {
            Delete({ ids })
              .then((res) => {
                if (res.success) {
                  self.$message.success(res.message)
                  self.onSearch()
                  resolve()
                }
              })
              .catch(() => {
                self.$message.error(res.message)
                resolve()
              })
          })
        },
      })
    },
  },
}
</script>
