<script>
import {queryQuoteDetailAllCost} from "../../../../../api/quote/quoteDetail";

export default {
  name: "quoteDetailCost",
  props: {
    quoteDetail: {
      type: Object,
      required: true
    },
  },
  model: {
    prop: "quoteDetail",
    event: "update",
  },
  data(){
    return {
      computeLoading:false,
      rules:{
        adjustPartCost: [
          { required: true, message: "请输入材料标准成本", trigger: ["blur","change"] }
        ],
        adjustBomUnYield: [
          { required: true, message: "请输入材料实际成本", trigger: ["blur","change"] }
        ],
        adjustMachineCost: [
          { required: true, message: "请输入机器成本", trigger: ["blur","change"] }
        ],
        adjustFabricateCost: [
          { required: true, message: "请输入制造费用成本", trigger: ["blur","change"] }
        ],
        adjustLabourCost: [
          { required: true, message: "请输入人工成本", trigger: ["blur","change"] }
        ],
        adjustToolCost: [
          { required: true, message: "请输入工具成本", trigger: ["blur","change"] }
        ],
        manageCost: [
          { required: true, message: "请输入管理成本", trigger: ["blur","change"] }
        ],
        otherCost: [
          { required: true, message: "请输入其他成本", trigger: ["blur","change"] }
        ],
        profitRate: [
          { required: true, message: "请输入利润率", trigger: ["blur","change"] }
        ],
        taxRate: [
          { required: true, message: "请输入税率", trigger: ["blur","change"] }
        ],
      },
    }
  },
  methods:{
    computeTotalCost(){
      this.quoteDetail.totalCost =
        this.quoteDetail.adjustPartCost +
        this.quoteDetail.adjustBomUnYield +
        this.quoteDetail.adjustQuoteCost +
        this.quoteDetail.adjustMachineCost +
        this.quoteDetail.adjustFabricateCost +
        this.quoteDetail.adjustLabourCost +
        this.quoteDetail.adjustToolCost +
        this.quoteDetail.manageCost +
        this.quoteDetail.otherCost;
      this.computeProfitAmount();
    },
    computeProfitAmount(){
      this.quoteDetail.profitAmount =
        this.quoteDetail.totalCost * (this.quoteDetail.profitRate / 100);
      this.computeTaxCost();
    },
    computeTaxCost(){
      this.quoteDetail.totalPrice =
        this.quoteDetail.totalCost + this.quoteDetail.profitAmount;
      this.quoteDetail.unitPrice =
        this.quoteDetail.totalPrice / this.quoteDetail.qty;
      this.quoteDetail.taxTotalPrice =
        this.quoteDetail.totalPrice + this.quoteDetail.totalPrice * (this.quoteDetail.taxRate / 100);
      this.quoteDetail.taxUnitPrice =
        this.quoteDetail.taxTotalPrice / this.quoteDetail.qty;
    },
    handleQueryAllCost(){
      let params = {
        ...this.quoteDetail
      }
      this.computeLoading = true
      queryQuoteDetailAllCost(params).then(({data})=>{
        if (data && data.code === 0){
          this.quoteDetail.partCost = data.row.unitQuotePrice;
          this.quoteDetail.adjustPartCost = data.row.unitQuotePrice;
          this.quoteDetail.bomUnYield = data.row.actualQuotePrice;
          this.quoteDetail.adjustBomUnYield = data.row.actualQuotePrice;
          this.quoteDetail.quoteCost = data.row.quoteCost;
          this.quoteDetail.adjustQuoteCost = data.row.quoteCost;
          this.quoteDetail.labourCost = data.row.labourCost;
          this.quoteDetail.adjustLabourCost = data.row.labourCost;
          this.quoteDetail.machineCost = data.row.machineCost;
          this.quoteDetail.adjustMachineCost = data.row.machineCost;
          this.quoteDetail.fabricateCost = data.row.manufactureCost;
          this.quoteDetail.adjustFabricateCost = data.row.manufactureCost;
          this.quoteDetail.toolCost = data.row.toolCost;
          this.quoteDetail.adjustToolCost = data.row.toolCost;
          this.quoteDetail.packCost = data.row.packCost;
          this.quoteDetail.shippingCost = data.row.shippingCost;
          this.quoteDetail.otherCost = data.row.otherCost;
        }else {
          this.$message.warning(data.msg);
        }
        this.computeLoading = false
      }).catch((error)=>{
        this.$message.error(error);
        this.computeLoading = false
      })
    }
  },
  watch:{
    'quoteDetail.adjustPartCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustPartCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustBomUnYield'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustPartCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustQuoteCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustQuoteCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustMachineCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustMachineCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustFabricateCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustFabricateCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustLabourCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustLabourCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.adjustToolCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.adjustToolCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.manageCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.manageCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.otherCost'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.otherCost = 0;
      }
      this.computeTotalCost();
    },
    'quoteDetail.profitRate'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.profitRate = 0;
      }
      this.computeProfitAmount();
    },
    'quoteDetail.taxRate'(newValue, oldValue){
      if (newValue === undefined || newValue === null){
        this.quoteDetail.taxRate = 0;
      }
      this.computeTaxCost();
    },

  }
}
</script>

