<script>
import {
  updateQuoteDetail
} from "../../../../api/quote/quoteDetail";
import {getProjectPartList} from "../../../../api/project/project";
import QuoteDetailCost from "./primary/quoteDetailCost.vue";
import QuoteDetailTool from "./primary/quoteDetailTool.vue";
import QuoteDetailBom from "./primary/quoteDetailBom.vue";
import QuoteDetailRouting from "./primary/quoteDetailRouting.vue";
import QuoteDetailOtherCost from "./primary/quoteDetailOtherCost.vue";
import DictDataSelect from "../../sys/dict-data-select.vue";
import QuoteDetailTest from "./primary/quoteDetailTest.vue";
import QuoteDetailOther from "./primary/quoteDetailOther.vue";
import {
  queryQuoteGroupDetail,
  removeQuoteGroupDetail,
  saveQuoteGroupDetail, updateCurrentQuoteGroupDetailItemNo
} from "../../../../api/quote/quoteGroupDetail";
import QuoteDetail from "./quoteDetail.vue";

export default {
  name: "quoteGroupDetail",
  components: {
    QuoteDetail,
    QuoteDetailOther,
    QuoteDetailTest,
    DictDataSelect,
    QuoteDetailOtherCost,
    QuoteDetailRouting,
    QuoteDetailBom,
    QuoteDetailTool,
    QuoteDetailCost,
  },
  props:{
    quote:{
      type:Object,
      required:true
    },
    height:{
      type:[Number,String],
      default:300
    },
    authFlag:{
      type:Boolean,
      default:false
    }
  },
  data(){
    return{
      quoteDetail:{
        id:null,
        partNo:'',
        partDesc:'',
        projectNo:'',
        projectDesc:'',
        buNo: '*',
        qty:null,
        quoteCount:1,
        partCost:0,
        adjustPartCost:0,
        labourCost:0,
        adjustLabourCost:0,
        fabricateCost:0,
        adjustFabricateCost:0,
        toolCost:0,
        adjustToolCost:0,
        machineCost:0,
        adjustMachineCost:0,
        otherCost:0,
        manageCost:0,
        totalCost:0,
        profitRate:0,
        profitAmount:0,
        totalPrice:0,
        unitPrice:0,
        taxRate:13,
        taxTotalPrice:0,
        taxUnitPrice:0,
        quoteTotalCost:0,
        quoteProfitRate:0,
        quoteProfitAmount:0,
        quoteTotalPrice:0,
        quoteUnitPrice:0,
        quoteTaxRate:0,
        quoteTaxTotalPrice:0,
        quoteTaxUnitPrice:0,
        currency1:undefined,
        currency2:undefined,
        exchangeRate1:undefined,
        exchangeRate2:undefined,
        currencyTotalCost1:0,
        currencyTotalCost2:0,
        quoteCurrencyTotalCost1:0,
        quoteCurrencyTotalCost2:0,
        // moq:'Standard 250K',
        moq:'',
        calculatedItems:'工具,测试,其他,包装,运输',
        testCost:0,
        adjustTestCost:0,
        elseCost:0,
        adjustElseCost:0,
        remark:'',
      },
      saveQuoteDetail:{

      },
      dataList:[],
      saveLoading:false,
      queryLoading:false,
      saveVisible:false,
      saveQuoteDetailRules:{
        partNo: [{required: true, message: '请输入物料编码', trigger: ['blur','change']}],
        partDesc: [{required: true, message: '请输入物料名称', trigger: ['blur','change']}],
        qty: [{required: true, message: '请输入数量', trigger: ['blur','change']}],
        currency1: [{required: true, message: '请选择币种', trigger: ['blur','change']}],
      },
      columns: [
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2PLMPartNo',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'plmPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: 'PLM物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2PartNo',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'ifsPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: 'IFS物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2PartDesc',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'partDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '物料名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 240
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'qty',
          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: 5011,
          serialNumber: '5011Table2AdjustPartCost',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustPartCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '材料成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 130
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2AdjustLabourCost',
          tableId: '5011Table2',
          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: 5011,
          serialNumber: '5011Table2AdjustMachineCost',
          tableId: '5011Table2',
          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: 5011,
          serialNumber: '5011Table2AdjustToolCost',
          tableId: '5011Table2',
          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: 5011,
          serialNumber: '5011Table2AdjustTestCost',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustTestCost',
          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: 5011,
          serialNumber: '5011Table2AdjustElseCost',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'adjustElseCost',
          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: 5011,
          serialNumber: '5011Table2TotalCost',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'totalCost',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '总成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2QuoteProfitRate',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteProfitRate',
          headerAlign: 'center',
          align: 'right',
          columnLabel: 'VA%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2QuoteProfitAmount',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteProfitAmount',
          headerAlign: 'center',
          align: 'right',
          columnLabel: 'Contribution%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2QuoteTaxRate',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteTaxRate',
          headerAlign: 'center',
          align: 'right',
          columnLabel: 'Margin%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2QuoteTaxTotalPrice',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteTaxTotalPrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: 'Price ¥(ex VAT)',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'unitPrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '未税单价',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'taxRate',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '税率',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'exchangeRate1',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '其他税率',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'taxUnitPrice',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '含税单价(CNY)',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'currencyDesc2',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '币种',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'exchangeRate2',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '汇率',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Qty',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'currencyTotalCost2',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '含税单价',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2Remark',
          tableId: '5011Table2',
          tableName: '报价详情信息表',
          columnProp: 'remark',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '备注',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
      ],
      partVisible:false,
      activeName:'bom',

      projectPart:{

      },
      projectPartList:[],
      pageNo:1,
      pageSize:20,
      pageTotal:0,
      partQueryLoading:false,

      projectPartColumns:[
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table3TestPartNo',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'testPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table3PartDesc',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'partDesc',
          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: 5011,
          serialNumber: '5011Table3FinalPartNo',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'finalPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: 'IFS物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table3CustomerPartNo',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'customerPartNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '客户物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table3Type',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'type',
          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: 5011,
          serialNumber: '5011Table3UmId',
          tableId: '5011Table3',
          tableName: '项目物料表',
          columnProp: 'umId',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '单位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
      ],

      drawerVisible: false,
      quoteGroupDetail:{
        id:-1,
      },
    }
  },
  methods:{
    handleSaveQuoteDetail(row){
      this.$nextTick(()=>{
        if (this.$refs.handleSaveQuoteDetailClick){
          this.$refs.saveQuoteDetailForm.clearValidate();
        }
      })
      if (row){
        this.saveQuoteDetail = {
          ...row
        }
      }else {
        this.saveQuoteDetail = {
          ...this.quoteDetail,
          createBy:this.$store.state.user.name,
          status:'草稿',
          active:'Y',
          qty:1,
          isDetail:false,
        }
        this.$nextTick(()=>{
          this.saveQuoteDetail.projectNo = this.quote.projectNo
        })
      }
      this.activeName = 'bom';
      this.saveVisible = true;
    },
    handleDeleteQuoteDetail(row){
      this.$alert('确认删除该条报价明细吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: row.id,
        }
        removeQuoteGroupDetail(params).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg);
            this.handleQueryQuoteDetail();
          } else {
            this.$message.warning(data.msg);
          }
        }).catch((error) => {
          this.$message.error(error);
        })
      }).catch(() => {
      })
    },
    handlePartNoBlur(){
      let params = {
        site:this.$store.state.user.site,
        testPartNo:this.saveQuoteDetail.partNo,
        projectId: this.quote.projectNo,
        no:1,
        size:20,
      }
      getProjectPartList(params).then(({data})=>{
        if (data && data.code === 0){
          if (data.rows && data.rows.length === 1){
            this.dblClickProjectPartTable(data.rows[0])
          }else {
            this.saveQuoteDetail.partDesc = ''
          }
        }
      })
    },
    handleQueryQuoteDetail(val){
      let params = {
        quoteId: this.quote.id,
      }
      if (!val){
        this.queryLoading = true;
      }
      queryQuoteGroupDetail(params).then(({data})=>{
        if (data && data.code === 0){
          this.dataList = data.rows
        }else {
          this.$message.warning(data.msg);
        }
        this.queryLoading = false;
      }).catch((error)=>{
        this.$message.error(error);
        this.queryLoading = false;
      })
    },
    handleSaveQuoteDetailClick(){
      this.$refs.saveQuoteDetailForm.validate((valid,obj) => {
        if (valid){
          if (this.saveQuoteDetail.id){
            this.handleUpdate();
          }else {
            this.handleSave();
          }
        }else {
          let i = 1;
          for (let key in obj) {
            this.$message.error(obj[key][0].message);
            if (i === 1){
              return
            }
            i++;
          }
        }
      })
    },
    handleSave(){
      let params = {
        ...this.saveQuoteDetail,
        quoteId: this.quote.id,
        quoteNo:this.quote.quoteNo,
        site:this.quote.site,
        buNo:this.quote.buNo,
        versionNo:this.quote.versionNo,
        createBy:this.saveQuoteDetail.createBy,
        active:'Y',
        status:'草稿',
        internalInquiryNo:this.quote.insideInquiryNo,
      }
      this.saveLoading = true;
      saveQuoteGroupDetail(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg);
          this.handleQueryQuoteDetail();
          if (this.saveQuoteDetail.isDetail){
            this.saveQuoteDetail = {
              ...data.row,
            }
          }else {
            this.saveVisible = false;
          }
        }else {
          this.$message.warning(data.msg);
        }
        this.saveLoading = false
      }).catch((error)=>{
        this.$message.error(error);
        this.saveLoading = false
      })
    },
    handleUpdate(){
      let params = {
        ...this.saveQuoteDetail,
        updateBy:this.$store.state.user.name,
      }
      updateQuoteDetail(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg);
          this.handleQueryQuoteDetail();
          this.saveVisible = false;
        }else {
          this.$message.warning(data.msg);
        }
      }).catch((error)=>{
        this.$message.error(error);
      })
    },
    handleClickTab(tab){
      if (this.activeName === 'routing'){
        this.$refs.routing.handleQueryQuoteDetailBomTree()
      }else if (this.activeName === 'tool'){
        this.$refs.tool.handleQueryQuoteDetailTool();
      }
    },
    handleQueryPartList(){
      this.projectPart = {
        testPartNo:this.saveQuoteDetail.partNo,
        partDesc:'',
        finalPartNo:'',
        customerPartNo:'',
        site:this.$store.state.user.site,
      }
      this.partVisible = true;
      this.getProjectPartList();
    },
    getProjectPartList(){
      let params = {
        ...this.projectPart,
        partType:'Manufactured',
        no:this.pageNo,
        size:this.pageSize,
      }
      params.projectId = this.quote.projectNo
      this.partQueryLoading = true;
      getProjectPartList(params).then(({data})=>{
        if (data && data.code === 0){
          this.projectPartList = data.rows;
          this.pageTotal = data.total;
        }
        this.partQueryLoading = false;
      }).catch((error)=>{
        this.partQueryLoading = false;
      })
    },
    handleSizeChange(val){
      this.pageSize = val;
      this.getProjectPartList();
    },
    handleCurrentChange(val){
      this.pageNo = val;
      this.getProjectPartList();
    },
    dblClickProjectPartTable(row){
      this.saveQuoteDetail.partNo = row.testPartNo;
      this.saveQuoteDetail.partDesc = row.partDesc;
      this.partVisible = false;
    },
    handleQueryQuoteDetailByDetailId(row){
      this.quoteGroupDetail = {
        ...row
      }
      this.drawerVisible = true;
    },
    rowStyle({row}){
      if (row.id === this.quoteGroupDetail.id){
        return {background:'#E8F7F6'}
      }
    },
    quoteGroupDetailRowClick(row){
      this.quoteGroupDetail = {
        ...row
      }
    },
    handleCurrentQuoteDetailItemNo(val){
      updateCurrentQuoteGroupDetailItemNo(this.quoteGroupDetail).then(({data})=>{
        if (data && data.code === 0){
          // this.$message.success(data.msg);
          this.handleQueryQuoteDetail(true);
        }else {
          this.$message.warning(data.msg);
          this.quoteGroupDetail.currentQuoteDetailItemNo = val;
        }
      }).catch((error)=>{
        this.$message.error(error);
        this.quoteGroupDetail.currentQuoteDetailItemNo = val;
      })
    },
    handleClose(){
      this.drawerVisible = false;
      this.saveVisible = false;
    },
    toPartMenu (partNo) {
      if (this.$router.resolve(`/part-partInformation`).resolved.name === '404') {
        this.$alert('权限不足,访问失败', '警告', {confirmButtonText: '确定',})
      } else {
        this.$router.push({name:`part-partInformation`,params:{partNo:partNo},})
      }
    },
  },
  watch:{
    quote(newVal,oldVal){
      if (newVal.id){
        this.quoteDetail.profitRate = this.quote.markup
        this.quoteDetail.quoteProfitRate = this.quote.markup
        this.handleQueryQuoteDetail();
      }else {
        this.dataList = [];
      }
    },
    "saveQuoteDetail.partNo"(newVal,oldVal){
      if (newVal){
        this.saveQuoteDetail.partNo = newVal.toUpperCase();
      }
    },
    partVisible(newVal,oldVal){
      if (newVal === false){
        this.partQueryLoading = false
        this.pageNo = 1;
        this.projectPartList = [];
      }
    }
  }
}
</script>

