<template>
  <div class="app-container">
    <div class="head-wrap">
      <el-form inline>
        <el-form-item label="跟踪号/转单号">
          <el-input v-model="orderNumber" />
        </el-form-item>
        <el-form-item><el-button
          type="success"
          :loading="loading"
          @click="loadOrder"
        >加载订单信息</el-button></el-form-item>
      </el-form>
    </div>
    <div class="main-wrap">
      <div class="info">
        <el-form>
          <el-form-item label="渠道">
            <el-select
              v-model="orderInfo.ChannelCode"
              placeholder="渠道名称"
              clearable
              filterable
            >
              <el-option
                v-for="item in channels"
                :key="item.PublicCode"
                :label="item.Name"
                :value="item.PublicCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户单号">
            <el-input v-model="orderInfo.ReferenceNo" />
          </el-form-item>
          <el-form-item label="订单重量">
            <el-input v-model="orderInfo.Weight" />
          </el-form-item>
          <el-form-item label="收件人">
            <el-input v-model="orderInfo.FullName" />
          </el-form-item>
          <el-form-item label="收件国家">
            <el-input v-model="orderInfo.CountryCode" />
          </el-form-item>
          <el-form-item label="收件省州">
            <el-input v-model="orderInfo.State" />
          </el-form-item>
          <el-form-item label="收件城市">
            <el-input v-model="orderInfo.City" />
          </el-form-item>
          <el-form-item label="收件地址">
            <el-input v-model="orderInfo.Street" />
          </el-form-item>
          <el-form-item label="收件电话">
            <el-input v-model="orderInfo.Phone" />
          </el-form-item>
          <el-form-item label="收件邮编">
            <el-input v-model="orderInfo.ZipCode" />
          </el-form-item>
        </el-form>
      </div>
      <div v-loading="submitting" class="result-info">
        <div class="result-title">下单结果</div>
        <div><span class="item">客户单号:</span>{{ result.CustomerOrderNo }}</div>
        <div><span class="item">跟踪号:</span>{{ result.TrackingNumber }}</div>
        <div><span class="item">转单号:</span>{{ result.TransferNumber }}</div>
        <div><span class="item">服务商单号:</span>{{ result.ServicesOrderNo }}</div>
        <div>
          <span class="item">错误信息:</span>{{ result.ErrorMessage }}{{ Message }}
        </div>
        <div v-if="result.LabelUrl" style="width: 100%; height: 500px">
          <embed :src="result.LabelUrl" style="width: 100%; height: 100%">
        </div>
        <div />
      </div>
    </div>
    <div class="goods">申报物品</div>
    <el-table :data="orderInfo.OrderGoods" border>
      <el-table-column label="操作">
        <template slot-scope="s">
          <el-button type="success" size="mini" @click="addGoods(s)">添加</el-button>
          <el-button
            v-if="canShowDeleteGoodsBtn"
            type="danger"
            size="mini"
            @click="deleteGoods(s.row)"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="产品编号" prop="ProductNo">
        <template slot-scope="s">
          <el-input v-model="s.row.ProductNo" />
        </template>
      </el-table-column>
      <el-table-column label="中文描述" prop="CnName">
        <template slot-scope="s">
          <el-input v-model="s.row.CnName" />
        </template>
      </el-table-column>
      <el-table-column label="英文描述" prop="EnName">
        <template slot-scope="s">
          <el-input v-model="s.row.EnName" />
        </template>
      </el-table-column>
      <el-table-column label="海关编码" prop="HsCode">
        <template slot-scope="s">
          <el-input v-model="s.row.HsCode" />
        </template>
      </el-table-column>
      <el-table-column label="申报价值" prop="DeclareFee">
        <template slot-scope="s">
          <el-input v-model="s.row.DeclareFee" />
        </template>
      </el-table-column>
      <el-table-column label="申报币种" prop="DeclareCurrency">
        <template slot-scope="s">
          <el-input v-model="s.row.DeclareCurrency" />
        </template>
      </el-table-column>
      <el-table-column label="数量" prop="Qty">
        <template slot-scope="s">
          <el-input v-model="s.row.Qty" />
        </template>
      </el-table-column>
      <el-table-column label="明细重量" prop="Weight">
        <template slot-scope="s">
          <el-input v-model="s.row.Weight" />
        </template>
      </el-table-column>
    </el-table>

    <div class="text-center">
      <el-button type="success" style="margin-top: 20px" @click="submit">提交</el-button>
      <el-button
        type="primary"
        style="margin-top: 20px"
        @click="addGoods"
      >添加物品</el-button>
    </div>

    <div />
  </div>
</template>

<script>
import { _getDeliverChannel } from '@/api/baseInfo'
import { _loadOrder, _createOrder } from '@/api/order'
export default {
  name: 'ChannelTest',
  data() {
    return {
      loading: false,
      submitting: false,
      channelId: null,
      channelName: null,
      orderNumber: null,
      channels: [],
      orderInfo: {ChannelCode:null, OrderGoods: [{}] },
      result: {}
    }
  },
  computed: {
    canShowDeleteGoodsBtn() {
      return this.orderInfo.OrderGoods && this.orderInfo.OrderGoods.length > 1
    }
  },
  created() {
    this.channelId = this.$route.params.Id
    this.channelName = this.$route.params.Name
    this.orderInfo.ChannelCode = this.$route.params.Code
    _getDeliverChannel().then((response) => {
      this.channels = response
    })
  },
  methods: {
    deleteGoods(goods) {
      if (!this.orderInfo || !this.orderInfo.OrderGoods) return
      const idx = this.orderInfo.OrderGoods.findIndex(i => i == goods)
      this.orderInfo.OrderGoods.splice(idx, 1)
    },
    addGoods(s) {
      console.log(s)
      if (!this.orderInfo) {
        this.orderInfo = {}
      }
      if (!this.orderInfo.OrderGoods) {
        this.orderInfo.OrderGoods = []
      }
      const goods = { ProductNo: '' }
      this.orderInfo.OrderGoods.push(goods)
    },
    submit() {
      if (!this.orderInfo) {
        this.$message({ message: '请先输入下单信息', type: 'error' })
        return
      } else if (!this.orderInfo.ChannelCode) {
        this.$message({ message: '请先选择渠道', type: 'error' })
        return
      }
      this.submitting = true
      const data = this.orderInfo
      _createOrder(data)
        .then((res) => {
          this.result = res
        })
        .finally(() => {
          this.submitting = false
        })
    },
    loadOrder() {
      const orderNumber = this.orderNumber
      const param = { orderNumber }
      this.loading = true
      _loadOrder(param)
        .then((res) => {
          this.orderInfo = res
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style>
.main-wrap {
  margin-top: 20px;
  display: flex;
}

.result-info {
  width: 50%;
  border: solid 2px #eee;
  padding: 20px;
  margin: 20px;
}
.result-title {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  margin: 20px 0;
}
.item {
  display: inline-block;
  width: 6em;
  text-align: right;
  font-weight: bold;
  margin: 10px 1em 10px 10px;
}
.info,
result-info {
  width: 50%;
}
.channel-info {
  margin: 20px 0;
  font-size: 16px;
}

.goods {
  margin: 20px 0;
  font-size: 16px;
  font-weight: bold;
}
</style>
