<template>
  <div class="customer-css">
    <!-- 查询条件 -->
    <el-form :inline="true" label-position="top" class="pi-search-form">
      <el-form-item :label="'报价单号'">
        <el-input v-model="searchData.orderNo" placeholder="报价单号" style="width:160px" @keyup.enter.native="getList" />
      </el-form-item>
      <el-form-item :label="'物料编码'">
        <el-input v-model="searchData.partNo" placeholder="物料编码" style="width:160px" @keyup.enter.native="getList" />
      </el-form-item>
      <el-form-item :label="'物料名称'">
        <el-input v-model="searchData.partDesc" placeholder="物料名称" style="width:200px" @keyup.enter.native="getList" />
      </el-form-item>
      <el-form-item :label="'报价日期'">
            <el-date-picker
              style="width: 130px"
              v-model="searchData.startDate"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="Start">
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="'To'">
            <el-date-picker
              style="width: 130px"
              v-model="searchData.endDate"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="End">
            </el-date-picker>
          </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" class="customer-bun-min" @click="getList">查询</el-button>
        <!-- <el-button type="primary" class="customer-bun-min" @click="openDialog()">新增报价</el-button> -->
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table :data="dataList" :height="height" border ref="mainTable" highlight-current-row v-loading="loading" style="width:100%" stripe>
      <el-table-column fixed="right" :label="'操作'" header-align="center" align="center" width="140">
        <template slot-scope="scope">
          <a class="customer-a" v-if="scope.row.status === '已报价'" @click="openDialog(scope.row)">审批</a>
          <a class="customer-a" v-if="scope.row.status === '已接受'" @click="openPartDialog(scope.row)">可供物料</a>
       
          <!-- <a class="customer-a" @click="handleDelete(scope.row)">删除</a> -->
        </template>
        
      </el-table-column>
      <el-table-column prop="status" :label="'状态'" min-width="100" header-align="center" align="center">
        <template slot-scope="scope">
          {{ getStatusLabel(scope.row.status) }}
          <!-- <el-tag :type="getStatusType(scope.row.status)" size="small">{{ getStatusLabel(scope.row.status) }}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" :label="'报价单号'" min-width="150" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="itemNo" :label="'序号'" min-width="80" header-align="center" align="center" />
      <el-table-column prop="seqNo" :label="'报价次数'" min-width="100" header-align="center" align="center" />
      <el-table-column prop="createdDate" :label="'报价时间'" min-width="160" header-align="center" align="center" />
      <el-table-column prop="supplierId" :label="'供应商编码'" min-width="120" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="supplierName" :label="'供应商名称'" min-width="150" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="partNo" :label="'物料编码'" min-width="140" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="partDesc" :label="'物料名称'" min-width="180" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="qty" :label="'数量'" min-width="100" header-align="center" align="right" />
      <el-table-column prop="umid" :label="'计量单位'" min-width="100" header-align="center" align="center" />
      <el-table-column prop="remark2" :label="'特殊要求'" min-width="150" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="materialCost" :label="'材料费用'" min-width="120" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.materialCost) }}
        </template>
      </el-table-column>
      <el-table-column prop="produceFee" :label="'加工费'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.produceFee) }}
        </template>
      </el-table-column>
      <el-table-column prop="surfaceFee" :label="'表面处理费'" min-width="120" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.surfaceFee) }}
        </template>
      </el-table-column>
      <el-table-column prop="profit" :label="'利润'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.profit) }}
        </template>
      </el-table-column>
      <el-table-column prop="price" :label="'未税单价'" min-width="120" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column prop="taxRate" :label="'税率(%)'" min-width="100" header-align="center" align="center" />
      <el-table-column prop="tax" :label="'含税单价'" min-width="120" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.tax) }}
        </template>
      </el-table-column>
      <el-table-column prop="duty" :label="'关税'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.duty) }}
        </template>
      </el-table-column>
      <el-table-column prop="purchaseCycle" :label="'采购周期(天)'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="mouldFee" :label="'模具费'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.mouldFee) }}
        </template>
      </el-table-column>
      <el-table-column prop="additionalCost" :label="'附加费用'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.additionalCost) }}
        </template>
      </el-table-column>
      <el-table-column prop="freight" :label="'运费'" min-width="100" header-align="center" align="right">
        <template slot-scope="scope">
          {{ formatMoney(scope.row.freight) }}
        </template>
      </el-table-column>
      <el-table-column prop="reason" :label="'原因'" min-width="150" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="remark" :label="'备注'" min-width="150" header-align="center" align="left" show-overflow-tooltip />
      <el-table-column prop="createdBy" :label="'报价人'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="wantReplyDate" :label="'要求报价时间'" min-width="160" header-align="center" align="center" />
      <el-table-column prop="buyer" :label="'采购员'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="paymentTerm" :label="'付款方式'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="deliveryTerm" :label="'交货条款'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="currency" :label="'货币'" min-width="80" header-align="center" align="center" />
      <el-table-column prop="contact" :label="'联系人'" min-width="120" header-align="center" align="center" />
      <el-table-column prop="phoneNo" :label="'联系电话'" min-width="130" header-align="center" align="center" />
      <el-table-column prop="faxNo" :label="'传真'" min-width="130" header-align="center" align="center" />
      <el-table-column prop="otherCondition" :label="'其他要求'" min-width="180" header-align="center" align="left" show-overflow-tooltip />
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top:10px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20,50,100,200,500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper" />

      
  <!-- 标签页 -->
    <el-tabs v-model="activeName" style="margin-top: 0px; width: 99%;" @tab-click="tabClick" class="customer-tab" type="border-card">
      <el-tab-pane label="附件" name="attachments">
        <attachments ref="attachments" :height="tabHeight"></attachments>
      </el-tab-pane>
    </el-tabs>

    <!-- 新增 / 编辑 弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px" :close-on-click-modal="false" v-drag>
      <el-form label-position="top" class="pi-form" :model="form" label-width="120px">
           <!-- 基础信息 -->
          <div class="form-section">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="物料编码" prop="partNo">
                  <el-input v-model="form.partNo" :disabled="true" placeholder="物料编码"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="18">
                <el-form-item label="物料名称" prop="partDesc">
                  <el-input v-model="form.partDesc" :disabled="true" placeholder="物料名称"></el-input>
                </el-form-item>
              </el-col>  
            </el-row>
            <el-row :gutter="20">
                <el-col :span="18">
                <el-form-item label="规格型号" prop="spec">
                  <el-input v-model="form.spec" :disabled="true" placeholder="规格型号"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="询价数量" prop="qty">
                  <el-input-number v-model="form.qty" :disabled="true" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="18">
                <el-form-item label="其他要求" prop="otherCondition">
                  <el-input v-model="form.otherCondition" type="text" :disabled="true" :rows="1" placeholder="其他要求"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="计量单位" prop="umid">
                  <el-input v-model="form.umid" :disabled="true" placeholder="计量单位"></el-input>
                </el-form-item>
              </el-col>   
            </el-row>        
          </div>

          <!-- 费用信息 -->
          <div class="form-section">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="材料费用" prop="materialCost">
                  <el-input-number v-model="form.materialCost"  :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="加工费" prop="produceFee">
                  <el-input-number v-model="form.produceFee"  :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="表面处理费" prop="surfaceFee">
                  <el-input-number v-model="form.surfaceFee" :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
                <el-col :span="6">
                <el-form-item label="利润" prop="profit">
                  <el-input-number v-model="form.profit"  :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>

             <!-- 价格信息 -->
          <div class="form-section">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="未税单价" prop="price">
                  <el-input-number v-model="form.price"  :disabled="true" :precision="4" :min="0" style="width: 100%" ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="税率(%)" prop="taxRate">
                  <el-input-number v-model="form.taxRate" :disabled="true" :precision="2" :min="0" :max="100" style="width: 100%" ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="含税单价" prop="tax">
                  <el-input-number v-model="form.tax"  :disabled="true" :precision="4" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
                <el-col :span="6">
                <el-form-item label="关税" prop="duty">
                  <el-input-number v-model="form.duty" :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
            <el-row :gutter="20">
               <el-col :span="6">
                <el-form-item label="采购周期(天)" prop="purchaseCycle">
                  <el-input-number v-model="form.purchaseCycle" :disabled="true" :min="0" :precision="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="模具费" prop="mouldFee">
                  <el-input-number v-model="form.mouldFee" :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="附加费用" prop="additionalCost">
                  <el-input-number v-model="form.additionalCost" :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
               <el-col :span="6">
                <el-form-item label="运费" prop="freight">
                  <el-input-number v-model="form.freight" :disabled="true" :precision="2" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>           
            </el-row>
          </div>

          <!-- 其他信息 -->
          <div class="form-section">
             <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="特殊要求" prop="remark2">
                  <el-input v-model="form.remark2" type="textarea" :disabled="true" :rows="1" placeholder="请输入特殊要求"></el-input>
                </el-form-item>
              </el-col>
             </el-row>
            <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" :disabled="true" :rows="1" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
            </el-row>

             <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item label="原因" prop="reason">
                <el-input v-model="form.reason" type="textarea"  :rows="1" placeholder="请输入原因"></el-input>
              </el-form-item>
            </el-col>
            </el-row>
          </div>
      
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: 20px">
        <el-button type="primary" @click="accept" >已接受</el-button>
        <el-button type="primary" @click="notAccept" >未接受</el-button>
        <el-button type="primary" @click="refuse" >已拒绝</el-button>
        
      </div>
    </el-dialog>

     <!-- 可供物料弹窗 -->
     <el-dialog :title="partDialogTitle" :visible.sync="partDialogVisible" width="800px" :close-on-click-modal="false" v-drag>
      <el-form label-position="top" class="pi-form" :model="partForm" label-width="120px">
        <!-- 基础信息 -->
        <div class="form-section">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="物料编码" prop="partNo">
                <el-input v-model="partForm.partNo" disabled placeholder="物料编码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物料名称" prop="partDesc">
                <el-input v-model="partForm.partDesc" disabled placeholder="物料名称"></el-input>
              </el-form-item>
            </el-col>  
            <el-col :span="6">
              <el-form-item label="主供应商" prop="isMainSupplier">
                <el-select v-model="partForm.isMainSupplier" placeholder="请选择" style="width: 100%">
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>  
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="供应商编码" prop="supplierCode">
                <el-input v-model="partForm.supplierCode" disabled placeholder="供应商编码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-input v-model="partForm.supplierName" disabled placeholder="供应商名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否在用" prop="active">
                 <el-select v-model="partForm.active" placeholder="请选择" style="width: 100%">
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="库存计量单位" prop="oldUmid">
                <el-input v-model="partForm.oldUmid" disabled placeholder="库存计量单位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="采购员" prop="buyer">
                <el-input v-model="partForm.buyer" disabled placeholder="采购员"></el-input>
              </el-form-item>
            </el-col>  
            <el-col :span="6">
              <el-form-item label="库存类型" prop="stockType">
                 <el-select v-model="partForm.stockType" placeholder="请选择" style="width: 100%">
                  <el-option label="正常库存" value="N"></el-option>
                  <el-option label="供应商库存" value="S"></el-option>
                  <el-option label="客户库存" value="C"></el-option>
                </el-select>
              </el-form-item>
            </el-col> 
          </el-row>        
        </div>

        <div class="form-section">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="采购计量单位" prop="purUmid">
                <el-input v-model="partForm.purUmid" disabled placeholder="采购计量单位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="供应商物料编码" prop="supplierPartNo">
                <el-input v-model="partForm.supplierPartNo" disabled placeholder="供应商物料编码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="税率" prop="taxCode">
                <el-input v-model="partForm.taxCode" disabled placeholder="税率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="换算系数" prop="convertFactor">
                <el-input-number v-model="partForm.convertFactor" :precision="2" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>

          <div class="form-section">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="标准包装量" prop="standardPackageQty">
                  <el-input-number v-model="partForm.standardPackageQty" :precision="0" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="含税价格" prop="price">
                  <el-input-number v-model="partForm.price" disabled :precision="4" :min="0" style="width: 100%"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="供应商联系人" prop="supplierContact">
                  <el-input v-model="partForm.supplierContact" placeholder="供应商联系人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="内部提前期" prop="internalLeadTime">
                  <el-input v-model="partForm.internalLeadTime" placeholder="内部提前期"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="附加费用" prop="additionalCost">
                <el-input-number v-model="partForm.additionalCost" :precision="2" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="采购提前期" prop="purLeadTime">
                <el-input-number v-model="partForm.purLeadTime" :precision="0" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="货币" prop="currency">
                <el-input v-model="partForm.currency" disabled placeholder="货币"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="最小订单量" prop="minOrderQty">
                <el-input-number v-model="partForm.minOrderQty" :precision="0" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>           
          </el-row>
        </div>

        <!-- 其他信息 -->
        <div class="form-section">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="partForm.remark" type="textarea" :rows="1" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="检验类型" prop="inspectType">
                <el-select v-model="partForm.inspectType" placeholder="请选择" style="width: 100%">
                  <el-option label="直接入库" value="1"></el-option>
                  <el-option label="检验后入库" value="2"></el-option>
                  <el-option label="接收到到货区" value="3"></el-option>
                  <el-option label="接收到到货区后检验" value="4"></el-option>
                </el-select>
               
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="检验方式" prop="inspectMethod">
                 <el-select v-model="partForm.inspectMethod" placeholder="请选择" style="width: 100%">
                  <el-option label="*" value="*"></el-option>
                  <el-option label="全检" value="F"></el-option>
                  <el-option label="数量抽检" value="Q"></el-option>
                  <el-option label="比例抽检" value="P"></el-option>
                  <el-option label="不固定抽检" value="V"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="抽检数量" prop="inspectQty">
                <el-input-number v-model="partForm.inspectQty" :precision="0" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="抽检比例" prop="inspectPercent">
                <el-input-number v-model="partForm.inspectPercent" :precision="2" :min="0" :max="100" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="检验水平" prop="inspectionLevelNo">
                <el-input v-model="partForm.inspectionLevelNo" placeholder="检验水平"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="自动关闭误差比例" prop="closeTolerance">
                <el-input-number v-model="partForm.closeTolerance" :precision="2" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="允许超订单量入库" prop="overReceiveFlag">
                <el-select v-model="partForm.overReceiveFlag" placeholder="请选择" style="width: 100%">
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="允许超订单比例" prop="overReceiveTolerance">
                <el-input-number v-model="partForm.overReceiveTolerance" :precision="2" :min="0" style="width: 100%"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: 20px">
        <el-button type="primary" @click="savePart">保存</el-button>
        <el-button @click="partDialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { 
    searchPurReplyHistPage,
    createPurReplyHist, 
    updatePurReplyHist, 
    deletePurReplyHist,
    updatePurReplyHistStatus
   } from '@/api/supplier/purReplyHist.js'

