<template>
  <a-card size="small" :bordered="false" class="bulkPartsTypeList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div ref="tableSearch" class="table-operator-nobor">
        <a-button v-if="$hasPermissions('B_basicData_bulkPartsType_add')" id="bulkPartsTypeList-add" type="primary" 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"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <!-- 系统内置类型不可编辑和删除  tenantId为0是内置 -->
          <a-button
            v-if="$hasPermissions('B_basicData_bulkPartsType_edit') && record.tenantId!=0"
            size="small"
            type="link"
            class="button-info"
            @click="handleEdit(record)"
            id="bulkPartsTypeList-edit-btn">编辑</a-button>
          <a-button
            v-if="$hasPermissions('B_basicData_bulkPartsType_del') && record.tenantId!=0"
            size="small"
            type="link"
            class="button-error"
            @click="handleDel(record)"
            id="bulkPartsTypeList-del-btn"
            style="margin: 0 0 0 8px;">删除</a-button>
          <span v-if="!($hasPermissions('B_basicData_bulkPartsType_edit') && record.tenantId!=0) && !($hasPermissions('B_basicData_bulkPartsType_del') && record.tenantId!=0)">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑散件类型 -->
    <bulkParts-type-edit-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { sparePartsPutTypeList, sparePartsPutTypeDel } from '@/api/sparePartsPutType'
import { STable, VSelect } from '@/components'
import bulkPartsTypeEditModal from './editModal.vue'
export default {
  name: 'BulkPartsTypeList',
  components: { STable, VSelect, bulkPartsTypeEditModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        name: '' //  散件类型名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '10%', align: 'center' },
        { title: '散件类型名称', dataIndex: 'name', width: '35%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '创建时间', dataIndex: 'createDate', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '25%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return sparePartsPutTypeList(Object.assign(parameter, this.queryParam)).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.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, //  新增编辑散件类型  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row ? row.id : null
      this.nowData = row || null
      this.openModal = true
    },
    //  删除
    handleDel (row) {
      const _this = this
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          sparePartsPutTypeDel({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      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 - 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()
    }
  }
}
</script>
