<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="address"
        label="快递单号">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间">
        <template v-slot="{row}">
          {{ row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="状态">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template v-slot="{ row, column, $index }">
          <el-button v-if="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>特殊需求</h2>
    <el-form ref="form" :model="form" label-width="180px" style="max-width: 600px;">
      <el-form-item label="套编织袋费">
        <el-input v-model="form.a" placeholder="请输入套编织袋费">
          <el-select v-model="form.b" 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>
      <el-form-item label="木架包装费">
        <el-input v-model="form.a" placeholder="请输入木架包装费">
          <el-select v-model="form.b" 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>
      <el-form-item label="纸箱包装费">
        <el-input v-model="form.a" placeholder="请输入纸箱包装费">
          <el-select v-model="form.b" 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>
      <el-form-item label="内陆运输费">
        <el-input v-model="form.a" placeholder="请输入内陆运输费">
          <el-select v-model="form.b" 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>
      <el-form-item label="打包费">
        <el-input v-model="form.a" placeholder="请输入打包费">
          <el-select v-model="form.b" 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>
      <el-form-item label="易碎标签">
        <el-input v-model="form.a" placeholder="请输入易碎标签">
          <el-select v-model="form.b" 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>
      <el-form-item label="其他费用">
        <el-input v-model="form.a" placeholder="请输入其他费用">
          <el-select v-model="form.b" 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">
        <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, 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'

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)
    }
    getCurrencyList().then(res => this.currencyList = res.data)
  },

  data() {
    return {
      areaVisible: false,
      finishVisible: false,
      locationName: '',
      warehousingVisible: false,
      form: {
        a: '',
        b: 3
      },
      currencyList:[],
      order: {},
      orderItemList: [],
      warehousing: undefined
    }
  },

  methods: {
    handleSubmit() {
      orderWarehouseInFinish({
        "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)
      }
    }
  }
}
</script>

<style scoped>

</style>