import {
  createPartSupplierRelation
} from '../../../api/part/partSupplierRelation'

import attachments from './com_inquiry_Attachments'

export default {
  name: 'MyQuotation',
  components: {
    attachments
  },
  data() {
    return {
      height: 200,
      loading: false,
      submitLoading: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      currentRow: {},
      activeName: 'attachments',
      searchData: {
        site: this.$store.state.user.site,
        createdBy: this.$store.state.user.name,
        orderNo: '',
        partNo: '',
        partDesc: '',
        status: ''
      },
     
      dialogVisible: false,
      dialogTitle: '新增报价',
      editMode: false,
      form: {
        id: '',
        status: 'pending',
        orderNo: '',
        itemNo: '',
        seqNo: '',
        createdDate: '',
        supplierId: '',
        supplierName: '',
        partNo: '',
        partDesc: '',
        qty: 0,
        umid: '',
        remark2: '',
        materialCost: 0,
        produceFee: 0,
        surfaceFee: 0,
        profit: 0,
        price: 0,
        taxRate: 13,
        tax: 0,
        duty: 0,
        purchaseCycle: 0,
        mouldFee: 0,
        additionalCost: 0,
        freight: 0,
        reason: '',
        remark: '',
        createdBy: this.$store.state.user.name,
        wantReplyDate: '',
        employeeName: '',
        paymentTerm: '',
        deliveryTerm: '',
        currency: 'CNY',
        contact: '',
        phoneNo: '',
        faxNo: '',
        otherCondition: '',
        actReplyComments: ''
      },
      partDialogVisible: false,
      partDialogTitle: '供应商可供物料',
      partForm: {           // 独立管理可供物料表单数据
        id: '',
        partNo: '',
        partDesc: '',
        isMainSupplier: 'N',
        supplierCode: '',
        supplierName: '',
        active: 'Y',
        oldUmid: '',
        buyer: '',
        stockType: 'N',
        purUmid: '',
        supplierPartNo: '',
        taxCode: '',
        convertFactor: 1,
        standardPackageQty: 0,
        price: 0,
        supplierContact: '',
        internalLeadTime: '',
        additionalCost: 0,
        purLeadTime: 0,
        currency: 'CNY',
        minOrderQty: 0,
        remark: '',
        inspectType: '1',
        inspectMethod: '*',
        inspectQty: 0,
        inspectPercent: 0,
        inspectionLevelNo: '',
        closeTolerance: 0,
        overReceiveFlag: 'N',
        overReceiveTolerance: 0
      }
    }
  },
  mounted() {
    this.calcHeight()
    window.addEventListener('resize', this.calcHeight)
    this.getList()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcHeight)
  },
  methods: {
    calcHeight() {
      this.$nextTick(() => {
        // this.height = window.innerHeight - 212
         this.height = (window.innerHeight - 280) / 2
         this.tabHeight = this.height - 50
         this.refreshCurrentTabTable()
      })
    },
      refreshCurrentTabTable() {
       if (this.currentRow === '' || this.currentRow === null) {
        this.currentRow = {site: '', orderNo: ''}
      }
   
      if (this.activeName === 'attachments') {
        this.refreshAttachments()
      }
    },
     refreshAttachments () {
      console.log('刷新附件组件，当前行数据：', this.currentRow)
      let inData = {
          orderRef1: this.currentRow.site,
          orderRef2: this.currentRow.orderNo,
          orderRef3: this.currentRow.partNo,
          orderReftype: 'purQuotationDetail',
          height: this.tabHeight
      }
      this.$refs.attachments.init(inData)
    },
    getStatusType(status) {
      const map = {
        pending: 'warning',
        approved: 'success',
        rejected: 'danger'
      }
      return map[status] || 'info'
    },
    getStatusLabel(status) {
      const map = {
        pending: '待审核',
        approved: '已通过',
        rejected: '已拒绝'
      }
      return map[status] || status
    },
    formatMoney(value) {
      if (value === null || value === undefined) return '-'
      return new Intl.NumberFormat('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(value)
    },
    getList() {
      this.loading = true
      const params = {
        limit: this.pageSize,
        page: this.pageIndex,
        ...this.searchData
      }
      searchPurReplyHistPage(params).then(({ data }) => {
        if (data.code === 0) {
          this.dataList = data.page.list
          this.pageIndex = data.page.currPage
          this.pageSize = data.page.pageSize
          this.totalPage = data.page.totalCount
           if (this.dataList.length > 0) {
            this.$refs.mainTable.setCurrentRow(this.dataList[0])
            console.log('默认选中第一行:', this.dataList[0])
            this.changeData(this.dataList[0])
          } else {
            this.changeData(null)
          }
        } else {
          this.$message.error((data && data.msg) || '获取列表失败')
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('请求失败')
      })
    },
    changeData(row) {
      this.currentRow = row ? JSON.parse(JSON.stringify(row)) : { site: '', orderNo: '', itemNo: '' }
      console.log('选中行数据:', this.currentRow)
      this.refreshCurrentTabTable()
   
    },
     tabClick() {
      this.refreshCurrentTabTable()
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getList()
    },
    openDialog(row) {
      if (row) {
        this.dialogTitle = '编辑报价'
        this.editMode = true
        this.form = { ...row }
      } else {
        this.dialogTitle = '新增报价'
        this.editMode = false
        this.form = {
          id: '',
          status: '已报价',
          orderNo: '',
          itemNo: '',
          seqNo: '',
          createdDate: new Date().toISOString(),
          supplierId: '',
          supplierName: '',
          partNo: '',
          partDesc: '',
          qty: 0,
          umid: '',
          remark2: '',
          materialCost: 0,
          produceFee: 0,
          surfaceFee: 0,
          profit: 0,
          price: 0,
          taxRate: 13,
          tax: 0,
          duty: 0,
          purchaseCycle: 0,
          mouldFee: 0,
          additionalCost: 0,
          freight: 0,
          reason: '',
          remark: '',
          createdBy: this.$store.state.user.name,
          wantReplyDate: '',
          employeeName: '',
          paymentTerm: '',
          deliveryTerm: '',
          currency: 'CNY',
          contact: '',
          phoneNo: '',
          faxNo: '',
          otherCondition: '',
          actReplyComments: ''
        }
      }
      this.dialogVisible = true
    },
    validateForm() {
      if (!this.form.partNo) {
        this.$message.warning('物料编码不能为空')
        return false
      }
      if (!this.form.partDesc) {
        this.$message.warning('物料名称不能为空')
        return false
      }
      return true
    },
    submitForm() {
      if (!this.validateForm()) return
      this.submitLoading = true
      const api = this.editMode ? updatePurReplyHist : createPurReplyHist
      api(this.form).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('保存成功')
          this.dialogVisible = false
          this.getList()
        } else {
          this.$message.error((data && data.msg) || '保存失败')
        }
      }).catch(err => {
        this.$message.error('请求失败: ' + err.message)
      }).finally(() => {
        this.submitLoading = false
      })
    },
    accept() {
      this.$confirm(`确定接受供应商报价?`, '提示', { type: 'warning' }).then(() => {
        updatePurReplyHistStatus({ 
          id: this.form.id,
          site: this.$store.state.user.site,
          itemNo: this.form.itemNo,
          orderNo: this.form.orderNo,
          status: '已接受'
         }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.getList()
          } else {
            this.$message.error((data && data.msg) || '操作失败')
          }
        }).catch(err => {
          this.$message.error('操作失败: ' + err.message)
        })
      }).catch(() => {})
    },
    notAccept(row) {
      //备注必填
        if (!this.form.reason) {
        this.$message.warning('原因不能为空')
        return false
      }
      this.$confirm(`确定不接受供应商报价?`, '提示', { type: 'warning' }).then(() => {
        updatePurReplyHistStatus({ 
          id: this.form.id,
          site: this.$store.state.user.site,
          itemNo: this.form.itemNo,
          orderNo: this.form.orderNo,
          status: '未接受',
          reason: this.form.reason
         }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.getList()
          } else {
            this.$message.error((data && data.msg) || '操作失败')
          }
        }).catch(err => {
          this.$message.error('操作失败: ' + err.message)
        })
      }).catch(() => {})
    },

    refuse(row) {
      //备注必填
        if (!this.form.reason) {
        this.$message.warning('原因不能为空')
        return false
      }
      this.$confirm(`确定拒绝供应商报价?`, '提示', { type: 'warning' }).then(() => {
        updatePurReplyHistStatus({ 
          id: this.form.id,
          site: this.$store.state.user.site,
          itemNo: this.form.itemNo,
          orderNo: this.form.orderNo,
          status: '已拒绝',
          reason: this.form.reason
         }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.getList()
          } else {
            this.$message.error((data && data.msg) || '操作失败')
          }
        }).catch(err => {
          this.$message.error('操作失败: ' + err.message)
        })
      }).catch(() => {})
    },
    handleDelete(row) {
      this.$confirm(`确定删除报价单: ${row.orderNo} ?`, '提示', { type: 'warning' }).then(() => {
        deletePurReplyHist({ id: row.id }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success('删除成功')
            this.getList()
          } else {
            this.$message.error((data && data.msg) || '删除失败')
          }
        }).catch(err => {
          this.$message.error('删除失败: ' + err.message)
        })
      }).catch(() => {})
    },
     openPartDialog(row) {
      if (row) {
        this.dialogTitle = '供应商可供物料'
        this.editMode = true
        this.partForm = { 
            ...this.partForm,  // 使用 data 中定义的默认值
            ...row,
              supplierCode: row.supplierId,
              oldUmid: row.umid
         

          }
       
      }
      this.partDialogVisible = true
    },
    savePart() {
      // 表单验证
      if (!this.partForm.partNo) {
        this.$message.warning('物料编码不能为空')
        return false
      }
      if (!this.partForm.supplierCode) {
        this.$message.warning('供应商编码不能为空')
        return false
      }
      
      this.submitLoading = true
      // 调用保存可供物料的 API（根据实际接口名称调整）
      createPartSupplierRelation(this.partForm).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('保存成功')
          this.partDialogVisible = false
          this.getList() // 刷新列表
        } else {
          this.$message.error((data && data.msg) || '保存失败')
        }
      }).catch(err => {
        this.$message.error('保存失败: ' + err.message)
      }).finally(() => {
        this.submitLoading = false
      })
    }
  }
}
</script>

<style scoped>
.pi-search-form { margin-top: 0; }
.pi-form { margin-top: -5px; }
.customer-a {
  cursor: pointer;
  color: #409EFF;
  margin: 0 4px;
}
.customer-a:hover {
  color: #66b1ff;
}
.customer-bun-min {
  margin-right: 10px;
}
</style>