<template>
<div>
  <template v-if="isAuth('5011:group:save')">
    <el-button type="primary" v-if="!authFlag" :disabled="quote.status === '下达' || !quote.status" @click="handleSaveQuoteDetail(null)">新增</el-button>
  </template>
  <el-table v-loading="queryLoading" border :data="dataList" style="width: 100%;margin-top: 5px" :height="height">
    <el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
    <el-table-column
      v-for="(item,index) in columns" :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 === 'partNo'">
          <el-link style="cursor:pointer;" v-if="!item.columnHidden" @click="toPartMenu(scope.row.partNo)"> {{ scope.row[item.columnProp] }}</el-link>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </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-column label="操作" v-if="!authFlag" fixed="right" align="center" width="120">
      <template slot-scope="{row,$index}">
        <template v-if="isAuth('5011:group:remove')">
          <a type="text" v-if="row.status === '草稿'" @click="handleDeleteQuoteDetail(row)">删除</a>
        </template>
        <template v-if="isAuth('5011:group:list')">
          <a @click="handleQueryQuoteDetailByDetailId(row)">条目明细</a>
        </template>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog :title="`报价明细`" top="10vh" v-drag :visible.sync="saveVisible" append-to-body :width="`${saveQuoteDetail.id?1200:600}px`" :close-on-click-modal="false">
    <el-form :model="saveQuoteDetail" ref="saveQuoteDetailForm" :rules="saveQuoteDetailRules" label-position="top" v-if="!saveQuoteDetail.id">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="物料编码" prop="partNo" :show-message="false">
            <span slot="label">
                <a @click="handleQueryPartList">物料编码</a>
            </span>
            <el-input v-model="saveQuoteDetail.partNo" @change="handlePartNoBlur"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="物料描述" prop="partDesc" :show-message="false">
            <el-input v-model="saveQuoteDetail.partDesc" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="MOQ" prop="qty" :show-message="false">
            <el-input-number style="width: 100%;"  v-model="saveQuoteDetail.qty" :min="1" :step="0" :precision="0" :controls="false"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="币种" prop="currency1" :show-message="false">
            <dict-data-select style="width: 100%" v-if="saveVisible" dict-type="plm_customer_information_customer_customer_currency" v-model="saveQuoteDetail.currency1"></dict-data-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="" :show-message="false">
            <el-checkbox v-model="saveQuoteDetail.isDetail">保存进入报价页面</el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" class="auto" :show-message="false">
            <el-input type="textarea" v-model="saveQuoteDetail.remark" :autosize="{minRows: 3, maxRows: 3}"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-form v-else :model="saveQuoteDetail" ref="saveQuoteDetailForm" :rules="saveQuoteDetailRules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="物料编码" prop="partNo" :show-message="false">
            <el-input v-model="saveQuoteDetail.partNo" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料描述" prop="partDesc" :show-message="false">
            <el-input v-model="saveQuoteDetail.partDesc" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="MOQ" prop="qty" :show-message="false">
            <el-input-number v-model="saveQuoteDetail.qty" style="width: 100%;" :controls="false" :disabled="saveQuoteDetail.status === '下达'"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="币种" prop="currency1" :show-message="false">
            <dict-data-select :disabled="saveQuoteDetail.status === '下达'" style="width: 100%" dict-type="plm_customer_information_customer_customer_currency" v-model="saveQuoteDetail.currency1"></dict-data-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form-item label="备注" class="auto" :show-message="false">
            <el-input type="textarea" resize="none" :autosize="{minRows: 3, maxRows: 3}" v-model="saveQuoteDetail.remark" :disabled="saveQuoteDetail.status === '下达'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-tabs v-model="activeName" v-if="saveQuoteDetail.id" @tab-click="handleClickTab">
      <el-tab-pane label="材料" name="bom">
        <quote-detail-bom v-if="saveVisible" @close="handleClose" v-model:quoteDetail="saveQuoteDetail"></quote-detail-bom>
      </el-tab-pane>
      <el-tab-pane label="工艺" name="routing">
        <quote-detail-routing ref="routing" v-if="saveVisible" @close="handleClose" v-model:quoteDetail="saveQuoteDetail"></quote-detail-routing>
      </el-tab-pane>
      <el-tab-pane label="工具" name="tool">
        <quote-detail-tool ref="tool" v-if="saveVisible" v-model:quoteDetail="saveQuoteDetail"></quote-detail-tool>
      </el-tab-pane>
      <el-tab-pane label="测试" name="test">
        <quote-detail-test v-if="saveVisible" v-model:quoteDetail="saveQuoteDetail"></quote-detail-test>
      </el-tab-pane>
      <el-tab-pane label="包装&运输" name="otherCost">
        <quote-detail-other-cost v-if="saveVisible" v-model:quoteDetail="saveQuoteDetail"></quote-detail-other-cost>
      </el-tab-pane>
      <el-tab-pane label="其他" name="other">
        <quote-detail-other v-if="saveVisible" v-model:quoteDetail="saveQuoteDetail"></quote-detail-other>
      </el-tab-pane>
      <el-tab-pane label="成本&价格" name="cost">
        <quote-detail-cost v-if="saveVisible" ref="cost" v-model:quoteDetail="saveQuoteDetail"></quote-detail-cost>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="saveQuoteDetail.status === '草稿'" :loading="saveLoading" @click="handleSaveQuoteDetailClick">确 定</el-button>
      <el-button @click="saveVisible = false">取 消</el-button>
    </div>
  </el-dialog>

  <el-dialog v-drag title="物料信息" append-to-body :close-on-click-modal="false" :visible.sync="partVisible">
    <!--搜索条件-->
    <el-form :model="projectPart" ref="projectPartDataForm" :inline="true" label-position="top">
      <el-form-item label="物料编码" prop="testPartNo">
        <el-input v-model="projectPart.testPartNo" clearable/>
      </el-form-item>
      <el-form-item label="物料名称" prop="partDesc">
        <el-input v-model="projectPart.partDesc" clearable/>
      </el-form-item>
      <el-form-item label="IFS物料编码" prop="finalPartNo">
        <el-input v-model="projectPart.finalPartNo" clearable/>
      </el-form-item>
      <el-form-item label="客户物料编码" prop="customerPartNo">
        <el-input v-model="projectPart.customerPartNo" 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" v-loading="partQueryLoading" stripe border @row-dblclick="dblClickProjectPartTable" :data="projectPartList" ref="projectPartDataTable" :style="{marginTop:'10px'}">
      <el-table-column
        v-for="(item,index) in projectPartColumns" :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-pagination style="margin-top: 0"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="pageNo"
                   :page-sizes="[20, 50, 100, 200, 500]"
                   :page-size="pageSize"
                   :total="pageTotal"
                   layout="total,sizes, prev, pager, next, jumper">
    </el-pagination>
  </el-dialog>


  <el-dialog title="报价明细" v-drag :visible.sync="drawerVisible" top="10vh" width="1200px" append-to-body :close-on-click-modal="false">
    <el-table v-loading="queryLoading"
              @row-click="quoteGroupDetailRowClick" :row-style="rowStyle" border :data="dataList" style="width: 100%;margin-top: 5px;" :height="'30vh'">
      <el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
      <el-table-column
        v-for="(item,index) in columns" :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>
    <quote-detail
      v-if="drawerVisible"
      style="margin-top: 20px"
      :save-auth="isAuth('5011:detail:save')"
      @close="handleClose"
      :quote="quote"
      :quote-group-detail="quoteGroupDetail"
      :auth-flag="false"
      :height="'33vh'"
      @currentQuoteDetailItemNo="handleCurrentQuoteDetailItemNo">

    </quote-detail>
    <div slot="footer" class="dialog-footer">
      <el-button @click="drawerVisible = false">取 消</el-button>
    </div>
  </el-dialog>

<!--  <part-table v-if="saveVisible" v-model="partVisible" :is-page="true" :part-no="saveQuoteDetail.partNo" @dblclick="handleDblClick"></part-table>-->
<!--  <project-part-table v-if="saveVisible" v-model="partVisible" :project-no="saveQuoteDetail.projectNo" :part-no="saveQuoteDetail.partNo" @dblclick="handleDblClick"></project-part-table>-->
</div>
</template>

<style scoped>
.el-table /deep/ .cell{
  height: auto;
  line-height: 1.5;
}

.auto /deep/ .el-form-item__content{
  height: auto;
  line-height: 1.5;
}
</style>
