<template>
  <div>
    <div >
      <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>

      <importExcel style="float: right" @onSuccess="onSuccess" v-if="type === '客户报价'" ref="quoterImport" @checkForm="checkFormImport"></importExcel>
      <productTable style="float: right;margin-right: 10px;margin-bottom: 10px" :id="sa_projectid" @checkForm="checkForm" ref="quoterPrice" @productAdd="productData"></productTable>
      <delProduct style="float: right;margin-right: 10px" :data="selectDel" @delSuccess="delSuccess"></delProduct>
    </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="55">
        </el-table-column>
        <el-table-column
            align="center"
            label="产品图"
            width="100">
          <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>&nbsp;/&nbsp;<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="数量"
            width="180">
          <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,scope.row.orderaddqty)"></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="折扣(%)"
            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="price"
          label="单价"
          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="amount"
          label="金额"
          width="100">
          <template slot-scope="scope">
            <p><span>{{scope.row.amount}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
          prop="specialoffer"
          label="是否特价"
          width="100">
          <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";

export default {
  props:["sa_projectid","type"],
  name: "quotedPriceProduct",
  components:{uploadFile, previewImage,productTable,importExcel,delProduct},
  data(){
    return {
      selectDel:[],
      totalPrice:0,
      step:1,
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      changeList:[],
      sa_quotedpriceid:'',
      param:{
        "id": 20221021095503,
        "version":1,
        "content": {
          "sa_quotedpriceid":'',
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": ""
          }

        }
      }
    }
  },
  methods:{
    /*/!*产品配置信息新增初始化*!/
    async productInitialization(id){
      this.param.content.sa_quotedpriceid = id
      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 = this.list[i].discountrate * 100
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      this.$emit("queryTotalPrice",totalPrice)
      console.log(this.list,totalPrice,'产品配置信息')
    },*/
    /*产品配置信息*/
    async productData(){
      console.log(this.id,"配置信息")
      this.param.content.sa_quotedpriceid = this.sa_quotedpriceid
      console.log(this.param,"param数据")
      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 = this.list[i].discountrate * 100
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      console.log(this.list,'产品配置信息')
    },
   /* /!*修改折扣或单价的时候刷新*!/
    async productDiscountrateData(id){
      this.param.content.sa_quotedpriceid = id
      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 = this.list[i].discountrate * 100
        this.totalPrice = this.totalPrice + this.list[i].qty * this.list[i].price
      }
      console.log(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){
      console.log(val,'选择')
      for (let i=0;i<val.length;i++){
        this.selectDel[i] = val[i].sa_quotedprice_itemsid
      }
      console.log(this.selectDel)
    },
    checkForm(){
      this.$emit('checkForm')
    },
    checkFormImport(){
      this.$emit('checkFormImport')
    },
    quoterImport(sa_quotedpriceid){
      this.sa_quotedpriceid = sa_quotedpriceid
      this.$refs.quoterImport.onImport(sa_quotedpriceid)
    },
    quoterPrice(sa_quotedpriceid,type){
      this.sa_quotedpriceid = sa_quotedpriceid
      console.log(sa_quotedpriceid,"报价单id")
      console.log(type,"项目报价")
      this.$refs.quoterPrice.queryProduct(sa_quotedpriceid,type)
    },
    onChangeNum (val,data,index,step) {
      console.log("步长",this.step)
      console.log(val,data,index,step)
      if (val <= data.orderminqty){
        val = data.orderminqty
      }else {
        val = val - data.orderminqty
        let m = val % data.orderaddqty
        console.log(m)
        if (m !== 0){
          val = val + data.orderaddqty
          console.log(val,"新的数量值")
          data.qty = val
        }
      }
      data.amount = val * data.price

      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.$emit('productData',this.list)
    },
    onChangeDiscountrate(val,data,index) {
      console.log(val,data,index)
      if (val > 100){
        data.discountrate = 100
        val = 100
      }else if (val <= 10){
        data.discountrate = 10
        val = 10
      }
      data.price = data.marketprice * (val / 100)
      data.amount = (data.qty * data.price).toFixed(2)
      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.$emit('productData',this.list)
    },
    onChangePrice(val,data,index){
      if (val > data.marketprice) {
        data.price = data.marketprice
        val = data.marketprice
      }else if (val < 0){
        data.price = data.marketprice * 0.1
        val = data.marketprice * 0.1
      }
      data.discountrate = ((val / data.marketprice) * 100).toFixed(2)
      data.amount = data.qty * data.price
      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.$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){
      console.log(this.list,"数据")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      console.log(res)
      this.productData(res.data[0].sa_quotedpriceid)
    },
    async changeDiscountrateData(obj){
      console.log(this.list,"数据")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      console.log(res)
      this.productDiscountrateData(res.data[0].sa_quotedpriceid)
    },
    async deleteProduct(val){
      console.log(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()
  }
}
</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>