<template>
  <div>
    <el-button :loading="computeLoading" type="primary" :disabled="quoteDetail.status === '下达'" @click="handleQueryAllCost"> 计 算</el-button>
    <!--系统自动计算结果-->
    <el-form :model="quoteDetail" ref="costForm" :rules="rules" label-position="top" label-width="120px">
      <fieldset
        style="height:80px;margin-top: 2px;">
        <legend>系统自动计算结果(料工费/工具)</legend>
        <el-row :gutter="10" type="flex">
          <el-col :span="4">
            <el-form-item label="材料标准成本:" prop="partCost" :show-message="false">
              <el-input-number style="width: 100%; " :controls="false"
                               v-model="quoteDetail.partCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="材料实际成本:" prop="bomUnYield" :show-message="false">
              <el-input-number style="width: 100%; " :controls="false"
                               v-model="quoteDetail.bomUnYield" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="材料报价成本:" prop="bomUnYield" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.quoteCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="机器成本:" prop="machineCost" :show-message="false">
              <el-input-number style="width: 100%; " :controls="false"
                               v-model="quoteDetail.machineCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="制造费用成本:" prop="fabricateCost" :show-message="false">
              <el-input-number style="width: 100%; " :controls="false"
                               v-model="quoteDetail.fabricateCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="人工成本:" prop="labourCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.labourCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="工具成本:" prop="toolCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.toolCost" :precision="4" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </fieldset>
      <fieldset
        style="height:80px;margin-top: 5px;">
        <legend>调整后成本(料工费/工具)</legend>
        <el-row :gutter="10" type="flex">
          <el-col :span="4">
            <el-form-item label="材料标准成本:" prop="adjustPartCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustPartCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="材料实际成本:" prop="adjustBomUnYield" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustBomUnYield" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="材料报价成本:" prop="adjustBomUnYield" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustQuoteCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="机器成本:" prop="adjustMachineCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustMachineCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="制造费用成本:" prop="adjustFabricateCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustFabricateCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="人工成本:" prop="adjustLabourCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustLabourCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="工具成本:" prop="adjustToolCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.adjustToolCost" :disabled="quoteDetail.status === '下达'" :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="10" type="flex">
          <el-col :span="4">
            <el-form-item label="管理成本:" prop="manageCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.manageCost" :disabled="quoteDetail.status === '下达'" :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%;" :controls="false"
                               v-model="quoteDetail.packCost" :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%; " :controls="false"
                               v-model="quoteDetail.shippingCost" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="其他成本:" prop="otherCost" :show-message="false">
              <el-input-number style="width: 100%; " :controls="false"
                               v-model="quoteDetail.otherCost" :disabled="quoteDetail.status === '下达'" :step="0" :precision="4" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="总成本:" prop="totalCost" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.totalCost" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
        </el-row>
      </fieldset>
      <fieldset style="height:80px;margin-top: 5px;">
        <legend>利润</legend>
        <el-row :gutter="10" type="flex">
          <el-col :span="4">
            <el-form-item label="利润率%:" prop="profitRate" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.profitRate" :disabled="quoteDetail.status === '下达'" :step="0" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="利润额:" prop="profitAmount" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.profitAmount" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
        </el-row>
      </fieldset>
      <fieldset style="height:80px;margin-top: 5px;">
        <legend>最终价格</legend>
        <el-row :gutter="10" type="flex">
          <el-col :span="4">
            <el-form-item label="未税总额:" prop="totalPrice" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.totalPrice" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="未税单价:" prop="unitPrice" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.unitPrice" :step="0" :min="0" :precision="6" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="税率%:" prop="taxRate" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.taxRate" :disabled="quoteDetail.status === '下达'" :step="0" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="含税总额:" prop="taxTotalPrice" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.taxTotalPrice" :step="0" :precision="4" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="含税单价:" prop="taxUnitPrice" :show-message="false">
              <el-input-number style="width: 100%;" :controls="false"
                               v-model="quoteDetail.taxUnitPrice" :step="0" :precision="6" :min="0" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
          </el-col>
          <el-col :span="4">
          </el-col>
        </el-row>
      </fieldset>
    </el-form>
  </div>
</template>

<style scoped>

</style>
