<script>
import QuoteSearch from "./primary/quoteSearch.vue";
import QuoteTable from "./primary/quoteTable.vue";
import {
  againQuote,
  queryQuotePage,
  queryQuoteRoleUser,
  removeQuote,
  saveQuote,
  updateQuote,
  updateQuoteStatus
} from "../../../api/quote/quote";
import QuoteDetail from "./detail/quoteDetail.vue";
import ChooseList from '@/views/modules/common/Chooselist';
import {queryCustomer} from "../../../api/customer/customerInformation";
import DictDataSelect from "../sys/dict-data-select.vue";
import {queryProjectByCustomer} from "../../../api/project/project";
import quotationCustomerContact from "../quotation/sellForQuotation/quotationCustomerContact.vue";
import CustomerInfo from "../quotation/sellForQuotation/customerInfo.vue";
import OssComponents from "../oss/ossComponents.vue";
import quotationProjectInformation from "../quotation/sellForQuotation/quotationProjectInformation.vue";
import {searchQuotationByQuotationNo} from "../../../api/quotation/quotationInformation";
import PriceCheckProperties from "../quotation/priceCheckProperties.vue";
import ApprovalInformation from "../changeManagement/approvalInformation.vue";
import {getPriceCheckPropertiesList} from "../../../api/quotation/priceCheckProperties";
import {getApprovalList} from "../../../api/changeManagement/changeManagement";
import QuoteGroupDetail from "./detail/quoteGroupDetail.vue";

