<template>
  <div class="storingLocationList-wrap">
    <a-page-header :ghost="false" @back="handleBack" class="storingLocationList-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="storingLocationList-back-btn" href="javascript:;" @click="handleBack">返回列表</a>
      </template>
    </a-page-header>
    <a-card :bordered="false" class="storingLocationList-cont">
      <!-- 搜索条件 -->
      <div 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">
        <a-button id="storingLocationList-add" type="primary" @click="handleEdit()">新增仓位</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button type="primary" @click="handleEdit(record)" id="storingLocationList-edit-btn">编辑</a-button>
          <a-button type="danger" @click="handleDel(record)" id="storingLocationList-del-btn" style="margin-left: 15px;">删除</a-button>
        </template>
      </s-table>
    </a-card>
    <!-- 新增/编辑仓位 -->
    <storing-location-edit-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @ok="handleOk" @close="closeModal" />
  </div>
</template>

<script>
import moment from 'moment'
import { warehouseLocFindAll, warehouseFindAll, warehouseLocDel } from '@/api/warehouse'
import { STable, VSelect } from '@/components'
import storingLocationEditModal from './editModal.vue'
export default {
  components: { STable, VSelect, storingLocationEditModal },
  data () {
    return {
      queryParam: { //  查询条件
        whId: this.$route.params.id,  //  仓库id
        name: '', //  仓位名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '仓位名称', dataIndex: 'name', align: 'center', ellipsis: true },
        { title: '排序', dataIndex: 'sort', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 240, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return warehouseLocFindAll( this.queryParam ).then(res => {
          const data = res.data
          this.disabled = false
          return data
        })
      },
      openModal: false,  //  新增编辑产品品牌  弹框
      itemId: '',  //  当前品牌id
      nowData: null  //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.whId = this.$route.params.id
      this.queryParam.name = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit(row){
      if(row){
        this.itemId = row.id
        row.whId = this.$route.params.id
        this.nowData = row
      }else{
        this.itemId = null
        this.nowData = { whId: this.$route.params.id }
      }
      this.openModal = true
    },
    //  新增/编辑  成功
    handleOk(){
      this.$refs.table.refresh(true)
    },
    //  关闭弹框
    closeModal(){
      this.itemId = ''
      this.openModal = false
    },
    //  删除
    handleDel(row){
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          warehouseLocDel({
            id: row.id,
            whId: _this.$route.params.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    //  返回列表
    handleBack(){
      this.$router.push({ path: '/inventoryManagement/warehouse/list' })
    },
  }
}
</script>

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