<template>
  <div class="goodsAllocationSet-page">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="goodsAllocationSet-baseInfo" >
        <template slot="subTitle">
          <a href="javascript:;" @click="handleBack(0)"><a-icon type="left"></a-icon> 返回列表</a>
          <span style="margin: 0 15px;">{{ pageInfo &&pageInfo.providerName?pageInfo.providerName:'--' }}</span>
          <span>（调回单号：{{ pageInfo &&pageInfo.putBizNo?pageInfo.putBizNo:'--' }}）</span>
        </template>
      </a-page-header>
      <a-card :bordered="false" size="small" class="goodsAllocationSet-baseInfo">
        <a-alert type="info" style="margin:10px 0">
          <div slot="message" >
            入库总数量<strong>{{ pageInfo &&pageInfo.productTotalQty?pageInfo.productTotalQty:'--' }}</strong>，
            入库总成本￥<strong>{{ pageInfo &&pageInfo.productTotalCost?pageInfo.productTotalCost:0 }}</strong>
          </div>
        </a-alert>
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          rowKey="id"
          :showPagination="false"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="true"
          bordered>
          <template slot="action" slot-scope="text, record">
            <a-cascader
              size="small"
              @change="e => changeWarehouseCascade(e, record)"
              v-model="record.warehouseCascade"
              expand-trigger="hover"
              :allowClear="false"
              :options="warehouseCascadeData"
              :fieldNames="{ label: 'name', value: 'sn', children: 'warehouseLocationList' }"
              id="bulkWarehousingOrderEdit-choose-warehouseCascade"
              placeholder="请选择仓库仓位"
              style="width: 100%;" />
          </template>
        </s-table>
        <div style="text-align: center;margin-top: 20px;">
          <a-button size="small" class="button-primary" type="primary" @click="handleBack(1)">确认入库</a-button>
        </div>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { warehouseCascadeList } from '@/api/warehouse'