export default {
  name: "quote",
  components: {
    QuoteGroupDetail,
    ApprovalInformation, PriceCheckProperties,
    quotationProjectInformation, OssComponents, CustomerInfo, quotationCustomerContact,
    DictDataSelect,
    QuoteDetail,
    ChooseList,
    QuoteTable, QuoteSearch
  },
  props:{
    isMenu:{
      type:Boolean,
      default:true,
    },
    projectNo:{
      type:String,
    },
    height:{
      type:[Number,String],
      default:'35vh'
    }
  },
  menuId:5011,
  data(){
    return{
      total: 0,
      no:1,
      size: 20,
      quote:{
        id: null,
        site:"",
        quoteNo: "",
        customerInquiryNo: "",
        insideInquiryNo: "",
        buNo: "",
        buId: null,
        versionNo: "",
        status:'',
        quoteVersionNo: "",
        customerNo: "",
        customerDesc: "",
        projectNo: "",
        projectDesc: "",
        finalCustomerNo: "",
        finalCustomerDesc:"",
        currency: "",
        quoteDate: "",
        quoter: "",
        quoterName: "",
        purchase: "",
        purchaseName: "",
        remark: "",
        createBy: "",
        createDate: "",
        updateBy: "",
        updateDate: "",
        application: "",
        annualDemand: "",
        costModel: "UFIDA",
        markup:0,
        chipPrice:'',
      },
      quoteForm:{
      },
      saveQuote:{
      },
      dataList:[],
      columns: [
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table1QuoteVersionNo',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'quoteVersionNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '报价单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 180
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table1QuoteDate',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'quoteDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '报价日期',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table1CustomerNo',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'customerNo',
          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: 5011,
          serialNumber: '5011Table1CustomerDesc',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'customerDesc',
          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: '5011Table1ProjectNo',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'projectNo',
          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: '5011Table1ProjectDesc',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'projectDesc',
          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: '5011Table1QuoterName',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'quoterName',
          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: '5011Table1PurchaseName',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'purchaseName',
          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: '5011Table1Status',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'status',
          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: 5011,
          serialNumber: '5011Table1CustomerInquiryNo',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'customerInquiryNo',
          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: 5011,
          serialNumber: '5011Table1InsideInquiryNo',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'insideInquiryNo',
          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: 5011,
          serialNumber: '5011Table1CreateBy',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'createBy',
          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: 5011,
          serialNumber: '5011Table1CreateDate',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 130
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table1UpdateBy',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'updateBy',
          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: 5011,
          serialNumber: '5011Table1UpdateDate',
          tableId: '5011Table1',
          tableName: '报价信息表',
          columnProp: 'updateDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '更新时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 130
        },
      ],
      searchLoading: false,
      saveVisible: false,
      saveLoading: false,
      activeName:'detail',
      userBuList: [],
      saveRules: {
        buId:[{required: true, message: '请选择BU', trigger: ['blur','change']}],
        customerNo:[{required: true, message: '请输入客户编码', trigger: ['blur','change']}],
        customerDesc:[{required: true, message: '请输入客户名称', trigger: ['blur','change']}],
        projectNo:[{required: true, message: '请输入项目编码', trigger: ['blur','change']}],
        projectDesc:[{required: true, message: '请输入项目名称', trigger: ['blur','change']}],
        currency:[{required: true, message: '请输入币种', trigger: ['blur','change']}],
        quoteDate:[{required: true, message: '请选择报价日期', trigger: ['blur','change']}],
        quoter:[{required: true, message: '请输入报价专员', trigger: ['blur','change']}],
      },
      customerVisible:false,
      projectVisible:false,
      quoterVisible:false,
      purchaseVisible:false,
      currentQuote:{

      },
      userVisible:false,
      userTitle:'人员信息',
      user:{
        username:'',
        userDisplay:'',
        active:'',
        buId:null,
        roleNo:'',
      },
      userDataList:[],
      tagNo:undefined,
      insideInquiryVisible:false,

      detailFlag:false,

      ossColumns:[
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2FileName',
          tableId: '5011Table2',
          tableName: '文件信息表',
          columnProp: 'fileName',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '文件名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2FileRemark',
          tableId: '5011Table2',
          tableName: '文件信息表',
          columnProp: 'fileRemark',
          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: '5011Table2CreateDate',
          tableId: '5011Table2',
          tableName: '文件信息表',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '上传时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5011,
          serialNumber: '5011Table2CreatedBy',
          tableId: '5011Table2',
          tableName: '文件信息表',
          columnProp: 'createBy',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '上传人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 140
        }
      ],

      insideInquiry:{

      },

      insideInquiryList:[],
      priceCheckPropertiesList:[],
      approvalList:[],

      exportData: [],
      exportName: '项目报价'+this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ["项目报价"],
      exportFooter: [],

      searchIndex:0,
    }
  },
  methods:{
    handleSearch(index){
      let params = {
        ...this.quoteForm,
        no: this.no,
        size: this.size,
      }
      this.searchLoading = true
      queryQuotePage(params).then(({data})=>{
        if (data && data.code === 0){
          this.dataList = data.rows
          this.total = data.total
          if (this.dataList[0] !== undefined){
            this.handleSelect(this.dataList[0])
          }
          // if (index !== undefined){
          //   this.handleSelect(this.dataList[index])
          // }
        }else {
          this.$message.error(data.msg)
        }
        this.searchLoading = false
      }).catch(error=>{
        this.$message.error(error)
        this.searchLoading = false
      })
    },
    handleSelect(row){
      if (row){
        this.currentQuote = {...row}
      }else {
        this.currentQuote = {}
      }
    },
    handleSave(row){
      this.$nextTick(()=>{
        if (this.$refs.saveForm){
          this.$refs.saveForm.clearValidate();
        }
      })
      if (row){
        this.saveQuote = {
          ...row
        }
      }else {
        this.saveQuote = {
          ...this.quote,
          buNo:'*',
          site:this.$store.state.user.site
        }
        this.$nextTick(()=>{
          this.saveQuote.buId = this.userBuList.length > 0? this.userBuList[0].id:null;
        })
      }
      this.saveVisible = true
    },
    handleSizeChange(size){
      this.size = size
      this.handleSearch();
    },
    handlePageChange(no) {
      this.no = no
      this.handleSearch();
    },
    handleQueryBu(){
      let params = {
        username: this.$store.state.user.name,
      }
      // getSiteAndBuByUserName(params).then(({data}) => {
      //   if (data && data.code === 0) {
      //     this.userBuList = data.rows
      //   }else {
      //     this.$message.warning(data.msg)
      //   }
      // }).catch((error)=>{
      //   this.$message.error(error)
      // })
    },
    handleSaveOrUpdateQuote(){
      this.$refs.saveForm.validate((valid,obj) => {
        if (valid){
          if (this.saveQuote.id){
            this.handleUpdateQuote();
          }else {
            this.handleSaveQuote();
          }
        }else {
          let i = 1;
          for (let key in obj){
            this.$message.warning(obj[key][0].message)
            if (i === 1){
              return
            }
            i++;
          }
        }
      })
    },
    handleSaveQuote(){
      let params = {
        ...this.saveQuote,
        status: '草稿',
        action: 'Y',
        createBy: this.$store.state.user.name,
      }
      this.saveLoading = true
      saveQuote(params).then(({data})=>{
        if (data && data.code === 0){
          this.saveVisible = false
          this.quoteForm.quoteVersionNo = data.quoteVersionNo
          this.$message.success(data.msg)
        }else {
          this.$message.warning(data.msg)
        }
        this.handleSearch(0);
        this.saveLoading = false
      }).catch((error)=>{
        this.$message.error(error)
        this.saveLoading = false
      })
    },
    handleUpdateQuote(){
      let params = {
        ...this.saveQuote,
        updateBy: this.$store.state.user.name,
      }
      this.saveLoading = true
      updateQuote(params).then(({data})=>{
        if (data && data.code === 0){
          this.saveVisible = false
          this.currentQuote = {
            ...this.currentQuote
          }
          this.$message.success(data.msg)
        }else {
          this.$message.warning(data.msg)
        }
        this.handleSearch();
        this.saveLoading = false
      }).catch((error)=>{
        this.$message.error(error)
        this.saveLoading = false
      })
    },
    handleRemove(row){
      this.$confirm('确认删除该报价信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRemoveQuote(row)
      }).catch(() => {

      })
    },
    handleUpdateStatus(row){
      let params = {
        ...row
      }
      updateQuoteStatus(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg)
          this.handleSearch();
          this.currentQuote = {
            ...row
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    handleQueryById(row){
      this.saveQuote = {
        ...row
      }
      this.detailFlag = true;
      this.saveVisible = true;
    },
    handleRemoveQuote(row){
      let params = {
        id: row.id,
      }
      removeQuote(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg)
          if (row.id === this.currentQuote.id){
            this.handleSearch(0);
          }else {
            this.handleSearch();
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    customerNoBlur(){
      let params = {
        site:this.$store.state.user.site,
        customerNo:this.saveQuote.customerNo
      }
      queryCustomer(params).then(({data})=>{
        if (data && data.code === 0 ) {
          if (data.rows && data.rows.length === 1){
            this.saveQuote.customerDesc = data.rows[0].customerDesc
          }else {
            this.saveQuote.customerDesc = ''
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    projectNoBlur(){
      let params = {
        site:this.$store.state.user.site,
        customerId:this.saveQuote.customerNo,
        projectId:this.saveQuote.projectNo
      }
      queryProjectByCustomer(params).then(({data})=>{
        if (data && data.code === 0 ){
          if (data.rows && data.rows.length === 1){
            this.saveQuote.projectDesc = data.rows[0].projectName
            this.saveQuote.finalCustomerNo = data.rows[0].finalCustomerId
            this.saveQuote.finalCustomerDesc = data.rows[0].finalCustomerName
          }else {
            this.saveQuote.projectDesc = ''
            this.saveQuote.finalCustomerNo = ''
            this.saveQuote.finalCustomerDesc = ''
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    handleRowClick(row){
      this.currentQuote = {...row}
    },
    userDblClick(row){
      if (this.quoterVisible){
        this.saveQuote.quoter = row.username
        this.quoterVisible = false
      }else if (this.purchaseVisible){
        this.saveQuote.purchase = row.username
        this.purchaseVisible = false
      }
      this.userVisible = false
    },
    handleQuoterClick(){
      this.userTitle = '报价专员'
      this.user.username = this.saveQuote.quoter
      this.user.userDisplay = ''
      this.user.active = ''
      this.user.buId = this.saveQuote.buId
      this.user.roleNo = 'QUOTER'
      this.quoterVisible = true
      this.handleQueryQuoteRoleUser();
      this.userVisible = true
    },
    handlePurchaseClick(){
      this.userTitle = '采购专员'
      this.user.username = this.saveQuote.purchase
      this.user.userDisplay = ''
      this.user.active = ''
      this.user.buId = this.saveQuote.buId
      this.user.roleNo = 'PURCHASE'
      this.purchaseVisible = true
      this.handleQueryQuoteRoleUser();
      this.userVisible = true
    },
    handleQueryQuoteRoleUser(){
      let params = {
        ...this.user,
      }
      this.userDataList = []
      queryQuoteRoleUser(params).then(({data})=>{
        if (data && data.code === 0){
          this.userDataList = data.rows
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    getBaseList(val){
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        let conSql = ''
        if (val === 102) {
          strVal = this.saveQuote.customerNo
        }
        if (val === 104) {
          strVal = this.saveQuote.projectNo
          conSql = " and customer_id = '" + this.saveQuote.customerNo + "'"
        }
        if (val === 2000) {
          strVal = this.saveQuote.purchase
          conSql = " and b.site = '" + this.$store.state.user.site + "'"
        }
        if (val === 2002) {
          strVal = this.saveQuote.quoter
          conSql = " and b.site = '" + this.$store.state.user.site + "'"
        }
        this.$refs.baseList.init(val, strVal, conSql)
      })
    },
    getBaseData(val){
      switch (this.tagNo){
        case 102:
          if (this.saveQuote.customerNo !== val.Customer_no) {
            this.saveQuote.projectNo = '';
            this.saveQuote.projectDesc = '';
          }
          this.saveQuote.customerNo = val.Customer_no
          this.saveQuote.customerDesc = val.Customer_desc
          break;
        case 104:
          this.saveQuote.projectNo = val.project_id
          this.saveQuote.projectDesc = val.project_name
          break;
        case 2000:
          this.saveQuote.purchase = val.username
          this.saveQuote.purchaseName = val.user_display
          break
        case 2002:
          this.saveQuote.quoter = val.username
          this.saveQuote.quoterName = val.user_display
          break;
      }
    },
    handleQueryByIds(){
      let params = {
        ids:this.$route.params.ids,
        no:this.no,
        size:this.size,
        createBy:this.$store.state.user.name,
      }
      queryQuotePage(params).then(({data})=>{
        if (data && data.code === 0){
          this.dataList = data.rows
          this.total = data.total
          if (this.total > 0){
            this.currentQuote = {...this.dataList[0]}
          }else {
            this.currentQuote = {}
          }
        }else {
          this.$message.error(data.msg)
        }
        this.searchLoading = false
      }).catch(error=>{
        this.$message.error(error)
        this.searchLoading = false
      })
    },
    handleInquiryBlur(){
      let params = {
        site:this.$store.state.user.site,
        quotationNo:this.saveQuote.insideInquiryNo,
      }
      searchQuotationByQuotationNo(params).then(({data}) => {
        if (data && data.code === 0){
          if (data.data.length === 1){
            this.dblclickInsideInquiry(data.data[0])
          }
        }
      })
    },
    handleAgainQuote(row){
      let params = {
        id: row.id,
        createBy: this.$store.state.user.name,
      }
      this.queryLoading = true
      againQuote(params).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success(data.msg)
          this.handleSearch();
        } else {
          this.$message.warning(data.msg)
          this.queryLoading = false
        }
      }).catch((error) => {
        this.$message.error(error)
        this.queryLoading = false
      })
    },
    clearModalData(field){
      this.$set(this.saveQuote,field,'')
    },
    handleQueryInquiry(){
      this.insideInquiry = {
        site:this.$store.state.user.site,
        quotationNo:this.saveQuote.insideInquiryNo,
        quotationBatchNo:'',
        quotationItemNo:'',
        testPartNo:'',
        projectName:'',
        finalCustomerName:'',
        quoterName:'',
        partName:'',
        quotationStatus:'审批中',
      }
      this.searchInsideInquiry();
      this.insideInquiryVisible = true
    },
    dblclickInsideInquiry(row){
      this.saveQuote.insideInquiryNo = row.quotationNo
      this.saveQuote.customerNo = row.customerNo
      this.saveQuote.customerDesc = row.customerDesc
      this.saveQuote.projectNo = row.projectId
      this.saveQuote.projectDesc = row.projectName
      this.saveQuote.quoter = row.quoter
      this.saveQuote.quoterName = row.quoterName
      this.saveQuote.finalCustomerNo = row.finalCustomerId
      this.saveQuote.finalCustomerDesc = row.finalCustomerName
      this.saveQuote.purchase = row.tracker
      this.saveQuote.purchaseName = row.trackerName
      this.insideInquiryVisible = false
    },
    searchInsideInquiry(){
      searchQuotationByQuotationNo(this.insideInquiry).then(({data}) => {
        if (data && data.code === 0){
          this.insideInquiryList = data.data;
        }
      })
    },
    getPriceCheckProperties() {
      if (!this.currentQuote && !this.currentQuote.insideInquiryNo){
        this.priceCheckPropertiesList = []
        return
      }
      let params = {
        site: this.saveQuote.site,
        quotationNo: this.currentQuote.insideInquiryNo
      }
      getPriceCheckPropertiesList(params).then(({data}) => {
        if (data && data.code === 0) {
          this.priceCheckPropertiesList = data.rows
        } else {
          this.$message.warning(data.msg)
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    // 查询审批信息
    getApprovalList () {
      let tempData = {
        site: this.$store.state.user.site,
        menuId: this.$route.meta.menuId,
        documentNo: this.currentQuote.quoteVersionNo
      }
      getApprovalList(tempData).then(({data}) => {
        if (data && data.code === 0) {
          this.approvalList = data.rows
        } else {
          this.approvalList = []
        }
      })
    },
    fields() {
      let json = "{"
      this.columns.forEach((item, index) => {
        if (index === this.columns.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      return eval("(" + json + ")")
    },
    createExportData() {
      return this.dataList;
    },
  },
  created() {
    if (this.isMenu && this.searchIndex === 0){
      this.quoteForm = {
        ...this.quote,
        site: this.$store.state.user.site,
        createBy: this.$store.state.user.name,
      }
      this.handleQueryBu();// 查询 BU
      this.handleSearch(0);// 查询报价信息
    }
    this.searchIndex++;
  },
  watch:{
    'quoteForm.customerNo'(newVal, oldVal){
      this.quoteForm.customerNo = newVal.toUpperCase()
    },
    'quoteForm.projectNo'(newVal, oldVal){
      this.quoteForm.projectNo = newVal.toUpperCase()
    },
    'quoteForm.quoteNo'(newVal, oldVal){
      this.quoteForm.quoteNo = newVal.toUpperCase()
    },
    'saveQuote.customerNo'(newVal, oldVal){
      if (newVal){
        this.saveQuote.customerNo = newVal.toUpperCase()
      }else {
        this.saveQuote.customerDesc = ''
        this.saveQuote.projectNo = ''
        this.saveQuote.projectDesc = ''
      }
    },
    'saveQuote.projectNo'(newVal, oldVal){
      if (newVal){
        this.saveQuote.projectNo = newVal.toUpperCase()
        this.projectNoBlur();
      }else {
        this.saveQuote.projectDesc = ''
        this.saveQuote.finalCustomerNo = ''
        this.saveQuote.finalCustomerDesc = ''
      }
    },
    'saveQuote.quoter'(newVal, oldVal){
      if (!newVal){
        this.saveQuote.quoterName = ''
      }
    },
    'saveQuote.purchase'(newVal, oldVal){
      if (!newVal){
        this.saveQuote.purchaseName = ''
      }
    },
    saveVisible(newVal, oldVal){
      if (newVal === false){
        this.detailFlag = false;
      }
    },
    'saveQuote.currency'(newVal, oldVal){
      this.saveQuote.currency = newVal.toUpperCase()
    },
    'currentQuote'(newVal, oldVal){
      if (newVal && this.isMenu){
        this.getPriceCheckProperties();
        this.getApprovalList();
      }
    },
    projectNo(newVal, oldVal){
      if (newVal && !this.isMenu){
        this.quoteForm.projectNo = newVal.toUpperCase()
        this.quoteForm.site = this.$store.state.user.site
        this.handleSearch(0);
      }else {
        this.dataList = []
        this.total = 0
        this.no = 1
      }
    },
  },
  activated() {
    if (!this.isMenu){
      return
    }
    if (this.$route.params.ids){
      this.handleQueryByIds();
    }else if (this.$route.params.type === 'project'){
      this.quoteForm = {
        ...this.quote,
        site: this.$store.state.user.site,
        quoteVersionNo: this.$route.params.quoteVersionNo,
      }
      this.handleSearch(0);
    }
  }
}
</script>

<template>
  <div>
    <download-excel v-if="!isMenu"
      :fields="fields()"
      :data="exportData"
      type="xls"
      :name="exportName"
      :header="exportHeader"
      :footer="exportFooter"
      :fetch="createExportData"
      worksheet="导出信息"
      class="el-button el-button--primary el-button--medium">
      {{ '导出' }}
    </download-excel>
    <quote-search v-if="isMenu" v-model:quote="quoteForm" @search="handleSearch" @save="handleSave"></quote-search>
    <quote-table v-loading="searchLoading"
                 :current-row="currentQuote"
                 :columns="columns"
                 style="margin-top: 5px"
                 :is-menu="isMenu"
                 @save="handleSave"
                 @remove="handleRemove"
                 @rowClick="handleRowClick"
                 @updateStatus="handleUpdateStatus"
                 @queryById="handleQueryById"
                 @againQuote="handleAgainQuote"
                 :data-list="dataList"
                 :height="height">

    </quote-table>
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handlePageChange"
                   :current-page="no"
                   :page-sizes="[20, 50, 100, 200, 500]"
                   :page-size="size"
                   :total="total"
                   layout="total,sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-tabs v-if="isMenu" v-model="activeName" type="border-card" style="margin-top: 0;" class="customer-tab">
      <el-tab-pane label="报价明细" name="detail">
        <quote-group-detail :quote="currentQuote" :auth-flag="false" :height="'28vh'"></quote-group-detail>
      </el-tab-pane>
      <el-tab-pane label="条目明细" name="detail2">
        <quote-detail v-if="activeName === 'detail2'" :save-auth="false" :is-export="true" :quote="currentQuote" :auth-flag="false" :height="'30vh'"></quote-detail>
      </el-tab-pane>
      <el-tab-pane label="项目信息" name="quotation_project_information">
        <quotation-project-information height="31vh" :quotation-header="currentQuote"></quotation-project-information>
      </el-tab-pane>
      <el-tab-pane label="客户信息" name="quotation_customer_information">
        <customer-info height="31vh" :project="currentQuote"></customer-info>
      </el-tab-pane>
      <el-tab-pane label="客户联系人" name="quotation_customer_contact">
        <quotation-customer-contact height="28vh" :quotation-header="currentQuote"></quotation-customer-contact>
      </el-tab-pane>
      <el-tab-pane label="报价-附件信息" name="quote_oss">
        <oss-components
          :save-visible="isAuth('5011:tab6:save')"
          :download-visible="isAuth('5011:tab6:download')"
          :remove-visible="isAuth('5011:tab6:remove')"
          :preview-visible="isAuth('5011:tab6:preview')"
          label="报价单号"
          height="28vh"
          :columns="ossColumns"
          :order-ref1="currentQuote.site?currentQuote.site:''"
          :order-ref2="currentQuote.quoteVersionNo?currentQuote.quoteVersionNo:''">
        </oss-components>
      </el-tab-pane>
      <el-tab-pane label="询价-基本信息" name="request">
        <price-check-properties ref="tabProperties"
                                v-model:data-list="priceCheckPropertiesList"
                                height="31vh"></price-check-properties>
      </el-tab-pane>
      <el-tab-pane label="询价-附件信息" name="oss">
        <oss-components
          :save-visible="isAuth('102001001:tab6:save')"
          :download-visible="isAuth('102001001:tab6:download')"
          :remove-visible="isAuth('102001001:tab6:remove')"
          :preview-visible="isAuth('102001001:tab6:preview')"
          label="询价单号"
          height="28vh"
          :columns="ossColumns"
          :order-ref1="currentQuote.site"
          :order-ref2="currentQuote.insideInquiryNo?currentQuote.insideInquiryNo.split('-')[0]:''">
        </oss-components>
      </el-tab-pane>
      <!-- 审批信息 -->
      <el-tab-pane label="审批信息" name="approvalInformation">
        <approval-information ref="approvalTable" v-model:data-list="approvalList" :height="300"></approval-information>
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="saveQuote.id? '报价信息:'+ saveQuote.quoteVersionNo : '报价信息'" v-drag :close-on-click-modal="false" :visible.sync="saveVisible" width="500px" >
      <el-form ref="saveForm" :model="saveQuote" :rules="saveRules" label-position="top" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="内部询价单号" prop="insideInquiryNo" :show-message="false">
              <span slot="label" v-if="!detailFlag">
                <a @click="handleQueryInquiry">内部询价单号</a>
              </span>
              <el-input v-model="saveQuote.insideInquiryNo" readonly @change="handleInquiryBlur" :disabled="detailFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报价日期" prop="quoteDate" :show-message="false">
              <el-date-picker style="width: 100%" :disabled="detailFlag" v-model="saveQuote.quoteDate" type="date" value-format='yyyy-MM-dd' format='yyyy-MM-dd'></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户询价单号" prop="customerInquiryNo" :show-message="false">
              <el-input v-model="saveQuote.customerInquiryNo" :disabled="detailFlag"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="客户编码" prop="customerNo" :show-message="false">
              <span slot="label" v-if="!detailFlag">
                <a @click="getBaseList(102)">客户编码</a>
              </span>
              <el-input v-model="saveQuote.customerNo" :disabled="detailFlag" @change="customerNoBlur"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="客户描述" prop="customerDesc" :show-message="false">
              <el-input v-model="saveQuote.customerDesc" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="项目编码" prop="projectNo" :show-message="false">
              <span slot="label" v-if="saveQuote.customerNo && !detailFlag">
                <a @click="getBaseList(104)">项目编码</a>
              </span>
              <el-input v-model="saveQuote.projectNo" :disabled="!saveQuote.customerNo || detailFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="项目描述" prop="projectDesc" :show-message="false">
              <el-input v-model="saveQuote.projectDesc" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="终端客户编码" prop="finalCustomerNo" :show-message="false">
              <el-input v-model="saveQuote.finalCustomerNo" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="终端客户描述" prop="finalCustomerDesc" :show-message="false">
              <el-input v-model="saveQuote.finalCustomerDesc" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="报价专员" prop="quoter" :show-message="false">
              <a slot="label" v-if="!detailFlag" @click="getBaseList(2002)">报价专员</a>
              <el-input v-model="saveQuote.quoterName" readonly :disabled="detailFlag">
                <span slot="suffix" v-show="saveQuote.quoter && !detailFlag" @click="clearModalData('quoter')"><i class="el-icon-circle-close" style="margin-left: 5px;cursor: pointer;"></i></span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购专员" prop="purchase" :show-message="false">
              <a slot="label" v-if="!detailFlag" @click="getBaseList(2000)">采购专员</a>
              <el-input v-model="saveQuote.purchaseName" readonly :disabled="detailFlag">
                <span slot="suffix" v-show="saveQuote.purchase && !detailFlag" @click="clearModalData('purchase')"><i class="el-icon-circle-close" style="margin-left: 5px;cursor: pointer;"></i></span>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark" class="auto" :show-message="false">
              <el-input v-model="saveQuote.remark" :disabled="detailFlag" :rows="3" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!detailFlag" @click="handleSaveOrUpdateQuote">确 定</el-button>
        <el-button @click="saveVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="询价申请" v-drag :visible.sync="insideInquiryVisible" width="1000px" modal-append-to-body :close-on-click-modal="false">
      <el-form label-position="top"
               :model="insideInquiry"
               size="mini">
        <el-row :gutter="10">
          <el-col :span="3">
            <el-form-item label="询价单号">
              <el-input v-model="insideInquiry.quotationNo" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="申请批次号">
              <el-input v-model="insideInquiry.quotationBatchNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="序号">
              <el-input v-model="insideInquiry.quotationItemNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="项目名称">
              <el-input v-model="insideInquiry.projectName" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="直接客户名称">
              <el-input v-model="insideInquiry.finalCustomerName" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="报价专员">
              <el-input v-model="insideInquiry.quoterName" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状态">
              <el-select v-model="insideInquiry.quotationStatus" style="width:100%">
                <el-option label="全部" value=""></el-option>
                <el-option label="草稿" value="草稿"></el-option>
                <el-option label="审批中" value="审批中"></el-option>
                <el-option label="已完成" value="已完成"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label=" ">
              <el-button type="primary" style="padding: 3px 12px" @click="searchInsideInquiry">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="insideInquiryList" height="300" stripe border @row-dblclick="dblclickInsideInquiry">
        <el-table-column prop="quotationNo" header-align="center" min-width="120" label="询价单号"/>
        <el-table-column prop="quotationBatchNo" header-align="center" label="询价批次号"/>
        <el-table-column width="60" align="center" header-align="center" prop="quotationItemNo" label="询价序号"/>
        <el-table-column prop="projectName" header-align="center" label="项目名称"/>
        <el-table-column prop="quotationStatus" header-align="center" label="状态"/>
        <el-table-column label="报价专员" header-align="center" prop="quoterName"/>
        <el-table-column label="直接客户编码" header-align="center" prop="finalCustomerId"/>
        <el-table-column label="直接客户名称" header-align="center" prop="finalCustomerName"/>
      </el-table>
      <el-footer style="height:30px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="insideInquiryVisible = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <choose-list ref="baseList" @getBaseData="getBaseData"></choose-list>
  </div>
</template>

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

</style>
