<template>
  <div class="shelfSet-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="shelfSet-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span class="store-info">{{ (basicInfoData&&basicInfoData.shelfName) || '--' }}</span>
        </template>
      </a-page-header>
      <!-- 内容 -->
      <a-card size="small" :bordered="false" class="shelfSet-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1">
            <template slot="header">
              <span>基础信息</span>
              <a-button icon="edit" size="small" type="link" style="margin-left: 20px;color: #39f;" @click.stop="handleInfoEdit">编辑</a-button>
            </template>
            <a-descriptions :column="3">
              <a-descriptions-item label="关联客户">{{ (basicInfoData&&basicInfoData.customerEntity&&basicInfoData.customerEntity.customerName) || '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="shelfSet-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="shelfSet-productCode" v-model.trim="queryParam.shelfProductApiEntity.productCode" allowClear placeholder="请输入产品编码"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="产品名称">
                  <a-input id="shelfSet-productName" v-model.trim="queryParam.shelfProductApiEntity.productName" allowClear placeholder="请输入产品名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="shelfSet-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm()" :disabled="disabled" id="shelfSet-reset">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮 -->
        <div class="table-operator" v-if="basicInfoData&&basicInfoData.finishFlag=='0'">
          <a-button id="shelfSet-import" type="primary" class="button-error" @click="openGuideModal=true">导入绑定产品</a-button>
          <a-tooltip placement="top">
            <template slot="title">
              <span>如果货位还没有绑定产品，可使用此功能通过Excel批量导入</span>
            </template>
            <a-icon type="question-circle" theme="filled" style="color: rgba(0,0,0,.45);font-size: 16px;margin-left: 5px;vertical-align: middle;cursor: pointer;" />
          </a-tooltip>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :showPagination="false"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-primary"
              v-if="!!(record.shelfProductApiEntity&&record.shelfProductApiEntity.productSn)"
              @click="handleProduct(record, 'edit')"
              id="shelfMonitoringList-edit-btn">修改信息</a-button>
            <a-button
              size="small"
              type="link"
              class="button-primary"
              v-if="!!(record.shelfProductApiEntity&&record.shelfProductApiEntity.productSn)"
              @click="handleReplace(record)"
              id="shelfMonitoringList-replace-btn">更换产品</a-button>
            <a-button
              size="small"
              type="link"
              class="button-primary"
              v-if="!(record.shelfProductApiEntity&&record.shelfProductApiEntity.productSn)"
              @click="handleProduct(record, 'bind')"
              id="shelfMonitoringList-bind-btn">绑定产品</a-button>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 修改信息/绑定产品/更换产品 -->
    <bind-product-modal :openModal="openModal" :type="modalType" :nowData="nowData" @ok="$refs.table.refresh()" @close="handleCancel" />
    <!-- 更换产品 -->
    <common-modal
      :openModal="openTipsModal"
      modalHtml="只有当前库存为0时，才能更换产品<br/>您可以在“货架监控”功能中将产品调回"
      okText="好的"
      :isCancel="false"
      @ok="openTipsModal=false"
      @close="openTipsModal=false" />
    <!-- 导入产品 -->
    <importGuideModal :openModal="openGuideModal" :params="{shelfSn: $route.params.sn}" @close="openGuideModal=false" @ok="handleGuideOk" />
    <!-- 基础设置 -->
    <basic-info-modal :openModal="openInfoModal" :nowData="basicInfoData" @ok="handleInfoOk" @close="openInfoModal=false" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import commonModal from '@/views/common/commonModal.vue'
import bindProductModal from './bindProductModal.vue'
import ImportGuideModal from './importGuideModal.vue'
import basicInfoModal from './basicInfoModal.vue'
import { shelfDetail, shelfProductList, shelfProductBatchInsert } from '@/api/shelf'
export default {
  name: 'ShelfMonitoringWarehousing',
  components: { STable, VSelect, commonModal, bindProductModal, ImportGuideModal, basicInfoModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        shelfProductApiEntity: {
          productCode: '',
          productName: ''
        }
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '绑定产品编码', dataIndex: 'shelfProductApiEntity.productCode', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '绑定产品名称', dataIndex: 'shelfProductApiEntity.productName', width: '23%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '销售价', dataIndex: 'shelfProductApiEntity.price', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '结算价', dataIndex: 'shelfProductApiEntity.cost', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '最大库容', dataIndex: 'shelfProductApiEntity.maxQty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '当前库存', dataIndex: 'shelfProductApiEntity.qty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfProductList(Object.assign(parameter, this.queryParam, { shelfSn: this.$route.params.sn })).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      basicInfoData: null,
      openTipsModal: false,
      openModal: false,
      nowData: null,
      openGuideModal: false,
      modalType: null,
      openInfoModal: false
    }
  },
  methods: {
    // 编辑基本信息
    handleInfoEdit () {
      this.openInfoModal = true
    },
    // 基本信息
    getDetail () {
      shelfDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 更换产品
    handleReplace (row) {
      if (row.qty) {
        this.openTipsModal = true
      } else {
        this.handleProduct(row, 'replace')
      }
    },
    // 更换产品/绑定产品/修改信息
    handleProduct (row, type) {
      if(row){
        this.nowData = Object.assign(row, { customerSn: this.basicInfoData&&this.basicInfoData.customerEntity&&this.basicInfoData.customerEntity.customerSn })
      }else{
        this.nowData = null
      }
      this.modalType = type
      this.openModal = true
    },
    handleCancel () {
      this.nowData = null
      this.openModal = false
    },
    // 导入成功
    handleGuideOk (obj) {
      shelfProductBatchInsert(obj).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh(true)
        }
      })
    },
    handleInfoOk () {
      this.getDetail()
    },
    //  重置
    resetSearchForm () {
      this.queryParam.shelfProductApiEntity.productCode = ''
      this.queryParam.shelfProductApiEntity.productName = ''
      this.$refs.table.refresh(true)
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/numsGoodsShelves/shelfSet/list' })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getDetail()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getDetail()
      this.resetSearchForm()
    }
  }
}
</script>

<style lang="less">
  .shelfSet-wrap{
    .store-info{
      margin: 0 15px;
      color: rgb(102, 102, 102);
    }
    .shelfSet-cont{
      margin-top: 10px;
    }
  }
</style>
