<template>
  <div class="storingLocationList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="storingLocationList-back">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="storingLocationList-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <strong style="color: rgba(0, 0, 0);margin-left: 50px;">所属仓库：{{ $route.query.name || '--' }}</strong>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="storingLocationList-cont">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="仓位名称">
                  <a-input id="storingLocationList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入仓位名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="storingLocationList-refresh">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="storingLocationList-reset">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮 -->
        <div class="table-operator" style="border: 0;padding: 0 0 10px;">
          <a-button v-if="$hasPermissions('B_inventory_warehouse_storingLocation_add')" id="storingLocationList-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">
            <!-- record.sysFlag表示该仓库是否默认，为默认仓库时则不可删除不可编辑 -->
            <a-button
              size="small"
              v-if="$hasPermissions('B_inventory_warehouse_storingLocation_edit') && record.sysFlag==0"
              type="link"
              class="button-info"
              @click="handleEdit(record)"
              id="storingLocationList-edit-btn">编辑</a-button>
            <a-button
              size="small"
              v-if="$hasPermissions('B_inventory_warehouse_storingLocation_del') && record.sysFlag==0"
              type="link"
              class="button-error"
              @click="handleDel(record)"
              id="storingLocationList-del-btn">删除</a-button>
            <span v-if="!$hasPermissions('B_inventory_warehouse_storingLocation_edit') && !$hasPermissions('B_inventory_warehouse_storingLocation_del')">--</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 新增/编辑仓位 -->
    <storing-location-edit-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @ok="handleOk" @close="closeModal" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { warehouseLocList, warehouseLocDel } from '@/api/warehouse'
import { STable } from '@/components'
import storingLocationEditModal from './editModal.vue'
export default {
  name: 'WarehouseLocList',
  components: { STable, storingLocationEditModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        warehouseSn: this.$route.params.sn, //  仓库sn
        name: '' //  仓位名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '14%', align: 'center' },
        { title: '仓位名称', dataIndex: 'name', width: '40%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '排序', dataIndex: 'sort', width: '16%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '30%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return warehouseLocList(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.warehouseSn = this.$route.params.sn
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      if (row) {
        this.itemId = row.id
        row.warehouseSn = this.$route.params.sn
        this.nowData = row
      } else {
        this.itemId = null
        this.nowData = { warehouseSn: this.$route.params.sn }
      }
      this.openModal = true
    },
    //  新增/编辑  成功
    handleOk () {
      this.$refs.table.refresh(true)
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          warehouseLocDel({
            id: row.id,
            warehouseSn: _this.$route.params.sn
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/inventoryManagement/warehouse/list' })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 290
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .storingLocationList-wrap{
    .storingLocationList-back{
      margin-bottom: 10px;
    }
  }
</style>
