<template>
  <a-card size="small" :bordered="false" class="erpAllotSettings-wrap">
    <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.id"
        :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">
          <v-select
            v-if="record.root==1"
            size="small"
            style="width:30%;"
            code="ERP_PRICE_TYPE"
            :id="'erpAllotSettings-erpPriceType'+record.id"
            v-model="record.erpPriceType"
            placeholder="请选择"
            @change="e=>handleType(record,e)"></v-select>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
// 接口
import { allocateTypeTreeList, allocateTypeSave } from '@/api/allocateType.js'
export default {
  name: 'ErpAllotSettings',
  mixins: [commonMixin],
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      // 表头
      columns: [
        { title: '费用/调拨类型', scopedSlots: { customRender: 'titles' }, width: '70%', align: 'left' },
        { title: '同步ERP价格类型', scopedSlots: { customRender: 'action' }, width: '30%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        // 获取列表数据 无分页
        return allocateTypeTreeList(parameter).then(res => {
          let data
          if (res.status == 200) {
            const newData = this.findChild(res.data)
            data = newData
          }
          this.spinning = false
          return data
        })
      },
      itemId: null // 当前行数据id
    }
  },
  methods: {
    // 查找树的子节点
    findChild (treeList) {
      treeList.forEach(item => {
        if (item.sonList && item.sonList.length > 0) {
          this.findChild(item.sonList)
        } else {
          item.root = this.$hasPermissions('B_erpAllotSettings_erpPriceType') ? 1 : 0
        }
      })
      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 () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      this.tableHeight = window.innerHeight - 180
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.$refs.table.refresh()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .erpAllotSettings-wrap{
    height: 100%;
  }
</style>
