<template>
  <a-card size="small" :bordered="false" class="marketingDivisionSetList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div ref="tableSearch" class="table-operator" style="border: 0;">
        <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 fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :showPagination="false"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        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
      v-drag
      :nowData="nowData"
      :openModal="openModal"
      :itemId="itemId"
      @ok="$refs.table.refresh()"
      @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import marketingDivisionSetEditModal from './editModal.vue'
import { subareaQueryIncludeAreaAll, subareaDeleteBySn } from '@/api/subarea'
export default {
  name: 'MarketingDivisionSetList',
  mixins: [commonMixin],
  components: { STable, VSelect, marketingDivisionSetEditModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '分区名', dataIndex: 'subareaName', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省份/市', scopedSlots: { customRender: 'cityArea' }, align: 'left', width: '55%' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return subareaQueryIncludeAreaAll(parameter).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = 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
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .marketingDivisionSetList-wrap{
    height: 100%;
  }
</style>
