<template>
  <div>
    <div style="padding-bottom: 35px">
      <el-input
          size="small"
          style="width: 200px;float: left"
          suffix-icon="el-icon-search"
          v-model="param.content.where.condition"
          placeholder="产品名称,编号"
          @keyup.enter.native="productData(param.content.pageNumber = 1)"
          @clear="productData(param.content.pageNumber = 1)"
          clearable>
      </el-input>
      <delProduct style="float: right;" ref="delProduct" :data="selectDel" @delSuccess="delSuccess"></delProduct>
      <importExcel style="float: right;margin-right: 10px" @onSuccess="onSuccess" v-if="type === '客户报价'" ref="quoterImport" @checkForm="checkFormImport"></importExcel>
      <productTableProject style="float: right;margin-right: 10px;" :id="sa_projectid" @checkFormProject="checkFormProject" ref="quoterPriceProject" @productAdd="productAddF" v-if="type === '项目报价'"></productTableProject>
      <productTable style="float: right;margin-right: 10px;" :id="sa_projectid" @checkForm="checkForm" ref="quoterPrice" @productAdd="productAddS"></productTable>

    </div>
    <div class="produtMag-panel" >
      <el-table
          ref="multipleTable"
          :data="list"
          style="width: 100%"
          :header-cell-style="{height:'50px',color:'#606266',fontWeight:'400'}"
          :cell-style="{height:'50px',color:'#666666',fontWeight:'400'}"
          @selection-change="selectionChange">
        <el-table-column
            type="selection"
            width="42"
            fixed>
        </el-table-column>
        <el-table-column
            align="center"
            label="产品图"
            width="80">
          <template slot-scope="scope">
            <div v-if="scope.row.attinfos[0]">
              <previewImage class="image" :image="scope.row.attinfos[0]" :list="scope.row.attinfos" :deletebtn="false"></previewImage>
            </div>
          </template>
        </el-table-column>
        <el-table-column
            prop="itemno"
            label="产品编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="itemname"
            label="产品名称"
            width="180">
        </el-table-column>
        <el-table-column
            label="型号"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.model}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            label="规格"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.spec}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="caliber"
            label="口径"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.caliber?scope.row.caliber:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="pressure"
            label="压力"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.pressure?scope.row.pressure:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="material"
            label="材质"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.material?scope.row.material:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="brand"
            label="品牌"
            width="180">
          <template slot-scope="scope">
            <p v-for="(item,index) in scope.row.brand">
              <span style="float: left" v-if="index === scope.row.brand.length -1">{{item?item.brandname+'':'--'}}</span>
              <span style="float: left" v-else>{{item?item.brandname+',':'--'}}</span>
            </p>
          </template>
        </el-table-column>
        <el-table-column
            prop="itemclass"
            label="类别"
            width="180">
          <template slot-scope="scope">
            <p v-for="(item,index) in scope.row.itemclass">
              <span style="float: left" v-if="index === scope.row.itemclass.length -1">{{item?item.itemclassname+'':'--'}}</span>
              <span style="float: left" v-else>{{item?item.itemclassname+',':'--'}}</span>
            </p>
          </template>
        </el-table-column>
        <el-table-column
            prop="unitid"
            label="计量单位"
            width="100">
          <template slot-scope="scope">
            <el-tag size="mini" type="info" effect="plain">{{scope.row.unitname}}/{{scope.row.axunitname}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="num"
            label="数量"
            fixed="right"
            width="150">
          <template slot-scope="scope">
            <el-input-number  v-model="scope.row.qty" size="mini"  :min="1" label="描述文字" @change="onChangeNum(scope.row.qty,scope.row,scope.$index)"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
            prop="marketprice"
            label="牌价(元)"
            width="100">
          <template slot-scope="scope">
            <p><span>{{scope.row.marketprice?scope.row.marketprice:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="discountrate"
            label="折扣(%)"
            fixed="right"
            width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.discountrate" size="mini" placeholder="请输入折扣" @change="onChangeDiscountrate(scope.row.discountrate,scope.row,scope.$index)"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            prop="mindiscountrate"
            label="最低授权折扣(%)"
            fixed="right"
            width="80">
          <!--          <template slot-scope="scope">
                      <el-input v-model="scope.row.discountrate" size="mini" placeholder="请输入最低授权折扣"></el-input>
                    </template>-->
        </el-table-column>
        <el-table-column
          prop="price"
          label="单价(元)"
          fixed="right"
          width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.price" size="mini" placeholder="请输入单价" @change="onChangePrice(scope.row.price,scope.row,scope.$index)"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            prop="discountDifferenceAmount"
            label="授权折扣差额(元)"
            fixed="right"
            width="80">
          <!--          <template slot-scope="scope">
                      <el-input v-model="scope.row.discountrate" size="mini" placeholder="请输入授权折扣差额" ></el-input>
                    </template>-->
        </el-table-column>
        <el-table-column
          prop="amount"
          label="金额(元)"
          fixed="right"
          width="100">
          <template slot-scope="scope">
            <p><span>{{tool.formatAmount(scope.row.amount,2)}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
          prop="specialoffer"
          fixed="right"
          label="是否特价"
          width="60">
          <template slot-scope="scope">
            <i class="el-icon-check" v-if="scope.row.specialoffer === 1"  style="color: red;font-size: 20px"></i>
          </template>
        </el-table-column>
<!--        <el-table-column
            prop="operation"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="deleteProduct(scope.row)">删除</el-button>
          </template>
        </el-table-column>-->
      </el-table>
      <div style="height: 35px;margin-top: 20px">
        <div style="float: left">总金额：{{ totalPrice }}元</div>
        <div style="float: right">
          <el-pagination
              background
              small
              style="text-align: right;"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-size="param.content.pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
import uploadFile from "@/components/upload/hw_obs_upload";
import previewImage from "@/components/previewImage";
import productTable from '../components/productTable'
import importExcel from '../modules/importQuotedPrice'
import delProduct from "@/SDrpManagement/QuotedPrice/components/delProduct";
import productTableProject from './productTableProject'
export default {
  props:["sa_projectid","type"],
  name: "quotedPriceProduct",
  components:{uploadFile, previewImage,productTable,importExcel,delProduct,productTableProject},
  data(){
    return {
      selectDel:[],
      totalPrice:0,
      step:1,
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      changeList:[],
      sa_quotedpriceid:'',
      param:{
        "id": 20221021095503,
        "content": {
          "sa_quotedpriceid":'',
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": ""
          }

        }
      }
    }
  },
  methods:{
    productAddF(){
      this.productData()
    },
    productAddS(){
      this.productData()
    },
    /*产品配置信息*/
    async productData(){
      console.log(this.sa_quotedpriceid,"报价单id")
      this.param.content.sa_quotedpriceid = this.sa_quotedpriceid
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      this.totalPrice = 0
      for (var i=0 ;i<this.list.length;i++){
        this.list[i].discountrate = Math.round((res.data[i].discountrate * 100)*100)/100
        this.list[i].mindiscountrate = Math.round((res.data[i].mindiscountrate * 100) * 100)/100
        this.list[i].price = Math.round(res.data[i].price * 100)/100
        this.list[i].discountDifferenceAmount = Math.round(res.data[i].discountDifferenceAmount * 100)/100
        this.totalPrice = this.totalPrice + res.data[i].qty * res.data[i].price
      }
      this.totalPrice = Math.round(this.totalPrice * 100)/100
      this.$emit('productData',this.list)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.productData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.productData()
    },
    /*批量删除*/
    delSuccess(){
      this.productData()
    },
    onSuccess(){
      this.productData()
    },
    clearData(){
      this.search = ""
      this.param.content.where.condition = this.search
      this.productData()
    },
    queryClick(){
      this.param.content.where.condition = this.search
      this.productData()
    },
    selectionChange(val){
      this.selectDel = []
      for (let i=0;i<val.length;i++){
        this.selectDel[i] = val[i].sa_quotedprice_itemsid
      }
      this.$refs.delProduct.length = this.selectDel.length
    },
    checkForm(){
      this.$emit('checkForm')
    },
    checkFormProject(type){
      this.$emit('checkFormProject',type)
    },
    checkFormImport(){
      this.$emit('checkFormImport')
    },
    quoterImport(sa_quotedpriceid){
      this.sa_quotedpriceid = sa_quotedpriceid
      this.$refs.quoterImport.onImport(sa_quotedpriceid)
    },
    quoterPrice(sa_quotedpriceid,type,btntype){
      this.sa_quotedpriceid = sa_quotedpriceid
      btntype === 'project'?this.$refs.quoterPriceProject.queryProduct(sa_quotedpriceid,type):this.$refs.quoterPrice.queryProduct(sa_quotedpriceid,type)

    },
    onChangeNum (val,data,index) {
      data.qty = Math.round(val *100)/100
      data.amount = Math.round((data.qty * data.price) * 100)/100
      data.discountDifferenceAmount = Math.round((data.marketprice * (data.discountrate/100) - data.marketprice * (data.mindiscountrate/100)) * 100)/100
      this.$set(this.list,index,data)
      this.totalPrice = 0
      for (var i=0 ;i<this.list.length;i++){
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      this.totalPrice = Math.round(this.totalPrice * 100)/100
      this.$emit('productData',this.list)
    },
    onChangeDiscountrate(val,data,index) {
      data.specialoffer = 0
      if (val > 100){
        data.discountrate = 100
      }else if (val <= 0){
        data.discountrate = 1
      }else {
        data.discountrate = Math.round(val * 100)/100
      }
      data.price = Math.round((data.marketprice * (data.discountrate / 100)) *100)/100
      data.amount = Math.round((data.qty * data.price)*100)/100
      data.discountDifferenceAmount =  Math.round(( data.marketprice * (data.discountrate/100) - data.marketprice * (data.mindiscountrate/100)) * 100)/100
      if (data.discountDifferenceAmount < 0){
        data.specialoffer =1
      }
      this.$set(this.list,index,data)
      this.totalPrice = 0
      for (var i=0 ;i<this.list.length;i++){
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      this.totalPrice = Math.round(this.totalPrice * 100)/100
      this.$emit('productData',this.list)
    },
    onChangePrice(val,data,index){
      data.specialoffer = 0
      if (val > data.marketprice) {
        data.price = data.marketprice
      }else if (val < data.marketprice * 0.01){
        data.price = data.marketprice * 0.01
      }else {
        data.price = Math.round(data.price * 100)/100
      }
      data.discountrate = Math.round(((data.price / data.marketprice) * 100) *100)/100
      data.discountDifferenceAmount =  Math.round(( data.marketprice * (data.discountrate/100) - data.marketprice * (data.mindiscountrate/100)) * 100)/100
      data.amount = Math.round((data.qty * data.price)*100)/100
      if (data.discountDifferenceAmount < 0){
        data.specialoffer =1
      }
      this.$set(this.list,index,data)
      this.totalPrice = 0
      for (var i=0 ;i<this.list.length;i++){
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      this.totalPrice = Math.round(this.totalPrice * 100)/100
      this.$emit('productData',this.list)
    },
    objSet(data){
      let obj = data.map(e=>{
        return {
          sa_quotedprice_itemsid:e.sa_quotedprice_itemsid,
          itemid:e.itemid,
          oldprice:e.marketprice,
          price:e.price,
          discountrate:0,
          qty:e.qty
        }
      })
      this.changeDiscountrateData(obj)
    },
    async changeData(obj){
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      this.productData(res.data[0].sa_quotedpriceid)
    },
    async changeDiscountrateData(obj){
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      this.productDiscountrateData(res.data[0].sa_quotedpriceid)
    },
    async deleteProduct(val){
      const res = await this.$api.requested({
        "id": 20221021095603,
        "version":1,
        "content": {
          "sa_quotedprice_itemsids":[val.sa_quotedprice_itemsid]     //sat_notice_classid<=0时 为新增
        }
      })
      this.tool.showMessage(res,()=>{
        this.productData(val.sa_quotedpriceid)
      })
    }
  },
  mounted() {
    /*this.productData()*/
    console.log(this.sa_projectid,this.type,"产品报价信息")
  }
}
</script>

<style scoped>
.produtMag-panel{
   margin-top: 15px;
  background: #fff;
  border-radius:5px;

  border:1px solid rgb(0 0 0 / 5%)
  /* box-shadow: 0 5px 5px rgb(0 0 0 / 10%);
  transform: translate3d(0,-2px,0); */
}
.image {
  width:38px;height:38px;margin:0px auto;
}
</style>