<script>
import {queryQuoteGroupDetailByInquiry} from "../../../../api/quote/quoteGroupDetail";

export default {
  name: "projectPartQuote",
  props:{
    part:{
      type: Object,
      required: true
    },
    height:{
      type: [String,Number],
      default: '26vh'
    },
    isExport:{
      type: Boolean,
      default: false
    }
  },
  data(){
    return{
      dataList:[],
      exportData: [],
      exportName: '项目报价'+this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ["项目报价"],
      exportFooter: [],
      queryLoading:false,
      columns:[
        {
          userId: this.$store.state.user.name,
          functionId: 101002002,
          serialNumber: '101002002Table2QuoteVersionNo',
          tableId: '101002002Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteVersionNo',
          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: 101002002,
          serialNumber: '101002002Table2QuoteDate',
          tableId: '101002002Table2',
          tableName: '报价详情信息表',
          columnProp: 'quoteDate',
          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: 101002002,
          serialNumber: '101002002Table2PartNo',
          tableId: '101002002Table2',
          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: 101002002,
          serialNumber: '101002002Table2PLMPartNo',
          tableId: '101002002Table2',
          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: 101002002,
          serialNumber: '101002002Table2PartDesc',
          tableId: '101002002Table2',
          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: 101002002,
          serialNumber: '101002002Table2Qty',
          tableId: '101002002Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: 102001,
          serialNumber: '102001Table2Qty',
          tableId: '102001Table2',
          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: '102001Table2Qty',
          tableId: '102001Table2',
          tableName: '报价详情信息表',
          columnProp: 'unitPrice',
          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: '102001Table2Qty',
          tableId: '102001Table2',
          tableName: '报价详情信息表',
          columnProp: 'unitPriceRate',
          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: 101002002,
          serialNumber: '101002002Table2Remark',
          tableId: '101002002Table2',
          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: 101002002,
          serialNumber: '101002002Table2InsideInquiryNo',
          tableId: '101002002Table2',
          tableName: '报价详情信息表',
          columnProp: 'insideInquiryNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '询价单号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
      ],
    }
  },
  methods:{
    handleQueryQuote(){
      let params = {
        site:this.part.site,
        projectNo:this.part.projectId,
        partNo:this.part.testPartNo,
      }
      this.queryLoading = true
      queryQuoteGroupDetailByInquiry(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
      })
    },

    handleClick(row){
      if (this.$router.resolve(`/quote-index`).resolved.name === '404'){
        this.$alert('权限不足,访问失败', '警告', {confirmButtonText: '确定',});
      }else {
        this.$router.push({
          name:`quote-index`,
          params:{
            quoteVersionNo:row.quoteVersionNo,
            type:'project'
          },
        })
      }
    },
    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;
    },
  },
  watch:{
    part(newVal,oldVal){
      if (newVal){
        this.handleQueryQuote()
      }else {
        this.dataList = []
      }
    }
  }
}
</script>

<template>
  <div>
    <download-excel v-if="isExport"
                    :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>
    <el-table :data="dataList" style="width: 100%;margin-top: 5px" :height="height" v-loading="queryLoading" border>
      <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" >
          <div v-if="item.columnProp === 'quoteVersionNo'">
            <el-link v-if="!item.columnHidden" @click="handleClick(scope.row)"> {{ `${scope.row[item.columnProp]}` }}</el-link>
            <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
          </div>
          <div 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>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>

</style>
