<template>
  <div class="app-container">
    <!-- 订单信息 -->
    <div class="info">
      <div class="info-title">订单信息</div>
      <div class="info-item">
        <div class="info-item-content">
          <div class="info-item-content-one">
            <div class="title">订单编号：</div>
            <div class="txt">{{ info.orderId }}</div>
          </div>
          <div class="info-item-content-one">
            <div class="title">订单类型：</div>
            <div class="txt">经销商订单</div>
          </div>
        </div>
        <div class="info-item-content">
          <div class="info-item-content-one">
            <div class="title">下单时间：</div>
            <div class="txt">{{ parseTime(info.createdTime) }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 卖家信息 -->
    <div class="info">
      <div class="info-title">买家信息</div>
      <div class="info-item">
        <div class="info-item-content">
          <div class="info-item-content-one">
            <div class="title">经销商：</div>
            <div class="txt">{{ info.channel && info.channel.name }}</div>
          </div>
          <div class="info-item-content-one">
            <div class="title">上级渠道：</div>
            <div class="txt">{{ info.channel && info.channel.parentName }}</div>
          </div>
        </div>
        <div class="info-item-content">
          <div class="info-item-content-one">
            <div class="title">收货人：</div>
            <div class="txt">{{ info.receiver || "--" }}</div>
          </div>
          <div class="info-item-content-one">
            <div class="title">联系电话：</div>
            <div class="txt">{{ info.tel || "--" }}</div>
          </div>
          <div class="info-item-content-one">
            <div class="title">收货地址：</div>
            <div class="">
              {{
                `${info.province || "--"}${info.city || "--"}${
                  info.area || "--"
                }${info.address || "--"}`
              }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 订单状态 -->
    <div class="info">
      <div class="info-title">订单状态</div>
      <div class="info-item">
        <div class="info-item-content">
          <div class="info-item-content-one">
            <div class="title">订单状态：</div>
            <div class="txt" v-if="status.value == 2">已发货</div>
            <div class="txt" v-else>{{ status.desc || "--" }}</div>
          </div>
          <div class="info-item-content-one" v-if="status.value === 2">
            <div class="title">发货时间：</div>
            <div class="txt">{{ parseTime(info.deliveryTime) || "--" }}</div>
          </div>
          <div class="info-item-content-one" v-if="status.value === 2">
            <div class="edit-express" @click="editExpress">修改</div>
          </div>
        </div>
        <div class="info-item-content">
          <el-button
            v-if="status.value === 1"
            v-hasPermi="['business:ticket:on']"
            type="primary"
            @click="toGoods()"
            >发货</el-button
          >
          <div class="info-item-content-one" v-if="status.value === 2">
            <div class="title">快递公司：</div>
            <div class="txt">
              {{ (delivery && delivery.companyName) || "--" }}
            </div>
          </div>
          <div class="info-item-content-one" v-if="status.value === 2">
            <div class="title">快递单号：</div>
            <div class="txt">{{ info.deliveryFlowId || "--" }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 商品信息 -->
    <div class="info">
      <div class="info-title">商品信息</div>
      <div class="info-table">
        <el-table :data="list">
          <el-table-column label="商品信息">
            <div slot-scope="{ row }" class="flex">
              <el-image
                style="width: 100px; height: 100px"
                :src="row.picUrl"
                :preview-src-list="[row.picUrl]"
              />
              <div class="txt">{{ row.title }}</div>
            </div>
          </el-table-column>
          <el-table-column label="单价(元)">
            <template slot-scope="{ row }">
              <div>{{ $numberFormat(row.pkgSalePrice) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="orderNum">
            <template slot-scope="{ row }">
              <div>{{ row.orderNum }}包</div>
            </template>
          </el-table-column>
          <el-table-column label="小计(元)">
            <template slot-scope="{ row }">
              <div>{{ $numberFormat(row.pkgSalePrice * row.orderNum) }}</div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="status.value === 2 || status.value === 3"
            label="附加信息"
            min-width="150"
          >
            <template slot-scope="{ row }">
              <div>
                序列号：
                <div v-for="(item, index) in row.detailList" :key="index">
                  {{ item.startSn }} 至 {{ item.endSn }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="info-amt">
        <div class="info-amt-price">
          <span>运费：</span>{{ $numberFormat(info.freightAmt) }}元
        </div>
        <div class="info-amt-price">
          <span>共计金额：</span>{{ $numberFormat(info.orderAmt) }}元
        </div>
      </div>
    </div>

    <!-- 发货 -->
    <send-goods
      :send-show="goodsShow"
      :goods-info="info"
      :channel-list="boxData"
      @close="close"
    />

    <!-- 修改快递信息 -->
    <express-edit
      v-if="editShow"
      :edit-show="editShow"
      :goods-info="info"
      @close="close"
    />
  </div>
</template>
<script>
import { orderDetail, channelList } from "@/api/business/order";
import { publicFileGetUrl } from "@/api/common";
import { accMul } from "@/utils/util";
import SendGoods from "./components/SendGoods";
import ExpressEdit from "./components/ExpressEdit";
export default {
  name: "Detail",
  components: {
    SendGoods,
    ExpressEdit,
  },
  data() {
    return {
      // 订单ID
      orderId: "",
      // 订单详情
      info: {},
      // 订单状态
      status: {},
      // 物流信息
      delivery: {},
      // 商品列表
      list: [],
      // 发货弹框显示
      goodsShow: false,
      // 盲票组列表
      boxData: [],
      // 修改快递信息弹框
      editShow: false,
    };
  },
  created() {
    this.orderId = this.$route.query.id;
    this.getDetail();
    this.getChannelList();
  },
  activated() {
    const orderId = this.$route.query.id;
    if (orderId != null && orderId != this.orderId) {
      this.orderId = orderId;
      this.getDetail();
      this.getChannelList();
    }
  },
  methods: {
    // 盲票组列表
    getChannelList(row) {
      channelList({ orderId: this.orderId }).then((res) => {
        this.boxData = res.data;
      });
    },

    // 订单详情
    getDetail() {
      orderDetail({ orderId: this.orderId }).then((res) => {
        if (res.code == 0) {
          this.info = res.data;
          this.status = JSON.parse(res.data.status);
          this.delivery = res.data.deliveryCompany;
          res.data.items.forEach((item) => {
            let picUrlArr = item.picUrl.split(",");
            item.picUrl = publicFileGetUrl + picUrlArr[0];
          });
          this.list = res.data.items;
        }
      });
    },

    // 点击发货
    toGoods() {
      this.goodsShow = true;
    },

    // 发货弹框关闭
    close() {
      this.goodsShow = false;
      this.editShow = false;
      this.getDetail();
    },

    // 修改快递信息
    editExpress() {
      this.editShow = true;
    },
  },
};
</script>
<style lang="scss" scoped>
.info {
  margin-bottom: 10px;
  &-title {
    font-size: 14px;
    margin-bottom: 10px;
    font-weight: bold;
  }
  &-item {
    padding-left: 50px;
    &-content {
      display: flex;

      &-one {
        display: flex;
        margin-bottom: 10px;

        .title {
          width: 100px;
        }

        .edit-express {
          color: #409eff;
          cursor: pointer;
        }

        .txt {
          width: 200px;
        }
      }
    }
  }
  &-amt {
    margin-top: 10px;
    &-price {
      span {
        display: inline-block;
        width: 80px;
        margin-bottom: 20px;
      }
    }
  }
}
.flex {
  display: flex;
  align-items: center;
  .txt {
    margin-left: 20px;
  }
}
</style>
