<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input
          size="small"
          style="width: 200px;"
          class="inline-16"
          suffix-icon="el-icon-search"
          v-model="param.content.where.condition"
          placeholder="产品类别名称"
          @keyup.enter.native="listData(param.content.pageNumber = 1)"
          @clear="listData(param.content.pageNumber = 1)"
          clearable>
      </el-input>&nbsp;
      <!--      <product_table style="float: right" :data="data"  ref="quoterPrice" @productAdd="productInitialization" ></product_table>-->
      <add v-if="tool.checkAuth($route.name,'productClassManage') && data.status === '新建' && isLeader" :data="data" class="inline-16" @itemClassAdd="listData"></add>
    </div>
    <div class="produtMag-panel">
      <el-table
          ref="multipleTable"
          height="calc(100vh - 500px)"
          :data="list"
          style="width: 100%"
          :header-cell-style="{height:'50px',color:'#606266',fontWeight:'400'}"
          :cell-style="{height:'50px',color:'#666666',fontWeight:'400'}"
      >
        <!--        <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>-->
        <el-table-column
            prop="itemclassfullname"
            label="产品类别名称"
            width="0">
        </el-table-column>
        <el-table-column
            prop="brandname"
            label="品牌"
            width="0">
        </el-table-column>
        <el-table-column
            prop="discountrate"
            label="折扣(%)"
            width="80"
            fixed="right">
          <template slot-scope="scope">
            <div v-if=" !saveShow || index !== scope.row.rowindex">
              <p><span >{{scope.row.discountrate}}</span></p>
            </div>
            <span v-else>
              <el-input v-model="scope.row.discountrate" size="mini" placeholder="请输入折扣" @change="onChangeDiscountrate(scope.row.discountrate,scope.row,scope.$index)"></el-input>
            </span>
          </template>
        </el-table-column>
        <el-table-column
            prop="operation"
            fixed="right"
            label="操作"
            width="120">
          <template slot-scope="scope">
            <el-button  size="mini" type="text" class="inline-16" @click="onEdit(scope.row)" v-if="index !== scope.row.rowindex" :disabled="!tool.checkAuth($route.name,'productClassManage') || data.status !== '新建' || !isLeader">编 辑</el-button>
            <el-button  size="mini" type="text" class="inline-16" @click="onSave(scope.row)" v-if="saveShow && index === scope.row.rowindex">保 存</el-button>
            <el-popconfirm
                title="确定删除该产品类别吗?"
                @confirm="deleteProduct(scope.row)"
            >
              <el-button class="inline-16" slot="reference" size="mini" type="text"  :disabled="!tool.checkAuth($route.name,'productClassManage') || data.status !== '新建' || !isLeader">删 除</el-button>
            </el-popconfirm>

<!--            <el-button  size="mini" type="text" class="inline-16" @click="onCancel" v-if="saveShow">取 消</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
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="param.content.pageNumber"
              :page-sizes="[20, 50, 100, 200]"
              :page-size="100"
              layout="total,sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>

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

<script>
import add from './add'
export default {
  name: "index",
  props:["data","isLeader"],
  components:{add},
  data(){
    return {
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      index:'',
      saveShow:false,
      param:{
        "id": 20221124135602,
        "content": {
          "sa_contractid": this.$route.query.id,
          "pageNumber": 1,
          "pageSize": 100,
          "where": {
            "condition": ""
          }
        }
      }
    }
  },
  methods:{
    async listData(){
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      for (var i=0 ;i<res.data.length;i++){
        this.list[i].discountrate = Math.round((res.data[i].discountrate * 100)*100)/100
        /* this.totalPrice = this.totalPrice + res.data[i].qty * res.data[i].price*/
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.productData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.productData()
    },
    onChangeDiscountrate(val,data,index){
      if (val > 100){
        data.discountrate = 100
      }else if (val <= 0){
        data.discountrate = 1
      }else {
        data.discountrate = Math.round(val * 100)/100
      }
      this.$set(this.list,index,data)
    },
    onEdit(row){
      this.saveShow = true
      this.index = row.rowindex
    },
    async onSave(row){
      row.discountrate =(row.discountrate/100).toFixed(4)
      console.log(row,'保存的数据')
      const res = await this.$api.requested({
        "id": 20221124110202,
        "content": {
          "sa_contractid":this.$route.query.id,     //sat_notice_classid<=0时 为新增
          "items":[row]
        }
      })
      this.tool.showMessage(res,()=>{
        this.saveShow = false
        this.index = ''
        this.listData()
      })
    },
    onCancel(){
      this.saveShow = false
      this.index = ''
      this.listData()
    },
    async deleteProduct(row){
      console.log(row)
      const res = await this.$api.requested({
        "id": 20221124111202,
        "content": {
          "sa_contractid":this.$route.query.id,
          "sa_contract_itemsaleclassids":[row.sa_contract_itemsaleclassid]
        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
      })
    }
  },
  mounted() {
    this.listData()
  }
}
</script>

<style scoped>

</style>