<script>
import {Decimal} from "decimal.js";

export default {
  name: "costPrice",
  props: {
    detail: {
      type: Object,
      request: true
    },
    height: {
      type: Number,
      default: 400,
    },
    loadingStatus:{
      type:Boolean,
      default: false,
    },
    bomCost:{
      type:Number,
      default:0,
    },
    toolCost:{
      type:Number,
      default:0,
    },
    packCost:{
      type:Number,
      default:0,
    },
    shippingCost:{
      type:Number,
      default:0,
    },
    machineCost:{
      type:Number,
      default:0,
    },
    laborCost:{
      type:Number,
      default:0,
    },
    manufacturingCost:{
      type:Number,
      default:0,
    }
  },
  data() {
    return {
      rules: {
        detailProfitRate: [{required: true, pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        adjustPartCost: [{required: true, pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        adjustMachineCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        adjustFabricateCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        adjustLabourCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        adjustToolCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/,  message: ' ', trigger: ['change','blur']}],
        detailManageCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/,  message: ' ', trigger: ['change','blur']}],
        detailOtherCost: [{required: true,pattern:/^[1-9]\d{0,14}(\.\d{1,16})?$|^0(\.\d{1,16})?$/, message: ' ', trigger: ['change','blur']}],
        taxRate: [{required: true,  message: ' ', trigger: ['change','blur']}],
      },
      quotationDetailColumns: [
        {label: "productDesc", value: "产品名称"},
        {label: "quotationDetailQuantity", value: "报价数量"},
        {label: "internalInquiryNo", value: "内部询价单号"},
        {label: "quotationDetailStatus", value: "报价状态"},
        {label: "itemNo", value: "序号"},
        {label: "adjustPartCost", value: "调整后的材料成本"},
        {label: "adjustMachineCost", value: "调整后的机器成本"},
        {label: "adjustFabricateCost", value: "调整后的制造费用"},
        {label: "adjustLabourCost", value: "调整后人的工成本"},
        {label: "adjustToolCost", value: "调整后的工具成本"},
        {label: "detailManageCost", value: "管理成本"},
        {label: "detailOtherCost", value: "其他成本"},
        {label: "detailProfitRate", value: "利润率"},
        {label: "finalUntaxedPrice", value: "最终去税价格"},
      ],
      copyPackCost:0,
      copyShippingCost:0,
    }
  },
  computed: {
    copyDetail: {
      get() {
        return this.detail
      },
      set(val) {

      }
    }
  },
  methods:{
    ruleCostForm(){
      let flag = false;
      this.$refs.costForm.validate((validate,objects)=>{
        if (!validate){
          this.rulesValidateLabel(objects, this.quotationDetailColumns);
        }
        flag = validate;
      })
      return flag;
    },
    restCostForm(){
      this.$refs.costForm.resetFields();
    },
    getQuotationDetail(){
      return JSON.parse(JSON.stringify(this.copyDetail));
    },
    rulesValidateLabel(objects, labels) {
      for (let filed in objects) {
        for (let i = 0; i < labels.length; i++) {
          let quotationToolColumn = labels[i];
          if (quotationToolColumn.label === filed) {
            this.$message.warning(quotationToolColumn.value+"为空或填写不正确");
            return
          }
        }
      }
    },
    computeCost(){
      //材料成本
      this.copyDetail.computePartCost = new Decimal(new Decimal(this.bomCost).mul(new Decimal(this.detail.quotationDetailQuantity)).toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.adjustPartCost = new Decimal(new Decimal(this.bomCost).mul(new Decimal(this.detail.quotationDetailQuantity)).toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      // 机器成本
      this.copyDetail.computeMachineCost = new Decimal(this.machineCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.adjustMachineCost = new Decimal(this.machineCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.computeMachineCost = new Decimal(this.copyDetail.computeMachineCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.adjustMachineCost = new Decimal(this.copyDetail.adjustMachineCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()

      // 制作费用
      this.copyDetail.computeFabricateCost = new Decimal(this.manufacturingCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.adjustFabricateCost = new Decimal(this.manufacturingCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.computeFabricateCost = new Decimal(this.copyDetail.computeFabricateCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.adjustFabricateCost = new Decimal(this.copyDetail.adjustFabricateCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      // 人工费用
      this.copyDetail.computeLabourCost = new Decimal(this.laborCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.adjustLabourCost = new Decimal(this.laborCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.computeLabourCost = new Decimal(this.copyDetail.computeLabourCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.adjustLabourCost = new Decimal(this.copyDetail.adjustLabourCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      // 工具成本
      this.copyDetail.computeToolCost = new Decimal(this.toolCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.adjustToolCost = new Decimal(this.toolCost).mul(new Decimal(this.detail.quotationDetailQuantity))
      this.copyDetail.computeToolCost = new Decimal(this.copyDetail.computeToolCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.adjustToolCost = new Decimal(this.copyDetail.adjustToolCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
      // 其他成本
      this.copyDetail.detailOtherCost = new Decimal(new Decimal(new Decimal(this.shippingCost).add(new Decimal(this.packCost)).mul(new Decimal(this.detail.quotationDetailQuantity))).toFixed(4,Decimal.ROUND_HALF_UP)).toSignificantDigits().toNumber()
      this.copyPackCost = new Decimal(new Decimal(new Decimal(new Decimal(this.packCost)).mul(new Decimal(this.detail.quotationDetailQuantity))).toFixed(4,Decimal.ROUND_HALF_UP)).toSignificantDigits().toNumber()
      this.copyShippingCost = new Decimal(new Decimal(new Decimal(this.shippingCost).mul(new Decimal(this.detail.quotationDetailQuantity))).toFixed(4,Decimal.ROUND_HALF_UP)).toSignificantDigits().toNumber()
      // 管理成本
      if (!this.copyDetail.detailManageCost){
        this.copyDetail.detailManageCost = 0;
      }
      // 利润率
      if (!this.copyDetail.detailProfitRate){
        this.copyDetail.detailProfitRate = 0;
      }
      this.computeAllCost();
    },
    computeAllCost(){
      this.computedTotalCost();// 总成本
      this.computedAmountOfProfitCost();//利润额
      this.computedUntaxedCost();//未税总额和单价
      this.computedTaxedCost();//含税总额和单价
    },
    // 计算总成本
    computedTotalCost(){
      this.copyDetail.detailTotalCost = new Decimal(this.copyDetail.adjustPartCost)
        .add(new Decimal(this.copyDetail.adjustMachineCost))
        .add(new Decimal(this.copyDetail.adjustFabricateCost))
        .add(new Decimal(this.copyDetail.adjustLabourCost))
        .add(new Decimal(this.copyDetail.adjustToolCost))
        .add(new Decimal(this.copyDetail.detailManageCost))
        .add(new Decimal(this.copyDetail.detailOtherCost))
      this.copyDetail.detailTotalCost = new Decimal(this.copyDetail.detailTotalCost.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
    },
    computedAmountOfProfitCost(){
      this.copyDetail.detailProfitAmount = new Decimal(this.copyDetail.detailTotalCost)
        .mul(
          this.copyDetail.detailProfitRate===0
            ?new Decimal(this.copyDetail.detailProfitRate)
            :new Decimal(this.copyDetail.detailProfitRate).div(new Decimal(100))
        )
      this.copyDetail.detailProfitAmount = new Decimal(this.copyDetail.detailProfitAmount.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
    },
    computedUntaxedCost(){
      this.copyDetail.systemComputeAmount =
        new Decimal(this.copyDetail.detailTotalCost)
          .add(new Decimal(this.copyDetail.detailProfitAmount));
      this.copyDetail.systemComputeAmount = new Decimal(this.copyDetail.systemComputeAmount.toFixed(6,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.finalUntaxedPrice =
        new Decimal(this.copyDetail.systemComputeAmount)
        .div(new Decimal(this.copyDetail.quotationDetailQuantity));
      this.copyDetail.finalUntaxedPrice = new Decimal(this.copyDetail.finalUntaxedPrice.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
    },
    computedTaxedCost(){
      this.copyDetail.systemComputePrice =
        new Decimal(this.copyDetail.systemComputeAmount)
          .add(new Decimal(this.copyDetail.systemComputeAmount)
            .mul(
              this.copyDetail.taxRate===0
                ?new Decimal(this.copyDetail.taxRate)
                :new Decimal(this.copyDetail.taxRate).div(new Decimal(100))
            )
          );
      this.copyDetail.systemComputePrice = new Decimal(this.copyDetail.systemComputePrice.toFixed(6,Decimal.ROUND_HALF_UP)).toNumber()
      this.copyDetail.finalTaxedPrice =
        new Decimal(this.copyDetail.systemComputePrice)
          .div(new Decimal(this.copyDetail.quotationDetailQuantity));
      this.copyDetail.finalTaxedPrice = new Decimal(this.copyDetail.finalTaxedPrice.toFixed(4,Decimal.ROUND_HALF_UP)).toNumber()
    }
  }
}
</script>

<template>
  <div>
    <div>
      <el-button type="primary" :loading="loadingStatus" @click="computeCost"> 计 算 </el-button>
    </div>
    <!--系统自动计算结果-->
    <el-form :model="copyDetail" ref="costForm" :rules="rules" label-position="top" label-width="120px">
      <fieldset
        style="height:80px;margin-top: 2px;">
        <legend>系统自动计算结果(料工费/工具)</legend>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="材料成本:" prop="computePartCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.computePartCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="机器成本:" prop="computeMachineCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.computeMachineCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="制造费用成本:" prop="computeFabricateCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.computeFabricateCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="人工成本:" prop="computeLabourCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.computeLabourCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="工具成本:" prop="computeToolCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.computeToolCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </fieldset>
      <fieldset
        style="height:80px;margin-top: 5px;">
        <legend>调整后成本(料工费/工具)</legend>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="材料成本:" prop="adjustPartCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.adjustPartCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="机器成本:" prop="adjustMachineCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.adjustMachineCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="制造费用成本:" prop="adjustFabricateCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.adjustFabricateCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="人工成本:" prop="adjustLabourCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.adjustLabourCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="工具成本:" prop="adjustToolCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.adjustToolCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
      </fieldset>
      <fieldset
        style="height:80px;margin-top: 5px;">
        <legend>其他成本</legend>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="管理成本:" prop="detailManageCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.detailManageCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="包装成本:" >
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyPackCost" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="运输成本:" >
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyShippingCost" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="其他成本:" prop="detailOtherCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                               v-model="copyDetail.detailOtherCost" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="总成本:" prop="detailTotalCost">
              <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                               v-model="copyDetail.detailTotalCost" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </fieldset>
      <fieldset style="height:80px;margin-top: 5px;">
        <legend>利润</legend>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item label="利润率%:" prop="detailProfitRate">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                                 v-model="copyDetail.detailProfitRate" :step="0" :min="0"/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="利润额:" prop="detailProfitAmount">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                                 v-model="copyDetail.detailProfitAmount" :step="0" :precision="4" :min="0" disabled/>
              </el-form-item>
            </el-col>
          </el-row>
      </fieldset>
      <fieldset style="height:80px;margin-top: 5px;">
        <legend>最终价格</legend>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item label="未税总额:" prop="systemComputeAmount">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                                 v-model="copyDetail.systemComputeAmount" :step="0" :precision="4" :min="0" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="未税单价:" prop="finalUntaxedPrice">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                                 v-model="copyDetail.finalUntaxedPrice" :step="0" :min="0" :precision="6" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="税率%:" prop="taxRate">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false" @blur="computeAllCost"
                                 v-model="copyDetail.taxRate" :step="0" :min="0"/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="含税总额:" prop="systemComputePrice">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                                 v-model="copyDetail.systemComputePrice" :step="0" :precision="4" :min="0" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="含税单价:" prop="finalTaxedPrice">
                <el-input-number style="width: 100%;margin-top: -5px;" :controls="false"
                                 v-model="copyDetail.finalTaxedPrice" :step="0" :precision="6" :min="0" disabled/>
              </el-form-item>
            </el-col>
          </el-row>
      </fieldset>
    </el-form>
  </div>
</template>

<style scoped>
.el-input-number /deep/ .el-input__inner {
  text-align: right;
  padding-right: 5px !important;
}

form fieldset{
  border: 1px solid #000;
}
</style>
