<script>
import {
  deleteQuotationDetail,
  insertQuotationDetail,
  searchQuotationDetailPageByHeaderId, updateQuotationDetail, updateQuoteDetailStatus
} from "../../../../api/quotation/quotationHeader";
import {getProjectPart, getProjectPartList} from "../../../../api/project/project";
import ToolQuotation from "./quotationDetail/toolQuotation.vue";
import PropertyTemplates from "./quotationDetail/propertyTemplates.vue";
import CostPrice from "./quotationDetail/costPrice.vue";
import BillOfMateriel from "./quotationDetail/billOfMateriel.vue";
import {Decimal} from "decimal.js";
import {searchQuoteBomHeader, searchQuoteBomList} from "../../../../api/quotation/quoteOfBom";
import QuoteRouting from "./quotationDetail/quoteRouting.vue";
import {searchQuoteRoutingHeader} from "../../../../api/quotation/quoteOfRouting";
import {queryQuoteDetailBom, queryQuoteDetailRouting} from "../../../../api/quotation/quote";
import QuoteDetailProperties from "./quotationDetail/quoteDetailProperties.vue";

export default {
  name:'quoteDetail',
  components: {QuoteDetailProperties, QuoteRouting, BillOfMateriel, CostPrice, PropertyTemplates, ToolQuotation},
  props:{
    header:{
      type:Object,
      request:true,
    },
    height:{
      type:[Number,String],
      default:'32vh',
    },
  },
  watch:{
    header:function (){
      this.initData()
    },
    update(){
      // 当bom触发修改版本时触发
      this.searchQuoteRoutingHeader();
    }
  },
  data(){
    const validateProductNo = (rule, value, callback) =>{
      if (this.quotationDetailFlag !== true && this.quotationLeadIntoDetailFlag !== true){
        return;
      }
      if (value === '' || value === null || value === undefined){
        callback(new Error(' '))
        return
      }
      let params = {...this.projectPartData}
      params.testPartNo = value
      getProjectPart(params).then(({data})=>{
        if (data && data.rows.length >= 1){
          callback()
        }else {
          this.projectPartData.testPartNo = undefined;
          this.$message.error("产品编码不存在!")
          callback(new Error(" "))
        }
      }).catch((error)=>{
        this.projectPartData.testPartNo = undefined
        callback(new Error(' '))
      })
    };
    return{
      allSearchFlagBom:false,
      allSearchFlagRouting:false,
      BOMCost:0,
      ToolCost:0,
      machineCost:0,
      laborCost:0,
      manufacturingCost:0,
      packInfoCost:0,
      shippingInfoCost:0,
      loadingStatus:false,
      update:0,
      index:'',
      insertQuotationDetailLoading:false,
      projectPartDialogFlag:false,
      quotationLeadIntoDetailFlag:false,
      initQuotationDetailLoading:false,
      quotationDetailFlag:false,
      editQuotationDetailLoading:false,
      no: 1,//当前页
      size: 50,//每页条数
      total: 0,//总条数
      tableData:[],
      projectPartTable:[],
      columnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ProductNo',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'productNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '产品编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 200
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ProductDesc',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'productDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '产品名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 200
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2QuotationDetailQuantity',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'quotationDetailQuantity',
          headerAlign: 'center',
          align: 'right',
          columnLabel: 'MOQ',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ComputePartCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'computePartCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '计算后材料成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ComputeMachineCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'computeMachineCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '计算后机器成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ComputeLabourCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'computeLabourCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '计算后人工成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ComputeFabricateCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'computeFabricateCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '计算后制造费用',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2ComputeToolCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'computeToolCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '计算后工具成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2AdjustPartCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustPartCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调整后材料成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2AdjustMachineCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustMachineCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调整后机器成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2AdjustLabourCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustLabourCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调整后人工成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2AdjustFabricateCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustFabricateCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调整后制造费用',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2AdjustToolCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustToolCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调整后工具成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2DetailOtherCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'detailOtherCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '其他成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2DetailManageCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'detailManageCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '管理成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2DetailTotalCost',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'detailTotalCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '总成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2DetailProfitRate',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'detailProfitRate',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '利润率%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2DetailProfitAmount',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'detailProfitAmount',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '利润额',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2FinalUntaxedPrice',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'finalUntaxedPrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '未税单价',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2TaxRate',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'taxRate',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '税率%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2FinalTaxedPrice',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'finalTaxedPrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '含税单价',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2SystemComputePrice',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'systemComputePrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '含税总价',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2QuotationDetailStatus',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'quotationDetailStatus',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '状态',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2Remark',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'remark',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '备注',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table2InternalInquiryNo',
          tableId: '102003Table2',
          tableName: '报价详情信息表',
          columnProp: 'internalInquiryNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '询价单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        // {
        //   userId: this.$store.state.user.name,
        //   functionId: 102003,
        //   serialNumber: '102003Table2QuotationDetailCount',
        //   tableId: '102003Table2',
        //   tableName: '报价详情信息表',
        //   columnProp: 'quotationDetailCount',
        //   headerAlign: 'center',
        //   align: 'right',
        //   columnLabel: '报价次数',
        //   columnHidden: false,
        //   columnImage: false,
        //   columnSortable: false,
        //   sortLv: 0,
        //   status: true,
        //   fixed: '',
        //   columnWidth: 120
        // },
      ],
      projectPartDetailList: [
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table3TestPartNo',
          tableId: '102003Table3',
          tableName: '项目物料表',
          columnProp: 'testPartNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '产品编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table3PartDesc',
          tableId: '102003Table3',
          tableName: '项目物料表',
          columnProp: 'partDesc',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '产品名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table3CustomerPartNo',
          tableId: '102003Table3',
          tableName: '项目物料表',
          columnProp: 'customerPartNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '客户料号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table3FinalPartNo',
          tableId: '102003Table3',
          tableName: '项目物料表',
          columnProp: 'finalPartNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '正式料号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102001,
          serialNumber: '102001Table3Type',
          tableId: '102001Table3',
          tableName: '项目物料表',
          columnProp: 'type',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '制造类型',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 102001,
          serialNumber: '102001Table3UmId',
          tableId: '102001Table3',
          tableName: '项目物料表',
          columnProp: 'umId',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '单位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
      ],
      //报价明细新增对象
      quotationDetail: {
        quotationDetailId: undefined,//主键
        quotationHeaderId: undefined,//父编号
        site: this.$store.state.user.site,// 工厂编号
        productNo: undefined,//产品编码
        productDesc: undefined,//产品名称
        quotationDetailQuantity: 1,//MOQ
        internalInquiryNo: undefined,//内部询价单号
        itemNo:0,//序号
        quotationDetailStatus: "草稿",//报价状态
        taxRate: 13,//税率
        remark: undefined,//备注
        flag:false
      },
      projectPartData:{
        site:this.$store.state.user.site,
        testPartNo:undefined,// 项目编号
        partDesc:undefined,// 项目名称
        finalPartNo:undefined,// 正式料号
        customerPartNo:undefined,// 客户料号
      },
      fullscreen:false,
      // 报价明细 参数列表
      quotationDetailColumns: [
        {label: "productDesc", value: "产品名称"},
        {label: "quotationDetailQuantity", value: "MOQ"},
        {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: "未税单价"},
        {label: "taxRate", value: "税率"},
      ],
      // 报价详情表单校验
      quotationDetailRules: {
        productNo: [{required: true,validator:validateProductNo,trigger: 'change'}],
        productDesc: [{required: true, message: ' ', trigger: ['change','blur']}],
        quotationDetailStatus: [{required: true, message: ' ', trigger: 'change'}],
        internalInquiryNo: [{required: true, message: ' ', trigger: ['change','blur']}],
        itemNo:[{required: true,  message: ' ', trigger: ['change','blur']}],
        quotationDetailQuantity: [{required: true,  message: ' ', trigger: ['change','blur']}],
      },
      activeName:'bom',
    }
  },
  methods:{
    // 报价详情size 改变
    detailSizeChange(val) {
      this.size = val;
      this.initData();
    },
    // 报价详情 页码
    detailCurrentChange(val) {
      this.no = val;
      this.initData();
    },
    // 查询报价明细
    initData() {
      let params = {
        no: this.no,
        size: this.size,
        quotationHeaderId: this.header.quotationHeaderId
      }
      this.tableData = [];
      this.initQuotationDetailLoading = true;
      searchQuotationDetailPageByHeaderId(params).then(({data}) => {
        this.initQuotationDetailLoading = false;
        if (data && data.code === 200) {
          this.tableData = data.data.records;
          this.total = data.data.total;
        }else {
          this.tableData = [];
          this.total = 0;
        }
      }).catch((error)=>{
        this.initQuotationDetailLoading = false;
      })
    },
    // 修改 报价详情
    editQuotationDetail(row,index) {
      if (index < 10){
        this.index = "00"+index;
      }else if (index%10 <= 10){
        this.index = "0"+index;
      }else if (index%100 <= 10){
        this.index = index
      }
      this.quotationDetail = JSON.parse(JSON.stringify(row));
      this.quotationDetailFlag = true;
    },
    // 删除 报价明细
    deleteQuotationDetailData(row){
      this.$confirm('确定要删除该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteQuotationDetail(row).then(({data})=>{
          if (data.code === 200){
            this.initData();
            this.$message.success(data.msg);
          }else {
            this.$message.error(data.msg);
          }
        })
      })
    },
    openInsertQuotationDetail() {
      this.quotationDetail.quotationHeaderId = this.header.quotationHeaderId;
      this.quotationDetail.internalInquiryNo = this.header.internalInquiryNo;
    },
    closeInsertQuotationDetail() {
      this.$refs['insertQuotationDetailForm'].resetFields();
      this.resetQuotationDetail();
    },
    resetQuotationDetail(){
      this.quotationDetail = {
        quotationDetailId: undefined,//主键
        quotationHeaderId: undefined,//父编号
        site: this.$store.state.user.site,// 工厂编号
        productNo: undefined,//产品编码
        productDesc: undefined,//产品名称
        quotationDetailQuantity: 1,//MOQ
        internalInquiryNo: undefined,//内部询价单号
        itemNo:0,//序号
        quotationDetailStatus: "草稿",//报价状态
        taxRate: 13,//税率
        remark: undefined,//备注
        flag:false
      };
    },
    insertQuotationDetailBtn() {
      this.$refs['insertQuotationDetailForm'].validate((validate, objects) => {
        if (validate) {
          this.insertQuotationDetailLoading = true;
          insertQuotationDetail(this.quotationDetail).then(({data}) => {
            this.insertQuotationDetailLoading = false;
            if (data.code === 200) {
              // 保存成功后
              if (this.quotationDetail.flag) {
                this.$nextTick(()=>{
                  this.quotationDetail = JSON.parse(JSON.stringify(data.data));
                })
                this.quotationDetailFlag = true;
              }else {
                this.quotationLeadIntoDetailFlag = false;
              }
              this.initData();
              this.$message.success(data.msg);
            } else {
              this.$message.error(data.msg);
            }
          }).catch((error)=>{
            this.insertQuotationDetailLoading = false;
          })
        } else {
          this.rulesValidateLabel(objects, this.quotationDetailColumns);
        }
      })
    },
    // 校验处理
    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
          }
        }
      }
    },
    closeProjectPartDialog(){
      this.projectPartData = {
        site:this.$store.state.user.site,
        testPartNo:undefined,// 项目编号
        partDesc:undefined,// 项目名称
        finalPartNo:undefined,// 正式料号
        customerPartNo:undefined,// 客户料号
      }
      this.projectPartTable = [];
    },
    openProjectPartDialog(){
      this.getProjectPartList();
    },
    getProjectPartList(){
      let params = {...this.projectPartData}
      params.projectId = this.header.projectId
      getProjectPartList(params).then(({data})=>{
        if (data && data.code === 0){
          this.projectPartTable = data.rows;
        }
      })
    },
    blurProjectHandle(){
      let params = {
        site:this.$store.state.user.site,
        testPartNo:this.quotationDetail.productNo,
        projectId: this.header.projectId
      }
      getProjectPartList(params).then(({data})=>{
        if (data && data.code === 0){
          if (data.rows && data.rows.length === 1){
            this.dblClickProjectPartTable(data.rows[0])
          }
        }
      })
    },
    // 项目物料双击事件
    dblClickProjectPartTable(row){
      this.quotationDetail.productNo = row.testPartNo;
      this.quotationDetail.productDesc = row.partDesc;
      this.projectPartDialogFlag = false;
    },
    closeDetailEditDialog(){
      this.activeName = 'bom'
      this.fullscreen = false;
      this.resetQuotationDetail();
      this.$refs.cost.restCostForm()

      // 关闭新增弹框
      this.quotationLeadIntoDetailFlag = false;
    },
    openQuotationDetail(){
      this.$nextTick(()=>{
        // this.$refs.property.getPropertyTemplatesItem();// 属性
        this.$refs.tool.initQuotationToolData();//刀具
        this.searchQuoteBomList();//bom
      })
    },
    searchQuoteRoutingHeader(){
      let params = {
        site:this.$store.state.user.site,
        quoteDetailId:this.quotationDetail.quotationDetailId,
        testPartNo:this.quotationDetail.productNo
      }
      searchQuoteRoutingHeader(params).then(({data})=>{
        if (data && data.code === 0){
          let row = data.row
          let arr = [];
          if (row){
            arr[0] = row.version;
            arr[1] = row.alternativeNo;
            arr[2] = row.routingType;
            arr[3] = row.partNo;
            arr[4] = row.id;
            arr[5] = row.parentId;
          }
          this.$refs.routing.setSelectRouting(arr);
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    searchQuoteBomList(){
      this.allSearchFlagBom = false;
      this.allSearchFlagRouting = false;
      let params = {
        site:this.$store.state.user.site,
        quoteDetailId:this.quotationDetail.quotationDetailId,
        testPartNo:this.quotationDetail.productNo
      }
      this.loadingStatus = true
      searchQuoteBomHeader(params).then(({data})=>{
        if (data && data.code === 0){
          let row = data.row
          let arr = [];
          if (row !== null){
            arr[0] = row.version;
            arr[1] = row.alternativeNo;
            arr[2] = row.bomType;
            arr[3] = row.partNo;
            arr[4] = row.id;
            arr[5] = row.parentId;
          }
          this.$refs.bom.setSelectBom(arr);
          this.searchQuoteRoutingHeader();// routing
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    saveQuotationDetail(){
      if (!this.$refs.cost.ruleCostForm()) {
        return
      }
      let params = this.$refs.cost.getQuotationDetail();
      this.editQuotationDetailLoading = true;
      updateQuotationDetail(params).then(({data}) => {
        this.editQuotationDetailLoading = false;
        if (data.code === 200) {
          this.quotationDetailFlag = false;
          this.initData();
          this.$message.success(data.msg);
        } else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
        this.editQuotationDetailLoading = false;
      })
      // this.$message.success(this.$refs.cost.getQuotationDetail())
      // this.quotationDetailFlag = false;
    },
    tabClick(tab){
      if (this.activeName === 'cost'){
        this.searchQuoteBomList();
        this.$refs.tool.initQuotationToolData();
      }else if (this.activeName === 'tool'){
        this.$refs.tool.initQuotationToolData();
      }
    },
    editQuotationDetailStatus(row,status){
      let params = {
        quotationDetailId:row.quotationDetailId,
        quotationDetailStatus:status
      }
      updateQuoteDetailStatus(params).then(({data}) => {
        if (data.code === 0) {
          this.initData();
          this.$message.success(data.msg);
        } else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      })
    },
    clickProjectHandle(){
      this.projectPartData.testPartNo = this.quotationDetail.productNo
      this.projectPartDialogFlag = true
    },
    handleToBom(row){
      let params = {
        quotationDetailId:row.quotationDetailId
      }
      queryQuoteDetailBom(params).then(({data})=>{
        if (data && data.code === 0){
          if (this.$router.resolve('part-bomManagement').resolved.name === '404') {
            this.$alert('权限不足,访问失败', '警告', {confirmButtonText: '确定',})
          } else {
            let result = {
              partNo:'',
              site:'',
            }
            if (!data.row){
              result.partNo = row.productNo
              result.site = row.site
            }else {
              result = {
                ...data.row
              }
            }
            this.$router.push({
              name:"part-bomManagement",
              params:{
                type:"quote",
                site:result.site,
                partNo: result.partNo,
                engChgLevel:result.version,
                bomType: result.bomType,
              }})
          }
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      })
    },
    handleToRouting(row){
      let params = {
        quotationDetailId:row.quotationDetailId
      }
      queryQuoteDetailRouting(params).then(({data})=>{
        if (data && data.code === 0){
          if (this.$router.resolve('part-routingManagement').resolved.name === '404') {
            this.$alert('权限不足,访问失败', '警告', {confirmButtonText: '确定',})
          } else {
            let result = {
              partNo:'',
              site:'',
            }
            if (!data.row){
              result.partNo = row.productNo
              result.site = row.site
            }else {
              result = {
                ...data.row
              }
            }
            this.$router.push({
              name:"part-routingManagement",
              params:{
                type:"quote",
                site:result.site,
                partNo: result.partNo,
                engChgLevel:result.version,
                routingType: result.routingType,
              }})
          }
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      })
    },
    handleDetail(row,index){
      if (index < 10){
        this.index = "00"+index;
      }else if (index%10 <= 10){
        this.index = "0"+index;
      }else if (index%100 <= 10){
        this.index = index
      }
      this.quotationDetail = {...row};
      this.quotationDetailFlag = true;
    },
    handleRouter(internalInquiryNo){
      if (this.$router.resolve('quotation-requestForQuote').resolved.name === '404') {
        this.$alert('权限不足,访问失败', '警告', {confirmButtonText: '确定',})
      } else {
        this.$router.push({
          name: 'quotation-requestForQuote',
          params:{
            quotationNo:internalInquiryNo,
          }
        })
      }
    }
  },

}
</script>

<template>
<div>
  <div style="margin-bottom: 10px;padding-left: 10px">
    <el-button type="primary" @click="quotationLeadIntoDetailFlag = true">新增</el-button>
  </div>
  <el-table v-loading="initQuotationDetailLoading" element-loading-text = "数据正在加载中"
            element-loading-spinner = "el-icon-loading" :data="tableData" :height="height" border>
    <el-table-column
      label="操作"
      fixed
      width="140" align="center">
      <template slot-scope="{row,$index}">
        <el-link style="cursor: pointer" v-if="row.quotationDetailStatus === '草稿'" @click="editQuotationDetail(row,$index+1)">编辑</el-link>
        <el-link style="cursor: pointer" v-if="row.quotationDetailStatus === '草稿'" @click="deleteQuotationDetailData(row)">删除</el-link>
<!--        <el-link style="cursor: pointer" v-if="row.quotationDetailStatus !== '草稿'" @click="handleDetail(row,$index)">详情</el-link>-->
        <el-dropdown trigger="click">
          <el-link style="cursor: pointer;font-size: 12px">
            更多
          </el-link >
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleToBom(row)">BOM</el-dropdown-item>
            <el-dropdown-item @click.native="handleToRouting(row)">Routing</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
    <el-table-column
      header-align="center"
      label="序号"
      width="55"
      align="center"
      type="index">
    </el-table-column>
    <el-table-column
      v-for="(item,index) in columnList" :key="index"
      :sortable="item.columnSortable"
      :prop="item.columnProp"
      :header-align="item.headerAlign"
      :show-overflow-tooltip="item.showOverflowTooltip"
      :align="item.align"
      :fixed="item.fixed===''?false:item.fixed"
      :min-width="item.columnWidth"
      :label="item.columnLabel">
      <template slot-scope="scope" >
        <template v-if="item.columnProp === 'internalInquiryNo'">
          <a @click="handleRouter(scope.row.internalInquiryNo)" v-if="scope.row[item.columnProp]">{{ scope.row[item.columnProp] }}</a>
        </template>
        <template v-else>
          <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp]}}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination style="margin-top: 0px"
                 @size-change="detailSizeChange"
                 @current-change="detailCurrentChange"
                 :current-page="no"
                 :page-sizes="[ 50, 100, 200, 500]"
                 :page-size="size"
                 :total="total"
                 layout="total,sizes, prev, pager, next, jumper">
  </el-pagination>

  <el-dialog title="新增报价明细" v-drag  @open="openInsertQuotationDetail" @close="closeInsertQuotationDetail"
             :visible.sync="quotationLeadIntoDetailFlag"  width="500px" :close-on-click-modal="false">
    <el-form :model="quotationDetail" ref="insertQuotationDetailForm" :rules="quotationDetailRules"
             label-position="top">
      <el-row :gutter="10">
        <el-col :span="10">
          <el-form-item label="产品编码" prop="productNo">
            <span slot="label" style="" @click="clickProjectHandle"><a>产品编码</a></span>
            <el-input v-model="quotationDetail.productNo" @blur="blurProjectHandle" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="产品名称" prop="productDesc">
            <el-input v-model="quotationDetail.productDesc" disabled clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-form-item label="MOQ" prop="quotationDetailQuantity">
            <el-input-number :min="1" v-model="quotationDetail.quotationDetailQuantity" :controls="false"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="">
            <el-checkbox v-model="quotationDetail.flag">保存进入报价界面</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" class="auto" style="display: block;min-height: 80px" prop="remark">
            <el-input type="textarea" resize="none" v-model="quotationDetail.remark" :autosize="{minRows: 3, maxRows: 3}"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="height: 30px;line-height: 30px">
      <el-button type="primary" :loading="insertQuotationDetailLoading" @click="insertQuotationDetailBtn">保 存</el-button>
      <el-button @click="quotationLeadIntoDetailFlag = false">关 闭</el-button>
    </div>
  </el-dialog>
  <el-dialog v-drag :fullscreen="fullscreen" :visible.sync="quotationDetailFlag" @open="openQuotationDetail" @close="closeDetailEditDialog"
             width="60%" top="10vh" :close-on-click-modal="false">
    <div slot="title" class="medium">
      <div>
        <span class="el-dialog__title">报价单号 {{this.header.quotationNo+"-"+this.header.versionCode+"-"+index}}</span>
        <span style="float: right;margin-right: 30px;cursor:pointer;" @click="fullscreen = !fullscreen">
          <svg v-if="!fullscreen" t="1710144122404" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4774" width="16" height="16"><path d="M145.066667 85.333333h153.6c25.6 0 42.666667-17.066667 42.666666-42.666666S324.266667 0 298.666667 0H34.133333C25.6 0 17.066667 8.533333 8.533333 17.066667 0 25.6 0 34.133333 0 42.666667v256c0 25.6 17.066667 42.666667 42.666667 42.666666s42.666667-17.066667 42.666666-42.666666V145.066667l230.4 230.4c17.066667 17.066667 42.666667 17.066667 59.733334 0 17.066667-17.066667 17.066667-42.666667 0-59.733334L145.066667 85.333333z m170.666666 563.2L162.133333 802.133333l-76.8 76.8V725.333333C85.333333 699.733333 68.266667 682.666667 42.666667 682.666667s-42.666667 17.066667-42.666667 42.666666v256c0 25.6 17.066667 42.666667 42.666667 42.666667h256c25.6 0 42.666667-17.066667 42.666666-42.666667s-17.066667-42.666667-42.666666-42.666666H145.066667l76.8-76.8 153.6-153.6c17.066667-17.066667 17.066667-42.666667 0-59.733334-17.066667-17.066667-42.666667-17.066667-59.733334 0z m665.6 34.133334c-25.6 0-42.666667 17.066667-42.666666 42.666666v153.6l-76.8-76.8-153.6-153.6c-17.066667-17.066667-42.666667-17.066667-59.733334 0-17.066667 17.066667-17.066667 42.666667 0 59.733334l153.6 153.6 76.8 76.8H725.333333c-25.6 0-42.666667 17.066667-42.666666 42.666666s17.066667 42.666667 42.666666 42.666667h256c25.6 0 42.666667-17.066667 42.666667-42.666667v-256c0-25.6-17.066667-42.666667-42.666667-42.666666z m0-682.666667h-256c-25.6 0-42.666667 17.066667-42.666666 42.666667s17.066667 42.666667 42.666666 42.666666h153.6l-76.8 76.8-153.6 153.6c-17.066667 17.066667-17.066667 42.666667 0 59.733334 17.066667 17.066667 42.666667 17.066667 59.733334 0l153.6-153.6 76.8-76.8v153.6c0 25.6 17.066667 42.666667 42.666666 42.666666s42.666667-17.066667 42.666667-42.666666v-256c0-25.6-17.066667-42.666667-42.666667-42.666667z" fill="#8a8a8a" p-id="4775"></path></svg>
          <svg v-else t="1710143735808" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4603" width="16" height="16"><path d="M354.133333 682.666667H256v-42.666667h170.666667v170.666667H384v-98.133334L243.2 853.333333l-29.866667-29.866666L354.133333 682.666667z m358.4 0l140.8 140.8-29.866666 29.866666-140.8-140.8V810.666667h-42.666667v-170.666667h170.666667v42.666667h-98.133334zM354.133333 384L213.333333 243.2l29.866667-29.866667L384 354.133333V256h42.666667v170.666667H256V384h98.133333z m358.4 0H810.666667v42.666667h-170.666667V256h42.666667v98.133333L823.466667 213.333333l29.866666 29.866667L712.533333 384z" fill="#444444" p-id="4604"></path></svg>
        </span>
      </div>
    </div>
    <el-form :rules="quotationDetailRules" style="margin-top: 5px" ref="quotationDetailForm"
             :model="quotationDetail" label-width="80px" :inline="true" label-position="top">
      <el-form-item label="产品编码" prop="productNo">
        <el-input clearable disabled style="width: 200px" v-model="quotationDetail.productNo"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="productDesc">
        <el-input clearable disabled style="width: 240px" v-model="quotationDetail.productDesc"/>
      </el-form-item>
      <el-form-item label="MOQ" prop="quotationDetailQuantity">
        <el-input-number disabled style="width: 100%;margin-top: -5px;" :min="1" v-model="quotationDetail.quotationDetailQuantity" :controls="false"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" style="display: block;min-height: 90px;width: 63%">
        <el-input type="textarea" resize="none" disabled v-model="quotationDetail.remark" :autosize="{minRows: 3, maxRows: 3}"
                  style="min-height: 80px" />
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName" @tab-click="tabClick" :style="{height:`${fullscreen?740:500}px`}" border stripe>
      <el-tab-pane name="bom" label="材料">
        <bill-of-materiel ref="bom" :all-search-flag.sync="allSearchFlagBom" :total-cost.sync="BOMCost" :loading-status.sync="loadingStatus" :updateTree.sync="update" v-if="quotationDetailFlag" :height="fullscreen?655:415" :detail="quotationDetail"></bill-of-materiel>
      </el-tab-pane>
      <el-tab-pane name="routing" label="工艺">
        <quote-routing ref="routing" :machine-cost.sync="machineCost" :labor-cost.sync="laborCost" :manufacturing-cost.sync="manufacturingCost" :all-search-flag.sync="allSearchFlagRouting" v-if="quotationDetailFlag" :loading-status.sync="loadingStatus" :height="fullscreen?655:415" :detail="quotationDetail"></quote-routing>
      </el-tab-pane>
      <el-tab-pane name="tool" label="工具">
        <tool-quotation ref="tool" :total-cost.sync="ToolCost"  v-if="quotationDetailFlag" :height="fullscreen?660:420" :detail="quotationDetail"></tool-quotation>
      </el-tab-pane>
      <el-tab-pane name="property" label="其他成本">
        <quote-detail-properties :quotation-detail="quotationDetail" :pack-cost.sync="packInfoCost" :shipping-cost.sync="shippingInfoCost" v-if="quotationDetailFlag"></quote-detail-properties>
<!--        <property-templates ref="property" :pack-cost.sync="packInfoCost" :shipping-cost.sync="shippingInfoCost"  v-if="quotationDetailFlag" :height="fullscreen?660:420" :detail="quotationDetail" ></property-templates>-->
      </el-tab-pane>
      <el-tab-pane name="cost" label="成本&价格">
        <cost-price ref="cost" :tool-cost.sync="ToolCost"
                    :bom-cost.sync="BOMCost" :machine-cost.sync="machineCost"
                    :manufacturing-cost.sync="manufacturingCost"
                    :labor-cost.sync="laborCost"
                    :pack-cost.sync="packInfoCost" :shipping-cost.sync="shippingInfoCost"
                    :loading-status.sync="loadingStatus"
                    v-if="quotationDetailFlag"
                    :height="fullscreen?660:420"
                    :detail="quotationDetail"></cost-price>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" style="padding-top: 10px">
      <el-button type="primary" :loading="editQuotationDetailLoading" @click="saveQuotationDetail">确 定</el-button>
      <el-button @click=" quotationDetailFlag= false">取 消</el-button>
    </div>
  </el-dialog>

  <el-dialog v-drag title="产品列表" @close="closeProjectPartDialog" @open="openProjectPartDialog"
             :visible.sync="projectPartDialogFlag">
    <!--搜索条件-->
    <el-form :model="projectPartData" ref="projectPartDataForm" :inline="true" label-position="top">
      <el-form-item label="产品编号" prop="testPartNo">
        <el-input v-model="projectPartData.testPartNo" clearable/>
      </el-form-item>
      <el-form-item label="产品名称" prop="partDesc">
        <el-input v-model="projectPartData.partDesc" clearable/>
      </el-form-item>
      <el-form-item label="客户料号" prop="customerPartNo">
        <el-input v-model="projectPartData.customerPartNo" clearable/>
      </el-form-item>
      <el-form-item label="正式料号" prop="finalPartNo">
        <el-input v-model="projectPartData.finalPartNo" clearable/>
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="getProjectPartList" type="primary">查 询</el-button>
      </el-form-item>
    </el-form>
    <el-table height="300px" stripe border @row-dblclick="dblClickProjectPartTable" :data="projectPartTable" ref="projectPartDataTable" :style="{marginTop:'10px'}">
      <el-table-column
        v-for="(item,index) in projectPartDetailList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed===''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</div>
</template>

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