<template>
  <div>
    <el-card class="box-card">
      <div style="padding: 8px; text-align: center" v-if="formData.data.code || formData.data.wlnCode">
        <span style="font-size: 18px; font-weight: 700">{{ formData.data.code }} </span>
        <span style="font-size: 18px; font-weight: 700" v-if="formData.data.wlnCode"> ({{ formData.data.wlnCode }})</span>
      </div>
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #type>
          <div style="width: 100%">
            <el-radio-group v-model="formData.data.type">
              <el-radio v-for="(itemType, index) in typeList" :key="index" :label="itemType.dictKey">{{ itemType.dictValue }}</el-radio>
            </el-radio-group>
          </div>
        </template>
        <template #departmentId>
          <div style="width: 100%">
            <el-select v-model="formData.data.departmentId" placeholder="请选择事业部" clearable style="width: 100%">
              <el-option v-for="item in departmentList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
            </el-select>
          </div>
        </template>
        <template #orderSkuList>
          <div style="width: 100%; padding: 0 20px">
            <el-collapse v-model="activeNames">
              <div v-for="(item, index) in formData.data.orderSkuList" :key="index" style="margin-bottom: 20px">
                <div style="border: 1px solid #edf0f5">
                  <el-table :data="[item]" :row-style="{ height: '35px' }" header-row-class-name="tableHeader">
                    <el-table-column label="产品" width="300">
                      <template #default="{ row }">
                        <div style="width: 100%">
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">商品名称: </span>
                            <span>{{ item.wlnSkuName }}</span>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">品号: </span>
                            <span>{{ item.code }}</span>
                          </div>
                          <div style="line-height: 35px; word-break: break-all">
                            <span style="color: black; font-weight: 700">品名: </span>
                            <span>{{ item.name }}</span>
                          </div>
                          <div style="line-height: 35px; display: flex">
                            <span style="width: 37px; color: black; font-weight: 700">数量: </span>
                            <el-form-item
                              :prop="'orderSkuList.' + index + '.quantity'"
                              :rules="rules.quantity"
                              :inline-message="true"
                              style="width: calc(100% - 37px)">
                              <el-input-number
                                onmousewheel="return false;"
                                v-model="row.quantity"
                                placeholder="数量"
                                style="width: 100%"
                                :controls="false"
                                :min="0"
                                :precision="0" />
                            </el-form-item>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">加工费: </span>
                            <span>{{ item.customProcessingFee }}</span>
                            <el-tooltip content="修改加工费" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'customProcessingFee', '加工费')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">代发费: </span>
                            <span>{{ item.lssueFee }}</span>
                            <el-tooltip content="修改代发费" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'lssueFee', '代发费')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">快递包材费: </span>
                            <span>{{ item.deliveryMaterialsFee }}</span>
                            <el-tooltip content="修改快递包材费" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'deliveryMaterialsFee', '快递包材费')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">包装人工费: </span>
                            <span>{{ item.packingLabor }}</span>
                            <el-tooltip content="修改包装人工费" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'packingLabor', '包装人工费')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">管理费: </span>
                            <span>{{ item.managementFee }}</span>
                            <el-tooltip content="修改管理费" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'managementFee', '管理费')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">单价: </span>
                            <span>{{ item.unitPrice }}</span>
                            <el-tooltip content="修改单价" placement="top" effect="light">
                              <el-icon
                                style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                @click="clickChangePrice(item, index, 'unitPrice', '单价')"
                                v-if="route.query && route.query.detailId">
                                <EditPen />
                              </el-icon>
                            </el-tooltip>
                          </div>
                          <div style="line-height: 35px">
                            <span style="color: black; font-weight: 700">小计: </span>
                            <span>{{ getSubtotal(item) }}</span>
                          </div>
                          <div style="line-height: 35px">
                            <span style="width: 37px; color: black; font-weight: 700">打印: </span>
                            <el-form-item
                              :prop="'orderSkuList.' + index + '.printType'"
                              :rules="rules.printType"
                              :inline-message="true"
                              style="width: calc(100% - 37px)">
                              <el-radio-group v-model="item.printType">
                                <el-radio v-for="(itemType, index) in printType" :key="index" :label="itemType.dictKey">{{ itemType.dictValue }}</el-radio>
                              </el-radio-group>
                            </el-form-item>
                          </div>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column label="产品图稿" width="400">
                      <template #default="{ row }">
                        <div style="display: flex; width: 100%">
                          <div style="width: 80px">设计图:</div>
                          <div style="width: calc(100% - 80px)">
                            <el-image
                              fit="scale-down"
                              style="width: 148px; height: 148px; margin-right: 10px; cursor: pointer"
                              v-if="row.blueprint"
                              :src="row.blueprint"
                              @click="openFile(row.blueprint)" />
                          </div>
                        </div>
                        <div style="display: flex; margin-top: 20px; width: 100%">
                          <div style="width: 80px">图稿文件:</div>
                          <div style="width: calc(100% - 80px)">
                            <a
                              style="color: #409eff; cursor: pointer; word-break: break-all; margin-right: 10px"
                              @click="openFile(row.productionDocument)"
                              v-if="row.productionDocument">
                              {{ row.productionDocument }}
                            </a>
                          </div>
                        </div>
                        <div style="font-weight: 700; margin-top: 20px">产品不干胶图稿</div>
                        <div style="display: flex; width: 100%">
                          <div style="width: 80px">不干胶图片:</div>
                          <div style="width: calc(100% - 80px)">
                            <el-image
                              fit="scale-down"
                              style="width: 148px; height: 148px; margin-right: 10px; cursor: pointer"
                              v-if="row.selfAdhesiveStickerFile && row.selfAdhesiveStickerFile.fileUrl"
                              :src="row.selfAdhesiveStickerFile.fileUrl"
                              @click="openFile(row.selfAdhesiveStickerFile.fileUrl)" />
                          </div>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column label="包材配件/单品" min-width="400">
                      <template #default="{ row }">
                        <div style="width: 100%">
                          <el-table :data="row.orderSkuBomList" :row-style="{ height: '35px' }" header-row-class-name="tableHeader">
                            <el-table-column label="单价¥" width="120">
                              <template #default="props">
                                <div>
                                  <span>{{ moneyFormat(props.row.unitPrice, 2) }}</span>
                                  <el-tooltip content="修改单价¥" placement="top" effect="light">
                                    <el-icon
                                      style="margin-left: 10px; transform: translateY(2px); color: #409eff; cursor: pointer"
                                      @click="clickChangeBomPrice(index, props.row, props.$index, 'unitPrice', '单价¥')"
                                      v-if="route.query && route.query.detailId">
                                      <EditPen />
                                    </el-icon>
                                  </el-tooltip>
                                </div>
                              </template>
                            </el-table-column>
                            <el-table-column label="数量" width="100">
                              <template #default="props">
                                <el-form-item
                                  :prop="'orderSkuList.' + index + '.orderSkuBomList.' + props.$index + '.quantity'"
                                  :rules="rules.quantity"
                                  :inline-message="true"
                                  style="width: 100%">
                                  <el-input-number
                                    onmousewheel="return false;"
                                    v-model="props.row.quantity"
                                    placeholder="数量"
                                    style="width: 100%"
                                    :controls="false"
                                    :min="0" />
                                </el-form-item>
                              </template>
                            </el-table-column>
                            <el-table-column label="名称" prop="bomSpecName" min-width="150" />
                            <el-table-column label="总量" width="80">
                              <template #default="props">
                                {{ computeQuantity(index, props.$index) }}
                              </template>
                            </el-table-column>
                            <el-table-column label="小计¥" width="100">
                              <template #default="props">
                                {{ moneyFormat(computeMoney(index, props.$index), 2) }}
                              </template>
                            </el-table-column>
                          </el-table>
                        </div>
                      </template>
                    </el-table-column>
                  </el-table>
                  <el-collapse-item :name="index">
                    <template #title>
                      <span>包装</span>
                    </template>
                    <div style="display: flex; padding: 8px 10px 0px">
                      <div style="flex: 1; padding: 0px 10px">
                        <div>包装要求:</div>
                        <div v-html="getStyle(item.packageRemark)"></div>
                      </div>
                    </div>
                  </el-collapse-item>
                </div>
              </div>
            </el-collapse>
          </div>
        </template>
        <template #deliveryTime>
          <div style="width: 100%">
            <el-date-picker
              v-model="formData.data.deliveryTime"
              type="datetime"
              placeholder="请选择交货日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              style="width: 100%" />
          </div>
        </template>
        <template #totalMonet>
          <div style="width: 100%; margin-left: 30px">
            <div>
              <span style="font-weight: 700; color: #6c88f1">产品总金额: ¥{{ moneyFormat(calculatedAmount("unitPrice"), 2) }}</span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">
                定制加工费: ¥{{ moneyFormat(calculatedAmount("customProcessingFee"), 2) }}
              </span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">代发费: ¥{{ moneyFormat(calculatedAmount("lssueFee"), 2) }}</span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">
                快递包材费: ¥{{ moneyFormat(calculatedAmount("deliveryMaterialsFee"), 2) }}
              </span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">包装人工费: ¥{{ moneyFormat(calculatedAmount("packingLabor"), 2) }}</span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">包材费: ¥{{ moneyFormat(calculatedPackagingMaterialCost(), 2) }}</span>
              <span style="font-weight: 700; color: #6c88f1; margin-left: 40px">管理费: ¥{{ moneyFormat(calculatedAmount("managementFee"), 2) }}</span>
            </div>
            <div style="padding: 8px 0 0 0">
              <span style="font-weight: 700; color: red">订单总金额(含税): ¥{{ moneyFormat(calculatedTotalAmount(), 2) }}</span>
            </div>
          </div>
        </template>
        <template #attachments>
          <div style="width: 100%">
            <el-upload
              v-model:fileList="fileList"
              action="https://winfaster.obs.cn-south-1.myhuaweicloud.com"
              :data="uploadFileData"
              multiple
              :before-upload="beforeUpload"
              :on-success="onSuccessFile"
              :on-preview="onPreviewFile">
              <el-button style="background: #20b2aa; color: #fff; border: 1px solid #20b2aa">上传</el-button>
            </el-upload>
          </div>
        </template>
        <template #remark>
          <div style="margin: 0 2vw">
            <div v-html="getStyle(formData.data.remark)"></div>
          </div>
        </template>
      </byForm>
      <div style="text-align: center; margin: 10px">
        <el-button @click="clickCancel()" size="large">关 闭</el-button>
      </div>
    </el-card>

    <el-dialog :title="'修改' + textPrice" v-if="openChangePrice" v-model="openChangePrice" width="500">
      <el-form :model="changePrice.data" label-width="120px" ref="price">
        <el-form-item
          label="定制加工类型"
          prop="customProcessingType"
          :rules="[{ required: true, message: '请选择定制加工类型', trigger: 'change' }]"
          v-if="labelPrice === 'customProcessingFee'">
          <el-select
            v-model="changePrice.data.customProcessingType"
            placeholder="请选择定制加工类型"
            style="width: 100%"
            clearable
            @change="changeProcessingType">
            <el-option :label="'无'" :value="'-1'" />
            <el-option
              v-for="itemDict in useUserStore().allDict['processing_layout']"
              :key="itemDict.dictKey"
              :label="itemDict.dictValue"
              :value="itemDict.dictKey" />
          </el-select>
        </el-form-item>
        <el-form-item :label="textPrice" :prop="labelPrice" :rules="[{ required: true, message: '请输入' + textPrice, trigger: 'blur' }]">
          <el-input-number
            onmousewheel="return false;"
            v-model="changePrice.data[labelPrice]"
            :placeholder="textPrice"
            style="width: 100%"
            :controls="false"
            :min="0"
            :precision="2"
            :disabled="(!changePrice.data.customProcessingType || changePrice.data.customProcessingType === '-1') && labelPrice === 'customProcessingFee'" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="openChangePrice = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitChangePrice()" size="large" v-preReClick>提 交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byForm from "/src/components/byForm/index";
