<template>
  <a-card size="small" :bordered="false" class="customerTypeList-wrap">
	  <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="类型名称">
                <a-input id="customerTypeList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入客户类型名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="customerTypeList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="customerTypeList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button v-if="$hasPermissions('B_basicData_customerType_add')" id="customerTypeList-add" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 300 }"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            v-if="$hasPermissions('B_basicData_customerType_edit')"
            type="link"
            class="button-info"
            @click="handleEdit(record)"
            id="customerTypeList-edit-btn">编辑</a-button>
          <a-button
            size="small"
            v-if="$hasPermissions('B_basicData_customerType_del')"
            type="link"
            class="button-error"
            @click="handleDel(record)"
            id="customerTypeList-del-btn">删除</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑客户类型 -->
    <customer-type-edit-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import { custTypeList, custTypeDel } from '@/api/custType'
import { STable, VSelect } from '@/components'
import customerTypeEditModal from './editModal.vue'
export default {
  components: { STable, VSelect, customerTypeEditModal },
  data () {
    return {
      spinning: false,
      queryParam: { //  查询条件
        name: '' //  客户类型名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '客户类型名称', dataIndex: 'name', align: 'center', ellipsis: true },
        { title: '创建时间', dataIndex: 'createDate', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return custTypeList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false, //  新增编辑客户类型  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row ? row.id : null
      this.nowData = row || null
      this.openModal = true
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        // content: '客户类型被使用，删除后客户资料中的原有客户类型将同步被删除，确定删除吗？',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          custTypeDel({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            }else{
              _this.spinning = false
            }
          })
        }
      })
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
      this.$refs.table.refresh(true)
    }
  }
}
</script>
