<template>
  <el-card class="box-card">
    <div style="margin-bottom: 10px">
      <el-button type="primary" @click="clickColour()" v-preReClick>颜色加价规则</el-button>
      <el-button type="primary" @click="clickBasics()" v-preReClick>基础价格设置</el-button>
      <el-button type="primary" @click="clickProcessCost()" v-preReClick>加工费配置</el-button>
    </div>

    <el-form :model="formData.data" :rules="rules" :inline="true" style="margin-top: 20px" ref="submit">
      <el-form-item label="裸垫类型" prop="bomClassify">
        <el-select v-model="formData.data.bomClassify" placeholder="请选择">
          <el-option label="瑜伽垫" :value="1"></el-option>
          <el-option label="跳绳垫" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="levelConfigId">
        <el-select v-model="formData.data.levelConfigId" placeholder="请选择">
          <el-option v-for="item in levelConfigList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品类" prop="categoryConfigId">
        <el-select v-model="formData.data.categoryConfigId" placeholder="请选择">
          <el-option v-for="item in categoryConfigList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="颜色1" prop="colourConfigOne">
        <el-select v-model="formData.data.colourConfigOne" placeholder="请选择">
          <el-option v-for="item in colourPriceList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="颜色2" prop="colourConfigTwo">
        <el-select v-model="formData.data.colourConfigTwo" placeholder="请选择">
          <el-option v-for="item in colourPriceList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="压纹类型" prop="embossing">
        <el-select v-model="formData.data.embossing" placeholder="请选择">
          <el-option v-for="item in embossingList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="长度 cm" prop="length">
        <el-input-number
          v-model="formData.data.length"
          placeholder="请输入长度"
          onmousewheel="return false;"
          :controls="false"
          :min="0"
          :precision="0"></el-input-number>
      </el-form-item>
      <el-form-item label="宽度 cm" prop="width">
        <el-input-number
          v-model="formData.data.width"
          placeholder="请输入宽度"
          onmousewheel="return false;"
          :controls="false"
          :min="0"
          :precision="0"></el-input-number>
      </el-form-item>
      <el-form-item label="厚度 mm" prop="height">
        <el-input-number
          v-model="formData.data.height"
          placeholder="请输入厚度"
          onmousewheel="return false;"
          :controls="false"
          :min="0"
          :precision="2"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="clickCalculator()" v-preReClick>计算</el-button>
        <el-button @click="clickReset()" v-preReClick>重置</el-button>
      </el-form-item>
    </el-form>
    <div style="font-size: 16px; font-weight: 700; color: black; margin-top: 20px">
      计算公式=（基材价格+品类+特性+颜色价格）*[（长+裁切配置）*（宽+裁切配置）/10000]*（厚度+压纹配置）*（1+损耗）+加工费+运费
    </div>
    <div style="font-size: 16px; font-weight: 700; color: black; margin-top: 20px">
      注：颜色价格区分单色层双色层，双色层时如果一半为加价颜色时，颜色价格除以2计算，如果双色都为加价颜色分别除以2后单价相加，例如：兰黛紫+兰黛颜色费用为0.2/2+0.3/2
    </div>
    <div style="font-size: 22px; font-weight: 700; color: black; margin-top: 20px">计算结果:</div>
    <div style="font-size: 16px; margin-top: 20px">
      <span>裸垫价格: {{ priceData.data.unitPrice }}:</span>
      <span>
        （{{ priceData.data.baseMaterialPrice }} + {{ priceData.data.categoryPrice }} + {{ priceData.data.characterPrice }} + {{ priceData.data.colourPrice }}）
        *[（{{ priceData.data.length }} + {{ priceData.data.cropLength }}）*（{{ priceData.data.width }} + {{ priceData.data.cropWidth }}）/10000]* （{{
          priceData.data.height
        }}+ {{ priceData.data.embossingHeight }}）*（1+{{ priceData.data.depletion / 100 }}）+{{ priceData.data.processingFee }}+{{
          priceData.data.freightFee
        }}]
      </span>
    </div>

    <el-dialog title="颜色加价规则" v-if="openColour" v-model="openColour" width="1000">
      <Colour></Colour>
      <template #footer>
        <el-button @click="openColour = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>

    <el-dialog title="基础价格设置" v-if="openBasics" v-model="openBasics" width="1600">
      <Basics></Basics>
      <template #footer>
        <el-button @click="openBasics = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>

    <el-dialog title="加工费配置" v-if="openProcessCost" v-model="openProcessCost" width="500">
      <ProcessCost></ProcessCost>
      <template #footer>
        <el-button @click="openProcessCost = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import Colour from "/src/views/group/BOM/calculator/colour.vue";