import { ElMessage } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import useTagsViewStore from "/src/store/modules/tagsView";

const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
const submit = ref(null);
const departmentList = ref([]);
const activeNames = ref([]);
const formOption = reactive({
  inline: true,
  labelWidth: "120px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {
    remark: "",
    type: 1,
    orderSkuList: [],
    fileList: [],
  },
});
const formConfig = computed(() => {
  return [
    {
      type: "title",
      title: "类型",
      label: "",
    },
    {
      type: "slot",
      prop: "type",
      slotName: "type",
      label: "订单类型",
    },
    {
      type: "slot",
      prop: "departmentId",
      slotName: "departmentId",
      label: "事业部",
      itemWidth: 25,
    },
    {
      type: "title",
      title: "产品",
      label: "",
    },
    {
      type: "slot",
      prop: "orderSkuList",
      slotName: "orderSkuList",
    },
    {
      type: "title",
      title: "贸易",
      label: "",
    },
    {
      type: "slot",
      prop: "deliveryTime",
      slotName: "deliveryTime",
      label: "交货时间",
      itemWidth: 25,
    },
    {
      type: "select",
      label: "选择快递",
      prop: "expressDeliveryId",
      data: proxy.useUserStore().allDict["express_delivery"],
      itemWidth: 25,
      clearable: true,
    },
    {
      type: "select",
      label: "店铺来源",
      prop: "sourcePlatform",
      data: proxy.useUserStore().allDict["source_platform"],
      itemWidth: 25,
      clearable: true,
    },
    {
      type: "select",
      label: "店铺名称",
      prop: "shopName",
      data: proxy.useUserStore().allDict["shop_name"],
      itemWidth: 25,
      clearable: true,
    },
    {
      type: "title",
      title: "总计",
      label: "",
    },
    {
      type: "slot",
      prop: "totalMonet",
      slotName: "totalMonet",
    },
    {
      type: "title",
      title: "附件",
      label: "",
    },
    {
      type: "slot",
      slotName: "attachments",
      label: "附件",
    },
    {
      type: "title",
      title: "订单备注",
      label: "",
    },
    {
      type: "slot",
      slotName: "remark",
    },
  ];
});
const rules = ref({
  province: [{ required: true, message: "请输入省", trigger: "blur" }],
  detailedAddress: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
  consignee: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  consigneeNumber: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
  deliveryTime: [{ required: true, message: "请选择交货时间", trigger: "change" }],
  //   expressDeliveryId: [{ required: true, message: "请选择快递", trigger: "change" }],
  commercePlatform: [{ required: true, message: "请选择电商平台", trigger: "change" }],
  quantity: [{ required: true, message: "请输入数量", trigger: "blur" }],
  type: [{ required: true, message: "请选择订单类型", trigger: "change" }],
  departmentId: [{ required: true, message: "请选择事业部", trigger: "change" }],
});
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const openFile = (path) => {
  window.open(path);
};
const clickCancel = () => {
  const useTagsStore = useTagsViewStore();
  useTagsStore.delVisitedView(router.currentRoute.value);
  if (route.query && route.query.orderInquiry) {
    router.replace({
      path: "/production/schedule/order-inquiry",
    });
  } else {
    router.replace({
      path: "/group/order/order-management",
    });
  }
};
onMounted(() => {
  if (route.query && (route.query.id || route.query.detailId)) {
    useTagsViewStore().visitedViews = useTagsViewStore().visitedViews.map((item) => {
      if (item.query && item.query.random === route.query.random) {
        return {
          ...item,
          name: route.query.text,
          title: route.query.text,
        };
      } else {
        return {
          ...item,
        };
      }
    });
    getOrderDetail({ id: route.query.id || route.query.detailId });
  }
});
const getOrderDetail = (parameter) => {
  proxy.post("/orderInfo/detail", parameter).then((res) => {
    formData.data = res;
    if (!formData.data.type) {
      formData.data.type = 1;
    }
    if (route.query.id) {
      proxy.$refs.editor.changeHtml(formData.data.remark);
    }
    if (route.query.detailId) {
      let allIndex = [];
      for (let i = 0; i < formData.data.orderSkuList.length; i++) {
        allIndex.push(i);
      }
      activeNames.value = allIndex;
      formOption.disabled = true;
    }
    let list = [formData.data.id];
    if (formData.data.orderSkuList && formData.data.orderSkuList.length > 0) {
      list = list.concat(formData.data.orderSkuList.map((item) => item.id));
      proxy.post("/fileInfo/getList", { businessIdList: list }).then((fileObj) => {
        if (fileObj[formData.data.id] && fileObj[formData.data.id].length > 0) {
          let file = fileObj[formData.data.id].filter((item) => item.businessType == "0");
          if (file && file.length > 0) {
            fileList.value = file.map((item) => {
              return {
                raw: item,
                name: item.fileName,
                url: item.fileUrl,
              };
            });
          } else {
            fileList.value = [];
          }
        }
        for (let i = 0; i < formData.data.orderSkuList.length; i++) {
          if (fileObj[formData.data.orderSkuList[i].id] && fileObj[formData.data.orderSkuList[i].id].length > 0) {
            formData.data.orderSkuList[i].selfAdhesiveStickerFile = {
              id: fileObj[formData.data.orderSkuList[i].id][0].id,
              fileName: fileObj[formData.data.orderSkuList[i].id][0].fileName,
              fileUrl: fileObj[formData.data.orderSkuList[i].id][0].fileUrl,
            };
          }
        }
      });
    }
  });
};
const getStyle = (text) => {
  if (text) {
    return text.replace(/\n|\r\n/g, "<br>");
  } else {
    return "";
  }
};
const printType = ref([
  {
    dictKey: 1,
    dictValue: "单面",
  },
  {
    dictKey: 2,
    dictValue: "双面",
  },
]);
const typeList = ref([
  {
    dictKey: 1,
    dictValue: "自主订单",
  },
  {
    dictKey: 2,
    dictValue: "委外订单",
  },
]);
const changePrice = reactive({
  data: {},
});
const openChangePrice = ref(false);
const productIndex = ref(0);
const productIndexBOM = ref(0);
const labelPrice = ref("");
const textPrice = ref("");
const clickChangePrice = (item, index, label, text) => {
  changePrice.data = {
    customProcessingFee: item.customProcessingFee,
    lssueFee: item.lssueFee,
    deliveryMaterialsFee: item.deliveryMaterialsFee,
    packingLabor: item.packingLabor,
    managementFee: item.managementFee,
    unitPrice: item.unitPrice,
    customProcessingType: item.customProcessingType,
  };
  productIndex.value = index;
  productIndexBOM.value = null;
  labelPrice.value = label;
  textPrice.value = text;
  openChangePrice.value = true;
};
const submitChangePrice = () => {
  proxy.$refs.price.validate((valid) => {
    if (valid) {
      if (productIndexBOM.value === null) {
        formData.data.orderSkuList[productIndex.value][labelPrice.value] = changePrice.data[labelPrice.value];
        if (labelPrice.value === "customProcessingFee") {
          formData.data.orderSkuList[productIndex.value].customProcessingType = changePrice.data.customProcessingType;
        }
        if (formData.data.orderSkuList && formData.data.orderSkuList.length > 0) {
          for (let i = 0; i < formData.data.orderSkuList.length; i++) {
            let packagingMaterialCost = 0;
            if (formData.data.orderSkuList[i].quantity) {
              if (formData.data.orderSkuList[i].orderSkuBomList && formData.data.orderSkuList[i].orderSkuBomList.length > 0) {
                for (let j = 0; j < formData.data.orderSkuList[i].orderSkuBomList.length; j++) {
                  if (formData.data.orderSkuList[i].orderSkuBomList[j].quantity && formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice) {
                    packagingMaterialCost = Number(
                      Math.round(
                        (packagingMaterialCost +
                          formData.data.orderSkuList[i].orderSkuBomList[j].quantity * formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice) *
                          100
                      ) / 100
                    );
                  }
                }
              }
            }
            formData.data.orderSkuList[i].packagingMaterialCost = packagingMaterialCost;
          }
        }
        formData.data.productTotalAmount = calculatedAmount("unitPrice");
        formData.data.customProcessingFee = calculatedAmount("customProcessingFee");
        formData.data.lssueFee = calculatedAmount("lssueFee");
        formData.data.deliveryMaterialsFee = calculatedAmount("deliveryMaterialsFee");
        formData.data.packingLabor = calculatedAmount("packingLabor");
        formData.data.managementFee = calculatedAmount("managementFee");
        formData.data.packagingMaterialCost = calculatedPackagingMaterialCost();
        formData.data.totalAmount = calculatedTotalAmount();
        if (fileList.value && fileList.value.length > 0) {
          formData.data.fileList = fileList.value.map((item) => {
            return {
              id: item.raw.id,
              fileName: item.raw.fileName,
              fileUrl: item.raw.fileUrl,
            };
          });
        } else {
          formData.data.fileList = [];
        }
        proxy.post("/orderInfo/edit", formData.data).then(() => {
          ElMessage({ message: "修改完成", type: "success" });
          openChangePrice.value = false;
          getOrderDetail({ id: route.query.id || route.query.detailId });
        });
      } else {
        formData.data.orderSkuList[productIndex.value].orderSkuBomList[productIndexBOM.value][labelPrice.value] = changePrice.data[labelPrice.value];
        if (formData.data.orderSkuList && formData.data.orderSkuList.length > 0) {
          for (let i = 0; i < formData.data.orderSkuList.length; i++) {
            let packagingMaterialCost = 0;
            if (formData.data.orderSkuList[i].quantity) {
              if (formData.data.orderSkuList[i].orderSkuBomList && formData.data.orderSkuList[i].orderSkuBomList.length > 0) {
                for (let j = 0; j < formData.data.orderSkuList[i].orderSkuBomList.length; j++) {
                  if (formData.data.orderSkuList[i].orderSkuBomList[j].quantity && formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice) {
                    packagingMaterialCost = Number(
                      Math.round(
                        (packagingMaterialCost +
                          formData.data.orderSkuList[i].orderSkuBomList[j].quantity * formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice) *
                          100
                      ) / 100
                    );
                  }
                }
              }
            }
            formData.data.orderSkuList[i].packagingMaterialCost = packagingMaterialCost;
          }
        }
        formData.data.productTotalAmount = calculatedAmount("unitPrice");
        formData.data.customProcessingFee = calculatedAmount("customProcessingFee");
        formData.data.lssueFee = calculatedAmount("lssueFee");
        formData.data.deliveryMaterialsFee = calculatedAmount("deliveryMaterialsFee");
        formData.data.packingLabor = calculatedAmount("packingLabor");
        formData.data.managementFee = calculatedAmount("managementFee");
        formData.data.packagingMaterialCost = calculatedPackagingMaterialCost();
        formData.data.totalAmount = calculatedTotalAmount();
        if (fileList.value && fileList.value.length > 0) {
          formData.data.fileList = fileList.value.map((item) => {
            return {
              id: item.raw.id,
              fileName: item.raw.fileName,
              fileUrl: item.raw.fileUrl,
            };
          });
        } else {
          formData.data.fileList = [];
        }
        proxy.post("/orderInfo/edit", formData.data).then(() => {
          ElMessage({ message: "修改完成", type: "success" });
          openChangePrice.value = false;
          getOrderDetail({ id: route.query.id || route.query.detailId });
        });
      }
    }
  });
};
const clickChangeBomPrice = (index, item, indexBOM, label, text) => {
  changePrice.data = {
    unitPrice: item.unitPrice,
  };
  productIndex.value = index;
  productIndexBOM.value = indexBOM;
  labelPrice.value = label;
  textPrice.value = text;
  openChangePrice.value = true;
};
const changeProcessingType = () => {
  changePrice.data[labelPrice.value] = 0;
};
const computeQuantity = (index, indexSKU) => {
  let quantity = 0;
  if (formData.data.orderSkuList[index].quantity && formData.data.orderSkuList[index].orderSkuBomList[indexSKU].quantity) {
    quantity = Number(
      Math.round(formData.data.orderSkuList[index].orderSkuBomList[indexSKU].quantity * formData.data.orderSkuList[index].quantity * 100) / 100
    );
  }
  return quantity;
};
const computeMoney = (index, indexSKU) => {
  let money = 0;
  if (
    formData.data.orderSkuList[index].quantity &&
    formData.data.orderSkuList[index].orderSkuBomList[indexSKU].quantity &&
    formData.data.orderSkuList[index].orderSkuBomList[indexSKU].unitPrice
  ) {
    money = Number(
      Math.round(
        formData.data.orderSkuList[index].orderSkuBomList[indexSKU].quantity *
          formData.data.orderSkuList[index].orderSkuBomList[indexSKU].unitPrice *
          formData.data.orderSkuList[index].quantity *
          100
      ) / 100
    );
  }
  return money;
};
const getSubtotal = (item) => {
  let money = 0;
  if (item.quantity) {
    money = Number(
      Math.round(
        (item.customProcessingFee + item.deliveryMaterialsFee + item.lssueFee + item.packingLabor + item.managementFee + item.unitPrice) * item.quantity * 100
      ) / 100
    );
  }
  return money;
};
const calculatedAmount = (label) => {
  let money = 0;
  if (formData.data.orderSkuList && formData.data.orderSkuList.length > 0) {
    for (let i = 0; i < formData.data.orderSkuList.length; i++) {
      if (formData.data.orderSkuList[i].quantity && formData.data.orderSkuList[i][label]) {
        money = Number(Math.round((money + formData.data.orderSkuList[i][label] * formData.data.orderSkuList[i].quantity) * 100) / 100);
      }
    }
  }
  return money;
};
const calculatedPackagingMaterialCost = () => {
  let money = 0;
  if (formData.data.orderSkuList && formData.data.orderSkuList.length > 0) {
    for (let i = 0; i < formData.data.orderSkuList.length; i++) {
      if (formData.data.orderSkuList[i].orderSkuBomList && formData.data.orderSkuList[i].orderSkuBomList.length > 0) {
        for (let j = 0; j < formData.data.orderSkuList[i].orderSkuBomList.length; j++) {
          if (formData.data.orderSkuList[i].orderSkuBomList[j].quantity && formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice) {
            money = Number(
              Math.round(
                (money +
                  formData.data.orderSkuList[i].orderSkuBomList[j].quantity *
                    formData.data.orderSkuList[i].orderSkuBomList[j].unitPrice *
                    formData.data.orderSkuList[i].quantity) *
                  100
              ) / 100
            );
          }
        }
      }
    }
  }
  return money;
};
const calculatedTotalAmount = () => {
  let money = 0;
  money = Number(
    Math.round(
      (calculatedAmount("unitPrice") +
        calculatedAmount("customProcessingFee") +
        calculatedAmount("lssueFee") +
        calculatedAmount("deliveryMaterialsFee") +
        calculatedAmount("packingLabor") +
        calculatedAmount("managementFee") +
        calculatedPackagingMaterialCost()) *
        100
    ) / 100
  );
  return money;
};
const fileList = ref([]);
const uploadFileData = ref({});
const beforeUpload = async (file) => {
  const res = await proxy.post("/fileInfo/getSing", { fileName: file.name });
  uploadFileData.value = res.uploadBody;
  file.id = res.id;
  file.fileName = res.fileName;
  file.fileUrl = res.fileUrl;
  file.uploadState = true;
  return true;
};
const onSuccessFile = (any, UploadFile) => {
  UploadFile.raw.uploadState = false;
};
const onPreviewFile = (file) => {
  window.open(file.raw.fileUrl, "_blank");
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
:deep(.ql-editor) {
  height: auto;
}
:deep(.el-collapse-item__header) {
  justify-content: center;
}
:deep(.el-collapse-item__arrow) {
  margin: 0;
}
.avatar-uploader .avatar {
  width: 148px;
  height: 148px;
  display: block;
  background-color: black;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  text-align: center;
  border: 1px dashed var(--el-border-color);
}
:deep(.el-table__cell) {
  vertical-align: top;
}
</style>
