<script>
import { ipcRenderer } from "electron";

export default {
  mounted() {
    let that = this;
    ipcRenderer.on("getProductionNoInfo", (event, data) => {
      console.log(data, 99622);
      that.trackcodeInput(data);
    });
  },
  watch: {},
  data() {
    return {
      shipForm: { shipmentType: "1" },
      logistics: [],
      placeholderText: "",
      sendNum: 0,
      type: 1,
      isDownloadImage: false,
      isAutoSure: false,
      currentStatus: "IN_PRODUCTION",
      detail: {
        imgList: [],
      },
      dialogVisible: false,
      audios: {
        weight_warning: require(`../../../assets/audio/weight_warning.mp3`),
        weight_success: require(`../../../assets/audio/weight_success.mp3`),
        weight_repeat: require(`../../../assets/audio/weight_repeat.mp3`),
        weight_search_error: require(`../../../assets/audio/weight_search_error.mp3`),
        weight_search_success: require(`../../../assets/audio/weight_search_success.mp3`),
      },
      productInfo: {},
      TrackingNumber: "",
    };
  },
  methods: {
    changeStatus() {
      if (!this.detail || Object.keys(this.detail).length <= 1) {
        return this.$message.warning("请扫码生产单号");
      }
      this.$confirm(
        `确定${this.type === 1 ? "生产完成" : "确定发货"}？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.setData();
      });
    },
    async setData() {
      if (this.detail && this.detail.id) {
        let url =
          this.currentStatus == "IN_PRODUCTION"
            ? "pod/podProductionInfo/completeDelivery"
            : "pod/podProductionInfo/completeShipment";
        try {
          let data =
            this.currentStatus == "IN_PRODUCTION"
              ? {
                  id: this.detail.id,
                }
              : {
                  list: [this.detail],
                  ...this.shipForm,
                };
          if (data.list) {
            data.list[0].note =
              data.list[0].note && typeof data.list[0].note === "object"
                ? JSON.stringify(data.list[0].note)
                : null;
          }
          await post(url, data);
          if (this.type === 2) {
            this.sendNum = this.sendNum + 1;
          }
          this.playAudio("weight_success");
          this.detail = { imgList: [] };
        } catch (e) {
          console.error(e);
          this.detail = { imgList: [] };
          this.playAudio("weight_search_error");
        }
      }
    },
    downloadAllWe(data, designId) {
      if (!this.detail || Object.keys(this.detail).length <= 1) {
        return this.$message.warning("请扫码生产单号");
      }
      let params = {
        productionNo: this.detail.podProductionNo,
        method: "POST",
      };
      if (data === 1) {
        params = {
          ...params,
          ...{
            designId,
          },
        };
      } else if (data === 2) {
        params = {
          ...params,
          ...{
            negativeDesignId: designId,
          },
        };
      }

      downloadFile("podDesignCenter/downloadByDesignId", params)
        .then((res) => {
          const { data, fileName } = res;
          let url = window.URL.createObjectURL(data);
          // 生成一个a标签
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          // 生成时间戳
          link.download = fileName;
          link.click();
          window.URL.revokeObjectURL(url);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    playAudio(key, message) {
      let text = "";
      switch (key) {
        case "weight_search_success":
          text = "";
          break;
        case "weight_search_error":
          text = "请录入生产单号";
          break;
        case "weight_success":
          text = "";
          break;
        case "weight_repeat":
          text = "称重复录入";
          break;
        default:
          text = "请录入跟踪号或重量";
          break;
      }
      if (message || text) this.$message.warning(message || text);
      let audio = document.createElement("audio");
      console.log(key);
      audio.src = this.audios[key];
      audio.play();
    },
    async trackcodeInput(data) {
      // if (this.type === 1) {
      //   if (this.isAutoSure) {
      //     await this.setData()
      //   }
      // } else {
      //   await this.setData()
      // }

      try {
        let d = JSON.parse(JSON.stringify(data));

        if (d.note) {
          d.note = JSON.parse(d.note);
        } else {
          d.note = [];
        }
        let arr = [];
        if (!d.imageAry) {
          arr = [
            { url: d.variantImage, title: "正面", id: d.designId },
            { url: d.negativeImage, title: "反面", id: d.negativeDesignId },
          ].filter((el) => el.url);
        } else {
          arr =
            typeof d.imageAry == "string" ? JSON.parse(d.imageAry) : d.imageAry;
          arr = arr.concat([
            { url: d.negativeImage, title: "反面", id: d.negativeDesignId },
          ]);
        }
        const result = [];
        console.log(arr);
        arr = arr.filter((el) => el.url);
        arr = arr.map((el) => {
          return {
            ...el,
            ...{
              url: el.url.includes("http")
                ? el.url
                : window.location.origin + "/upload/erp" + el.url,
            },
          };
        });

        for (let i = 0; i < arr.length; i += 2) {
          result.push(arr.slice(i, i + 2));
        }
        d.imgList = result;
        console.log(d.imgList, "this.detail.imgList ");
        this.detail = d;
        // 自动下载素材
        // if (this.isDownloadImage) {
        //   this.downloadAllWe()
        // }

        this.playAudio("weight_search_success");
        // this.$nextTick(() => {
        //   if (this.$refs.trackingNumberRef) {
        //     this.$refs.trackingNumberRef.focus()
        //   }
        // })
      } catch (e) {
        console.error(e);
        this.productInfo = {};
        this.playAudio("weight_search_error");
        this.$nextTick(() => {
          if (this.$refs.trackingNumberRef) {
            this.$refs.trackingNumberRef.focus();
          }
        });
      }
    },
  },
};
</script>

<template>
  <div class="page">
    <div class="sure-btn" v-if="type === 2">
      <el-button @click="changeStatus" size="small" type="success">
        {{ type === 1 ? "生产完成" : "确定发货" }}
      </el-button>
    </div>
    <div class="detail-div">
      <div class="detail-content">
        <div class="left">
          <div class="left-images">
            <el-carousel
              v-if="detail.imgList.length > 0"
              style="height: 100%"
              :autoplay="false"
              indicator-position="none"
            >
              <el-carousel-item
                style="height: 100%"
                :key="index"
                v-for="(item, index) in detail.imgList"
              >
                <el-row :gutter="10" style="width: 100%; height: 100%">
                  <el-col
                    style="height: 100%"
                    v-for="(it, i) in item"
                    :key="i"
                    :span="12"
                  >
                    <div class="left-image">
                      <b v-show="it.title && it.url">
                        {{ it.title }}
                        <span
                          style="
                            text-decoration: underline;
                            cursor: pointer;
                            color: blue;
                          "
                          @click="
                            downloadAllWe(
                              it.title && it.title === '正面' ? 1 : 2,
                              it.id
                            )
                          "
                          v-if="it.id"
                        >
                          （DID：{{ it.id }}）
                        </span>
                      </b>
                      <img :src="it.url" alt="" />
                    </div>
                  </el-col>
                </el-row>
              </el-carousel-item>
            </el-carousel>
          </div>
        </div>
        <div class="right">
          <!--          <div-->
          <!--              class="input"-->
          <!--              v-if="!detailData || Object.keys(detailData).length === 0">-->
          <!--&lt;!&ndash;            <el-input&ndash;&gt;-->
          <!--&lt;!&ndash;                v-model="TrackingNumber"&ndash;&gt;-->
          <!--&lt;!&ndash;                :placeholder="placeholderText"&ndash;&gt;-->
          <!--&lt;!&ndash;                style="width: 660px; margin-right: 10px"&ndash;&gt;-->
          <!--&lt;!&ndash;                clearable&ndash;&gt;-->
          <!--&lt;!&ndash;                ref="trackingNumberRef"&ndash;&gt;-->
          <!--&lt;!&ndash;                size="medium"&ndash;&gt;-->
          <!--&lt;!&ndash;                @keyup.enter.native="trackcodeInput()"></el-input>&ndash;&gt;-->
          <!--            <el-button type="primary" size="medium" @click="trackcodeInput()">-->
          <!--              查询-->
          <!--            </el-button>-->
          <!--          </div>-->

          <div class="div-text">
            <div class="div-content">
              <div :title="String(detail?.userMark)" class="div-item">
                <span style="font-size: 18px">客户</span>
                <p style="color: red; font-size: 30px">
                  {{ detail?.userMark }}
                </p>
              </div>
              <div :title="String(detail?.factoryOrderNumber)" class="div-item">
                <span style="font-size: 18px">订单号</span>
                <p style="color: red; font-size: 22px">
                  {{ detail?.factoryOrderNumber }}
                </p>
              </div>
            </div>
          </div>
          <div class="div-text">
            <b>生产单信息</b>

            <div class="div-content">
              <div :title="detail.podProductionNo" class="div-item">
                <span>生产单号</span>
                <p>{{ detail.factorySubOrderNumber }}</p>
                <p
                  style="margin-left: 5px"
                  v-if="detail.podProductionNo"
                  :style="{
                    color: detail.customizedQuantity > 1 ? 'red' : '#67C23A',
                  }"
                >
                  {{ detail.customizedQuantity > 1 ? "多" : "单" }}
                </p>
              </div>
              <div :title="detail.process" class="div-item">
                <span>生产工艺</span>
                <p>{{ detail.process }}</p>
              </div>
              <div :title="detail.baseSku" class="div-item">
                <span>基版</span>
                <p>{{ detail.baseSku }}</p>
              </div>
              <div :title="detail.supplierItemNo" class="div-item">
                <span>货号</span>
                <p>{{ detail.supplierItemNo }}</p>
              </div>
              <div :title="detail.variantSku" class="div-item">
                <span>变体SKU</span>
                <p>{{ detail.variantSku }}</p>
              </div>
              <div :title="detail.num" class="div-item">
                <span>数量</span>
                <p>{{ detail.num }}</p>
              </div>
              <div :title="detail.color" class="div-item">
                <span>颜色</span>
                <p>
                  {{ detail.baseSku ? detail.baseSku.split("_")[1] : null }}
                </p>
              </div>
              <div :title="detail.size" class="div-item">
                <span>尺寸</span>
                <p>{{ detail.size }}</p>
              </div>
              <div :title="detail.shopNumber" class="div-item">
                <span>店铺单号</span>
                <p>{{ detail.shopNumber }}</p>
              </div>

              <div :title="detail.expectDeliveryTime" class="div-item">
                <span>期望交货时间</span>
                <p>{{ detail.expectDeliveryTime }}</p>
              </div>

              <div :title="detail.productionConfirmTime" class="div-item">
                <span>生产确认时间</span>
                <p>{{ detail.startStockingTime }}</p>
              </div>
            </div>
          </div>
          <div
            class="form"
            style="margin: 15px 0; background: #4168ff; border-radius: 5px"
            v-if="type === 2"
          >
            <el-form
              :model="shipForm"
              ref="form"
              label-width="80px"
              :inline="false"
              size="small"
            >
              <el-form-item style="margin-top: 10px" label="发货方式">
                <el-select
                  style="width: 384px"
                  v-model="shipForm.shipmentType"
                  value-key=""
                  placeholder="请选择"
                >
                  <el-option label="送货上门" value="1"></el-option>
                  <el-option label="快递" value="2"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="物流方式"
                v-if="shipForm.shipmentType == '2'"
              >
                <el-select
                  v-model="shipForm.shipmentWay"
                  style="width: 384px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in logistics"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="物流单号"
                v-if="shipForm.shipmentType == '2'"
              >
                <el-input
                  style="width: 384px"
                  v-model="shipForm.shipmentNumber"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="物流费用"
                v-if="shipForm.shipmentType == '2'"
              >
                <el-input
                  style="width: 384px"
                  v-model="shipForm.shipmentFreight"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-form>
          </div>

          <div
            v-if="type !== 2"
            class="div-text"
            style="
              flex: 1;
              margin-top: 30px;
              flex-shrink: 0;
              display: flex;
              flex-direction: column;
            "
          >
            <div style="height: 100%" class="div-content" v-if="detail.note">
              <b style="position: absolute; top: -12px">客户留言信息</b>
              <div
                v-for="(item, index) in detail.note"
                :key="index"
                class="div-item"
              >
                <span>{{ item.prop }}:</span>
                <p>
                  {{ item.value }}
                </p>
              </div>
            </div>
          </div>
          <div
            v-if="type === 2"
            class="div-text"
            style="
              flex: 1;
              flex-shrink: 0;
              display: flex;
              flex-direction: column;
            "
          >
            <div style="height: 100%" class="div-content">
              <b style="position: absolute; top: -12px">本次发货数量</b>
              <div
                class="div-item"
                style="
                  display: flex;
                  align-items: center;
                  width: 100%;
                  height: 100%;
                  font-size: 40px;
                  justify-content: center;
                "
              >
                <h2 style="color: red; font-weight: bold">{{ sendNum }}</h2>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
::v-deep {
  .el-dialog__body {
    box-sizing: border-box;
    flex: 1;
    flex-shrink: 0;
    overflow: hidden;
  }
  .el-dialog {
    display: flex;
    height: calc(100% - 50px);
    margin-top: 50px;
    flex-direction: column;
  }
  .el-carousel__container {
    height: 100%;
  }
  .el-form-item__label {
    font-weight: bold;
    color: white;
  }
  .el-dialog__title {
    font-weight: bold;
    font-size: 37px;
    color: black;
    position: relative;
    left: 47%;
    top: 13px;
  }
}
.sure-btn {
  position: absolute;
  right: 62px;
  top: 14px;
}
.detail-div {
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: space-between;
  .detail-images {
    .scroll-list {
      background: #ececec;
      display: flex;
      height: 100px;
      width: 100%;
      padding: 5px;
      .scroll-content {
        margin-left: 10px;
        overflow-x: auto;
        overflow-y: hidden;
        flex: 1;
        display: flex;
        flex-wrap: nowrap;
        flex-shrink: 0;
        .scroll-item {
          height: 100%;
          min-width: 100px;
          background: white;
          width: 80px;
          margin-right: 5px;
        }
      }
      .img-title {
        display: flex;
        flex-direction: column;
        justify-content: center;
        background: white;
        padding: 10px;
        b {
          text-align: center;
          color: black;
          font-weight: bold;
          font-size: 16px;
          margin-bottom: 15px;
        }
        .id {
          display: flex;
          align-items: center;
          padding: 3px 5px;
          background: #ececec;
          justify-content: center;
          img {
            width: 15px;
            margin-right: 8px;
          }
        }
      }
    }
  }
  .detail-content {
    display: flex;
    width: 100%;
    flex: 1;
    margin-bottom: 10px;
    flex-shrink: 0;
    justify-content: space-between;
  }
  .right {
    width: 710px;
    height: 100%;
    display: flex;
    flex-direction: column;
    .btn {
      margin: 20px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 50px;
      width: 100%;
    }
    .div-text {
      .div-content {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: flex-start;
        padding: 15px 10px;
        box-sizing: border-box;
        position: relative;
        border: 1px solid #ececec;
        .div-item {
          width: 50%;
          margin-bottom: 10px;
          font-size: 15px;
          display: flex;
          align-items: center;
          p {
            font-weight: 400;
            color: black;
          }
          span {
            display: inline-block;
            text-align: right;
            width: 100px;
            &::after {
              content: ":";
              margin: 0 3px;
            }
          }
        }
      }
      b {
        position: relative;
        background: white;
        top: 9px;
        left: 13px;
        padding: 0 10px;
        font-size: 18px;
        color: black;
        z-index: 3;
      }
    }
    .input {
      display: flex;
      align-items: center;
      margin: 30px 0;
    }
  }
  .left {
    flex: 1;
    flex-shrink: 0;
    margin-right: 20px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    .left-image {
      display: flex;
      height: 100%;
      flex-direction: column;
      justify-content: center;
      img {
        height: auto;
        width: 100%;
        max-height: 90%;
      }
      b {
        color: black;
        font-size: 18px;
        margin-bottom: 15px;
      }
    }
    .left-images {
      display: flex;
      width: 95%;
      height: 100%;
      flex-direction: column;
      b {
        color: black;
        text-align: center;
        margin-bottom: 15px;
      }
    }
  }
}
.detail-div .right .btn .btn-sure,
.detail-div .right .btn .btn-down {
  width: 100%;
  position: relative;
}
.detail-div .right .btn .btn-sure .check,
.detail-div .right .btn .btn-down .check {
  position: absolute;
  width: 144px;
  height: 100%;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  right: 0;
  top: 1px;
}
.btn {
  position: relative;
  .check {
    ::v-deep {
      .el-checkbox__inner {
        background-color: transparent !important;
        border-color: white !important;
        width: 12px;
        height: 12px;
        &::after {
          left: 3px;
        }
      }
      .el-checkbox__label {
        padding-left: 5px;
        font-size: 12px;
        color: white !important;
      }
    }
  }
}
.page {
  width: 100%;
  height: 100%;
}
p {
  margin: 0;
}
</style>
