<template>
  <a-card size="small" :bordered="false" class="marketingDivisionSetList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button id="marketingDivisionSetList-add" type="primary" v-if="$hasPermissions('B_marketingDivisionSet_add')" class="button-error" @click="handleEdit()">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :showPagination="false"
        :data="loadData"
        bordered>
        <!-- 市区 -->
        <template slot="cityArea" slot-scope="text, record">
          <div style="display: inline-block;margin-right: 15px;" v-for="item in record.subareaAreaList" :key="item.id">
            {{ item.areaName }}
            <span v-if="item.citySubareaAreaList">(</span>
            <span style="color: #999;margin:0 3px;" v-for="area in item.citySubareaAreaList" :key="area.id">
              {{ area.areaName }}
            </span>
            <span v-if="item.citySubareaAreaList">)</span>
          </div>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_marketingDivisionSet_edit')"
            class="button-info"
            @click="handleEdit(record)"
            id="marketingDivisionSetList-edit-btn">编辑</a-button>
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_marketingDivisionSet_del')"
            class="button-error"
            @click="handleDel(record)"
            id="marketingDivisionSetList-del-btn">删除</a-button>
          <span v-if="!$hasPermissions('B_marketingDivisionSet_edit') && !$hasPermissions('B_marketingDivisionSet_del')">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 编辑分区 -->
    <marketing-division-set-edit-modal :nowData="nowData" :openModal="openModal" :itemId="itemId" @ok="$refs.table.refresh()" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import marketingDivisionSetEditModal from './editModal.vue'
import { subareaQueryIncludeAreaAll, subareaDeleteBySn } from '@/api/subarea'
export default {
  components: { STable, VSelect, marketingDivisionSetEditModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '分区名', dataIndex: 'subareaName', width: 200, align: 'center' },
        { title: '省份/市', scopedSlots: { customRender: 'cityArea' }, align: 'left' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return subareaQueryIncludeAreaAll(parameter).then(res => {
          const data = res.data
          const no = 0
          for (var i = 0; i < data.length; i++) {
            data[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false, //  编辑  弹框
      nowData: null,
      itemId: null //  当前产品id
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.$refs.table.refresh(true)
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          subareaDeleteBySn({ sn: row.subareaSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  编辑
    handleEdit (row) {
      this.itemId = row && row.subareaSn ? row.subareaSn : null
      this.nowData = row
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = null
      this.nowData = null
      this.openModal = false
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
    })
  }
}
</script>
