<template>
  <div class="app-container">
    <h2>入仓操作-{{ order.orderNo }}</h2>

    <order-base-info :order="order"></order-base-info>

    <div style="margin-top: 20px">
      <span>储位：{{ locationName }}</span><el-button type="primary" size="mini" @click="areaVisible = true" style="margin-left: 15px">选择</el-button>
    </div>

    <h2>货物信息</h2>
    <el-table
      :data="order.orderItemVOList || []"
      style="width: 100%">
      <el-table-column
        type="index"
        width="50"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="prodTitleZh"
        label="品名">
      </el-table-column>
      <el-table-column
        label="填单货物属性">
        <template v-slot="{row}">
          品牌：{{ row.brand ? '有' : '无' }}<br>
          箱数：{{ row.num }}<br>
          体积：{{ row.volume }}m³<br>
          重量：{{ row.weight }}Kg
        </template>
      </el-table-column>
      <el-table-column
        label="入库货物属性">
        <template v-slot="{row}">
          品牌：{{ row.brand ? '有' : '无' }}<br>
          箱数：{{ row.num }}<br>
          体积：{{ row.volume }}m³<br>
          重量：{{ row.weight }}Kg
        </template>
      </el-table-column>
      <el-table-column
        prop="expressNo"
        v-slot="{row}"
        label="快递单号">
        {{ row.warehouseInInfoVO ? row.warehouseInInfoVO.expressNo : '' }}
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间">
        <template v-slot="{row}">
          {{ row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="diffType"
        v-slot="{row}"
        label="状态">
        <dict-tag :type="DICT_TYPE.WAREHOUSE_IN_STATUS" :value="row.warehouseInInfoVO.diffType" />
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template v-slot="{ row, column, $index }">
          <el-button v-if="orderItemList[$index] && orderItemList[$index].orderWarehouseInBackItemDoList && orderItemList[$index].orderWarehouseInBackItemDoList.length > 0" size="mini" type="text" @click="handleWarehousing($index)">追加</el-button>
          <el-button v-else size="mini" type="text" @click="handleWarehousing($index)">入仓</el-button>
          <el-button size="mini" type="text" @click="handleWarehousingReturn(row)">退仓</el-button>
        </template>
      </el-table-column>
    </el-table>

    <h2 v-if="orderSpecialNeeds.length > 0">特殊需求</h2>
    <el-form ref="form" :model="form" label-width="180px" style="max-width: 600px;">
      <el-form-item :label="item.label" v-for="(item, index) in orderSpecialNeeds" :key="item.value">
        <el-input v-model="form.orderSpecialNeedReceivableReqVoList[index].receivableMoney" :placeholder="'请输入' + item.label">
          <el-select v-model="form.orderSpecialNeedReceivableReqVoList[index].receivableMoneyCurrency" placeholder="请选择" slot="append" style="width: 100px">
            <el-option
              v-for="item in currencyList"
              :key="item.id"
              :label="item.titleZh"
              :value="item.id">
            </el-option>
          </el-select>
        </el-input>
      </el-form-item>

      <div style="text-align: center;margin-top: 15px">
        <el-button type="primary">转异</el-button>
        <el-button type="primary" @click="finishVisible = true">完成入仓</el-button>
      </div>
    </el-form>

    <warehouse-area-dialog :visible.sync="areaVisible" v-model="locationName"></warehouse-area-dialog>

    <edit-dialog :visible.sync="warehousingVisible" :warehousing="warehousing"></edit-dialog>

<!--  完成入仓  -->
    <el-dialog
      title="确认入仓"
      :visible.sync="finishVisible"
      width="300px">
      <div style="text-align: center">在完成入仓前，您还可以</div>
      <div style="text-align: center;padding: 15px 0">
        <el-button type="info" @click="areaVisible = true">选择储位</el-button>
        <el-button type="info">打印标签</el-button>
      </div>
      <div style="text-align: center">
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getCurrencyList} from "@/api/ecw/currency"
import {
  getOrder,
  getOrderWarehouseIn,
  getSpecialListByOrderId,
  orderWarehouseInFinish,
  rollbackApply
} from "@/api/ecw/order"
import orderBaseInfo from "@/components/OrderBaseInfo"
import WarehouseAreaDialog from '@/components/WarehouseAreaDialog'
import editDialog from '@/views/ecw/order/warehousing/components/editDialog'
import {DICT_TYPE} from "@/utils/dict"

export default {
  name: "Warehousing",

  components: {
    orderBaseInfo,
    WarehouseAreaDialog,
    editDialog
  },

  mounted() {
    if(this.$route.query.id){
      this.orderId = this.$route.query.id
      getOrder(this.orderId).then(r => this.order = r.data)
      getOrderWarehouseIn(this.orderId).then(r => this.orderItemList = r.data)
      getSpecialListByOrderId(this.orderId).then(r => this.specialList = r.data)
    }
    getCurrencyList().then(res => this.currencyList = res.data)
  },

  data() {
    return {
      DICT_TYPE,
      areaVisible: false,
      finishVisible: false,
      locationName: '',
      warehousingVisible: false,
      form: {
        orderSpecialNeedReceivableReqVoList: []
      },
      currencyList:[],
      order: {},
      orderItemList: [],
      specialList: [],
      warehousing: undefined
    }
  },

  methods: {
    handleSubmit() {
      orderWarehouseInFinish({
        orderSpecialNeedReceivableReqVoList: this.form.orderSpecialNeedReceivableReqVoList,
        "locationName": this.locationName,
        "orderId": this.order.orderId
      }).then(r => {
        if (r.data) {
          this.$confirm(
            '该订单已成功入仓，是否打印？',
            '货物已入仓',
            {
              confirmButtonClass: '是',
              cancelButtonText: '否'
            }
          ).then().catch(() => {
            // this.$store.dispatch('tagsView/delVisitedView')
            this.$message.success('入仓成功')
          })
        }
      })
    },
    handleWarehousing(index) {
      this.warehousing = {...this.order.orderItemVOList[index], ...this.orderItemList[index]}
      this.warehousingVisible = true
    },
    handleWarehousingReturn(item){
      this.$confirm(item.prodTitleZh + '退仓后不可恢复，是否确认退仓？', '确定要退仓？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(item)
        rollbackApply({
          "orderId": item.orderId,
          "orderNo": item.orderNo,
          "reason": ""
        }).then(() => {
          this.$message({
            type: 'success',
            message: '申请退仓成功!'
          });
        })

      }).catch(() => {
      });
    }
  },

  watch: {
    warehousingVisible(val) {
      if (!val){
        getOrder(this.orderId).then(r => this.order = r.data)
        getOrderWarehouseIn(this.orderId).then(r => this.orderItemList = r.data)
      }
    },
    orderSpecialNeeds(val){
      val.forEach(e => {
        this.form.orderSpecialNeedReceivableReqVoList.push(    {
          "id": e.id,
          "receivableMoney": '',
          "receivableMoneyCurrency": 3
        })
      })
    }
  },

  computed: {
    orderSpecialNeedsDict() {
      return this.$store.state.dict.dictDatas.order_special_needs
    },
    orderSpecialNeeds(){
      const result = []
      this.specialList.forEach(e => {
        const t = this.orderSpecialNeedsDict.find(f => f.value === e.advanceType)
        if(t) {
          result.push({...e, label: t.label})
        }
      })
      return result
    }
  }
}
</script>

<style scoped>

</style>
