<template>
  <a-card size="small" :bordered="false" class="transferTypeManagementList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <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>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+70+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :scroll="{ y: tableHeight }"
        :pageSize="30"
        :data="loadData"
        :defaultLoadData="false"
        bordered>
        <!-- 操作 -->
        <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="'transferTypeManagementList-edit-btn-'+record.id">编辑</a-button>
          <a-button
            size="small"
            type="link"
            class="button-error"
            @click="handleDel(record)"
            v-if="$hasPermissions('B_transferTypeManagement_del')"
            :id="'transferTypeManagementList-del-btn-'+record.id">删除</a-button>
          <span v-if="!$hasPermissions('B_transferTypeManagement_del') &&!$hasPermissions('B_transferTypeManagement_edit')">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑 -->
    <transfer-type-management-edit-modal :openModal="openModal" :nowData="nowData" :itemSn="itemSn" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import transferTypeManagementEditModal from './editModal.vue'
// 接口
import { allocateTypeList, allocateTypeDel } from '@/api/allocateType'
export default {
  name: 'TransferTypeManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, transferTypeManagementEditModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用类型', dataIndex: 'superior.superior.name', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调拨类别', dataIndex: 'superior.name', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调拨类型名称', dataIndex: 'name', align: 'center', width: '25%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        // 获取列表数据 有分页
        return allocateTypeList(Object.assign(parameter, {})).then(res => {
          let data
          if (res.status == 200) {
            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.spinning = false
          return data
        })
      },
      openModal: false, //  新增编辑调拨类型  弹框
      itemSn: '', //  当前sn
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  新增/编辑
    handleEdit (row) {
      this.itemSn = row ? row.allocateTypeSn : null
      this.nowData = row || null
      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 () {
      this.itemSn = ''
      this.openModal = false
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 185
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.$refs.table.refresh(true)
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.$refs.table.refresh(true)
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.$refs.table.refresh()
      this.pageInit()
    }
  }
}
</script>