import Basics from "/src/views/group/BOM/calculator/basics.vue";
import ProcessCost from "/src/views/group/BOM/calculator/processCost.vue";

const { proxy } = getCurrentInstance();
const openColour = ref(false);
const clickColour = () => {
  openColour.value = true;
};
const openBasics = ref(false);
const clickBasics = () => {
  openBasics.value = true;
};
const openProcessCost = ref(false);
const clickProcessCost = () => {
  openProcessCost.value = true;
};
const levelConfigList = ref([]);
const categoryConfigList = ref([]);
const colourPriceList = ref([]);
const embossingList = ref([]);
const formData = reactive({
  data: {
    bomClassify: "",
    levelConfigId: "",
    categoryConfigId: "",
    characterConfigIds: "",
    colourConfigIds: "",
    embossing: "",
    length: undefined,
    width: undefined,
    height: undefined,
  },
});
const rules = ref({
  bomClassify: [{ required: true, message: "请选择裸垫类型", trigger: "change" }],
  levelConfigId: [{ required: true, message: "请选择级别", trigger: "change" }],
  categoryConfigId: [{ required: true, message: "请选择品类", trigger: "change" }],
  colourConfigOne: [{ required: true, message: "请选择颜色", trigger: "change" }],
  embossing: [{ required: true, message: "请选择压纹类型", trigger: "change" }],
  length: [{ required: true, message: "请输入长度", trigger: "blur" }],
  width: [{ required: true, message: "请输入宽度", trigger: "blur" }],
  height: [{ required: true, message: "请输入厚度", trigger: "blur" }],
});
const getDemandData = () => {
  proxy.post("/bomSpecPriceConfig/list", { code: "base_material_price" }).then((res) => {
    if (res && res.length > 0) {
      levelConfigList.value = res.map((item) => {
        let name = item.name;
        if (item.type === 0) {
          name += "（体系一）";
        } else {
          name += "（体系二）";
        }
        return {
          dictKey: item.id,
          dictValue: name,
        };
      });
    }
  });
  proxy.post("/bomSpecPriceConfig/list", { code: "category_price" }).then((res) => {
    if (res && res.length > 0) {
      categoryConfigList.value = res.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
  proxy.post("/bomSpecPriceConfig/list", { code: "colour_price" }).then((res) => {
    if (res && res.length > 0) {
      colourPriceList.value = res.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.description + item.name,
        };
      });
    }
  });
  proxy.post("/bomSpecPriceConfig/list", { code: "embossing_config" }).then((res) => {
    if (res && res.length > 0) {
      embossingList.value = res.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const priceData = reactive({
  data: {},
});
const clickCalculator = () => {
  proxy.$refs.submit.validate((valid) => {
    if (valid) {
      if (formData.data.colourConfigTwo) {
        formData.data.colourConfigIds = formData.data.colourConfigOne + "," + formData.data.colourConfigTwo;
      } else {
        formData.data.colourConfigIds = formData.data.colourConfigOne;
      }
      proxy.post("/bomSpecPriceConfig/getBomSpecPrice", formData.data).then((res) => {
        priceData.data = res;
      });
    }
  });
};
const clickReset = () => {
  priceData.data = {};
  proxy.$refs.submit.resetFields();
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
