<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>
        </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="openInfoModal = true">编辑</a-button>
            </template>
            <a-descriptions :column="3">
              <a-descriptions-item label="货架名称">{{ basicInfoData&&basicInfoData.shelfName || '--' }}</a-descriptions-item>
              <a-descriptions-item>
                <template slot="label">
                  <a-tooltip placement="top">
                    <template slot="title">
                      <span>此数字货架，归属于您的哪一位客户。如果没有搜索到客户，请在客户管理功能中新建客户。</span>
                    </template>
                    关联客户<a-icon type="question-circle" style="color: rgba(0,0,0,.65);font-size: 16px;margin-left: 2px;vertical-align: sub;cursor: pointer;" />
                  </a-tooltip>
                </template>
                {{ (basicInfoData&&basicInfoData.customerEntity&&basicInfoData.customerEntity.customerName) || '--' }}
              </a-descriptions-item>
              <!-- <a-descriptions-item>
                <template slot="label">
                  <a-tooltip placement="top">
                    <template slot="title">
                      1、非铺货产品，是指不是货架上的产品<br>
                      2、终端会员价，对于汽修厂来说，即进货价<br>
                      3、如果该产品在销售单里有销售记录，则使用最近一次销售价，如果没有销售记录，则使用箭冠总公司的终端会员价
                    </template>
                    价格显示<a-icon type="question-circle" style="color: rgba(0,0,0,.65);font-size: 16px;margin-left: 2px;vertical-align: sub;cursor: pointer;" />
                  </a-tooltip>
                </template>
                非铺货产品——{{ showPrice == '1' ? '显示': '不显示' }}价格
              </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">
          <a-button id="shelfSet-addHW" type="primary" class="button-error" @click="addHW(1)">新增货位</a-button>
          <a-button id="shelfSet-impoort" type="primary" class="button-info" @click="addHW(0)">批量导入货位</a-button>
          <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, 'bind')"
            >绑定产品</a-button>
            <a-button
              size="small"
              type="link"
              class="button-primary"
              v-else
              @click="handleReplace(record)"
            >更换产品</a-button>
            <a-button
              size="small"
              type="link"
              class="button-primary"
              @click="editHW(record)"
            >编辑</a-button>
            <a-button
              size="small"
              type="link"
              class="button-error"
              v-if="!(record.shelfProductApiEntity&&record.shelfProductApiEntity.productSn)"
              @click="handleDel(record)"
            >删除</a-button>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 基础设置 -->
    <basic-info-modal :openModal="openInfoModal" :nowData="basicInfoData" @ok="handleInfoOk" @close="openInfoModal=false" />
    <!-- 修改信息/绑定产品/更换产品 -->
    <bind-product-modal :openModal="openModal" :type="modalType" :nowData="nowData" @ok="$refs.table.refresh()" @close="handleCancel" />
    <!-- 编辑、新增货位 -->
    <addHwModal :openModal="openHwModal" :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" />
    <!-- 导入货位模板 -->
    <importHuoweiModal :openModal="openImportModal" @close="openImportModal=false" @ok="$refs.table.refresh(true)" :shelfSn="$route.params.sn"></importHuoweiModal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import commonModal from '@/views/common/commonModal.vue'
import addHwModal from './addHwModal.vue'
import bindProductModal from './bindProductModal.vue'
import ImportGuideModal from './importGuideModal.vue'
import basicInfoModal from './basicInfoModal.vue'
import importHuoweiModal from './importHuoweiModal.vue'
// , getShelfPriceShow
import { shelfDetail, shelfProductList, shelfProductBatchInsert, delShelfPlaceSn } from '@/api/shelf'
export default {
  name: 'ShelfMonitoringWarehousing',
  components: { STable, VSelect, commonModal, bindProductModal, addHwModal, ImportGuideModal, basicInfoModal, importHuoweiModal },
  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,
      openHwModal: false,
      openImportModal: false,
      showPrice: ''
    }
  },
  methods: {
    // 获取价格显示设置
    // getShelfPriceShow () {
    //   getShelfPriceShow({ shelfSn: this.$route.params.sn }).then(res => {
    //     if (res.status == 200) {
    //       this.basicInfoData.showPrice = res.data.paramValue
    //       this.showPrice = res.data.paramValue
    //     }
    //   })
    // },
    // 导入或新增货位
    addHW (type) {
      // 导入货位
      if (type == 0) {
        this.openImportModal = true
      } else {
        // 新增货位
        this.nowData = this.basicInfoData
        this.modalType = 'add'
        this.openHwModal = true
      }
    },
    // 编辑货位
    editHW (row) {
      // 已绑定产品，不能编辑货位号，只能编辑已绑定产品的销售价、结算价、最大库容
      if (row.shelfProductApiEntity && row.shelfProductApiEntity.productSn) {
        this.handleProduct(row, 'edit')
      } else {
        // 货位没有绑定产品，仅编辑货位号
        this.nowData = Object.assign(this.basicInfoData, row)
        this.modalType = 'edit'
        this.openHwModal = true
      }
    },
    // 基本信息
    getDetail () {
      shelfDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
          // this.getShelfPriceShow()
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 删除货位
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后无法恢复，确认删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          delShelfPlaceSn({ shelfPlaceSn: row.shelfPlaceSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 更换产品
    handleReplace (row) {
      if (row.shelfProductApiEntity.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.modalType = null
      this.nowData = null
      this.openModal = false
      this.openHwModal = 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>