import { recallrStockDetail, recallrStockDetailList, updateSelective, confirmPutStock } from '@/api/shelfRecall.js'
export default {
  mixins: [commonMixin],
  components: { STable },
  data () {
    return {
      spinning: false,
      disabled: false,
      isEdit: false, // 货位号是否为编辑
      openModal: false, // 货位模板弹窗初始状态
      stockPutTotal: 0, // 入库总数量
      stockPutAmount: 0, // 入库总成本
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      defaultWarehouseCascade: [],
      pageInfo: null,
      queryParam: {
        beginDate: '',
        endDate: '',
        payWay: undefined
      },
      settleData: null,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '20%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '入库数量', dataIndex: 'putQty', width: '7.5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productUnit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库单价', dataIndex: 'putCost', width: '7.5%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '入库成本', dataIndex: 'putTotalCost', width: '25%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '操作', width: '15%', align: 'left', scopedSlots: { customRender: 'action' } }
      ],
      warehouseCascadeData: [], //  仓库仓位
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return recallrStockDetailList({ stockPutSn: this.$route.params.stockPutSn }).then(res => {
          let data
          if (res.status == 200) {
            console.log(res, 'liebiao')
            data = res.data
            for (let i = 0; i < data.length; i++) {
              data[i].no = i + 1
              if (this.defaultWarehouseCascade.length > 0) {
                data[i].warehouseCascade = this.defaultWarehouseCascade
                data[i].warehouseSn = this.defaultWarehouseCascade[0]
                data[i].warehouseLocationSn = this.defaultWarehouseCascade[1]
              } else {
                data[i].warehouseCascade = undefined
              }
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    handleBack (val) {
      if (val == 1) {
        const _this = this
        this.$confirm({
          title: '提示',
          content: '经销商库存将增加，确认入库吗？',
          centered: true,
          onOk () {
            _this.spinning = true
            confirmPutStock({ stockPutSn: _this.$route.params.stockPutSn }).then(res => {
              _this.$message.warning(res.message)
              _this.spinning = false
              _this.$router.push({ path: '/numsGoodsShelves/recallStockManagement/list' })
            })
          }
        })
      } else {
        this.$router.push({ path: '/numsGoodsShelves/recallStockManagement/list' })
      }
    },
    // 入库信息的查询
    getStockInfo () {
      recallrStockDetail({ stockPutSn: this.$route.params.stockPutSn }).then(res => {
        if (res.status == 200) {
          this.pageInfo = res.data
        } else {
          this.pageInfo = null
        }
      })
    },
    changeWarehouseCascade (val, opt) {
      console.log(val, opt)
      updateSelective({ id: opt.id, warehouseSn: val[0], warehouseLocationSn: val[1] }).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh()
          this.getStockInfo()
        }
      })
      // let loadData
      // if (type == 'dealerProduct') { //  全部产品
      //   loadData = this.loadDataSource[ind]
      // } else if (type == 'sparePartsPurDetail') { //  已选产品
      //   loadData = this.chooseLoadDataSource[ind]
      // }
      // if (val.length < 2) {
      //   this.$message.warning('当前仓库无仓位，请选择其他仓库')
      //   const warehouseSnBackups = loadData.warehouseSnBackups || undefined
      //   const warehouseLocationSnBackups = loadData.warehouseLocationSnBackups || undefined
      //   loadData.warehouseSn = warehouseSnBackups
      //   loadData.warehouseLocationSn = warehouseLocationSnBackups
      //   if (warehouseSnBackups || warehouseLocationSnBackups) {
      //     loadData.warehouseCascade = [warehouseSnBackups, warehouseLocationSnBackups]
      //   } else {
      //     loadData.warehouseCascade = undefined
      //   }
      // } else {
      //   loadData.warehouseSn = val[0] ? val[0] : ''
      //   loadData.warehouseLocationSn = val[1] ? val[1] : ''
      //   if (type == 'sparePartsPurDetail') { //  已选产品
      //     loadData = this.chooseLoadDataSource[ind]
      //     // this.handleAdd(loadData, 'edit')
      //   }
      // }
    },
    //  仓库仓位  级联  列表
    getWarehouseCascade () {
      const _this = this
      warehouseCascadeList({}).then(res => {
        if (res.status == 200) {
          res.data.filter(item => {
            // 过滤默认仓库
            if (item.defaultFlag == 1 && item.wasteFlag == 0) { //  defaultFlag为1,且不是废品仓
              _this.defaultWarehouseCascade[0] = item.warehouseSn
              // 过滤默认仓位
              item.warehouseLocationList.filter(subItem => {
                if (subItem.defaultFlag == 1 && subItem.wasteFlag == 0) {
                  _this.defaultWarehouseCascade[1] = subItem.warehouseLocationSn
                  _this.$refs.table.refresh(true)
                }
              })
            }
          })
          console.log(this.defaultWarehouseCascade)
          res.data.map(item => {
            item.sn = item.warehouseSn
            if (item.warehouseLocationList) {
              item.warehouseLocationList.map(subItem => {
                subItem.sn = subItem.warehouseLocationSn
              })
            }
          })
          this.warehouseCascadeData = res.data
        } else {
          this.warehouseCascadeData = []
        }
      })
    },
    // 查看详情
    handleDetail (row) {
      this.settleData = row
      this.openModal = true
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getStockInfo()
      this.getWarehouseCascade()
      // this.$refs.table.refresh()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getStockInfo()
      this.getWarehouseCascade()
      // this.$refs.table.refresh()
    }
  }
}
</script>

<style lang="less">
  .goodsAllocationSet-page{
    position: relative;
    height: 100%;
    padding-bottom: 51px;
    box-sizing: border-box;
    .goodsAllocationSet-baseInfo{
      margin-bottom: 10px;
    }
    .bzj-collapse{
      margin-top: 10px;
      .ant-collapse-content-box{
        padding: 10px !important;
      }
    }

  }
</style>
