<template>
  <a-card size="small" :bordered="false" class="erpAllotSettings-wrap">
    <!-- 操作按钮 -->
    <div ref="tableSearch" class="table-operator-nobor">
      <a-button
        id="transferTypeManagementList-add"
        type="primary"
        v-if="$hasPermissions('B_transferTypeManagement_add')"
        @click="handleEdit()">新增一级类型</a-button>
    </div>
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        id="erpAllotSettings-table"
        ref="table"
        :style="{ height: tableHeight+60+'px'}"
        size="small"
        :rowKey="(record) => record.allocateTypeSn"
        rowName="allocateTypeSn"
        :columns="columns"
        :showPagination="false"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        childrenColumnName="sonList"
        bordered>
        <template slot="titles" slot-scope="text, record">
          {{ record.name }}
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-info"
            @click="handleEdit(record)"
            v-if="$hasPermissions('B_transferTypeManagement_edit')"
            :id="'erpAllotSet-edit-btn-'+record.id">编辑</a-button>
          <a-button
            size="small"
            type="link"
            class="button-success"
            @click="handleEdit({root:1,allocateCategory:record.allocateTypeSn,treeLevel:record.treeLevel+1,superior:{name:record.name,parentSn:record.parentSn}})"
            v-if="record.treeLevel != 3&&$hasPermissions('B_transferTypeManagement_add')"
            :id="'erpAllotSet-add-btn-'+record.id">新增子级</a-button>
          <!-- <a-button
            size="small"
            type="link"
            class="button-error"
            @click="handleDel(record)"
            v-if="record.root==1 && $hasPermissions('B_transferTypeManagement_del')"
            :id="'erpAllotSet-del-btn-'+record.id">删除</a-button> -->
        </template>
        <template slot="createDate" slot-scope="text, record">
          {{ record.updateDate }}
        </template>
        <template slot="priceType" slot-scope="text, record">
          {{ record.root==1 ? record.erpPriceTypeDictValue : '' }}
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑 -->
    <editModal :openModal="openModal" :nowData="nowData" :itemSn="itemSn" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import editModal from './editModal.vue'
// 接口
import { allocateTypeTreeList, allocateTypeSave, allocateTypeDel } from '@/api/allocateType.js'
export default {
  name: 'ErpAllotSettings',
  mixins: [commonMixin],
  components: { STable, VSelect, editModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      // 表头
      columns: [
        { title: '费用/调拨类型', scopedSlots: { customRender: 'titles' }, width: '60%', align: 'left' },
        { title: '同步ERP价格类型', scopedSlots: { customRender: 'priceType' }, width: '20%', align: 'center' },
        { title: '更新时间', scopedSlots: { customRender: 'createDate' }, width: '20%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '20%', align: 'left' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        // 获取列表数据 无分页
        return allocateTypeTreeList(parameter).then(res => {
          let data
          if (res.status == 200) {
            this.allData = res.data
            data = this.findChild(res.data)
          }
          this.spinning = false
          return data
        })
      },
      allData: [], // 所有数据
      openModal: false, //  新增编辑调拨类型  弹框
      itemSn: '', //  当前sn
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  新增/编辑
    handleEdit (row) {
      console.log(row)
      this.itemSn = row && row.allocateTypeSn ? row.allocateTypeSn : null
      this.nowData = row
      if (row) {
        this.nowData.parentName = row.treeLevel == 1 ? [] : [row.superior && row.superior.name]
        if (row.treeLevel > 2) {
          const pnode = this.allData.find(item => item.allocateTypeSn == row.superior.parentSn)
          if (pnode) {
            this.nowData.parentName.unshift(pnode.name)
          }
        }
      }
      this.openModal = true
    },
    //  删除
    handleDel (row) {
      const _this = this
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          allocateTypeDel({ sn: row.allocateTypeSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  关闭弹框
    closeModal (flag) {
      this.itemSn = ''
      this.openModal = false
      if (flag == 1) {
        this.$refs.table.refresh(true)
      }
    },
    // 查找树的子节点
    findChild (treeList) {
      treeList.forEach(item => {
        if (item.sonList && item.sonList.length > 0) {
          this.findChild(item.sonList)
        } else {
          item.root = 1
        }
      })
      return treeList
    },
    // 价格类型选择
    handleType (row, val) {
      const params = {
        allocateTypeSn: row.allocateTypeSn,
        erpPriceType: val,
        name: row.name,
        allocateCategory: row.allocateCategory,
        treeLevel: row.treeLevel
      }
      allocateTypeSave(params).then(res => {
        if (res.status == 200) {
          this.$message.success('操作成功')
        }
      })
    },
    // 初始化
    pageInit () {
      this.$nextTick(() => { // 页面渲染完成后的回调
        this.setTableH()
      })
      setTimeout(() => {
        this.$refs.table.refresh(true)
      }, 100)
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 165
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .erpAllotSettings-wrap{
    height: 100%;
  }
</style>
