
<template>
  <div class="commission app-container">
    <div class="search">
      <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >新增规则</el-button
      >
    </div>

    <div class="title">C端用户佣金</div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="佣金规则"
        prop="channelRule"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        :prop="'setMealId'+item.value"
        v-for="item in options"
        :key="'setMealId'+item.value"
        align="center"
        :label="item.label"
        show-overflow-tooltip
      >
      </el-table-column>
      
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" class="red" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="title">渠道佣金</div>
    <el-table
      :data="tableData1"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="渠道佣金规则"
        prop="channelRule"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        :prop="'setMealId'+item.value"
        v-for="item in options"
        :key="'setMealId'+item.value"
        align="center"
        :label="item.label"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="渠道网点佣金"
        prop="channelRate"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="渠道网点员工佣金"
        prop="employeeRate"
        show-overflow-tooltip
      >
      </el-table-column>
      
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" class="red" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
   

    <el-dialog :title="title" width="550px" :visible.sync="show">
        <el-form
        :model="setQuery"
        ref="setQuery"
        :rules="rules"
        label-width="150px"
        >
        <el-form-item label="佣金规则名称：" prop="shareRule">
            <el-input
          size="small"
            v-model="setQuery.channelRule"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        <el-form-item :label="item.label+'：'" v-for="item in options" :key="item.label">
           <el-input
            v-model.number="item.commission"
            size="small"
            :maxlength="4"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        <el-form-item label="渠道网点佣金：" prop="channelRate">
           <el-input
            v-model.number="setQuery.channelRate"
            :maxlength="4"
            class="item-width-300"
            ></el-input>
             <span slot="append">%</span>
        </el-form-item>
         <el-form-item label="渠道网点员工佣金：" prop="employeeRate">
           <el-input
            v-model.number="setQuery.employeeRate"
            :maxlength="3"
          size="small"
            class="item-width-300"
            ></el-input>
             <span slot="append">%</span>
        </el-form-item>
        </el-form>

        <div class="btn-group">
        <el-button>取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="save">确 定</el-button>
        </div>

    </el-dialog>


  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import {getRuleList,delRule,addOrUpdate } from "@/api/commission";
import { getSetMealNameAndId, operate } from '@/api/common';
export default {
  name: "commission",
  data() {
    return {
      title:'',
      tableData: [],
      tableData1: [],
      options: [],
      copyOptions:[],//拷贝options
      loading: false,
      btnLoading: false,
      show: false,
      query: {
        currentPage: 1,
        pageSize: 10,
      },
       setQuery:{
        "channelRate": 0,
        "channelRule": "",
        "employeeRate": 0,
        "shareJsons": [],
        "shareType": 2
        },
      rules: {
        channelRule: [
          { required: true, message: "请输入渠道佣金规则名称", trigger: "blur" },
        ],
        employeeRate: [
          { required: true, message: "请输入员工分账比率", trigger: "blur" },
        ],
        channelRate: [
          { required: true, message: "请输入渠道商分账比率", trigger: "blur" },
        ],
       
      },
      title: "",
      total: 0,
    };
  },
  watch: {
    query: {
      handler: debounce(function (val) {
          this.getRuleList()

      }),
      deep: true,
    },
  },
  methods: {
    handleDel(id){
         this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delRule({id}).then(res=>{
                if(res.state == 'Success'){
                    this.$notify({
                        title: '成功',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.getRuleList()
                }
            })
        })
    },
    handleEdit(row){
     this.title = '编辑规则'
     this.show = true
    //  this.setQuery = {
    //     "id":id,
    //     "cycle": 0,
    //     "platformRate": 0,
    //     "shareRule": "",
    //     "shareType": 1,
    //     "shopRate": 0
    //   }
    this.setQuery = row
    this.copyOptions = JSON.parse(JSON.stringify(this.options)) 
    row.setMealJson.map(item=>{
         this.options.map(i=>{
             if(item.setMealId == i.value){
                this.$set(i,'commission',item.commission)
             }   
        })
    })

    },
    handleAdd() {
     this.title = '新增规则'
     this.show = true
     this.setQuery = {
        "channelRate": 0,
        "channelRule": "",
        "employeeRate": 0,
        "shareJsons": [],
        "shareType": 2
        }
    },
    save(){
        this.btnLoading = true;
        this.$refs.setQuery.validate((v) => {
            if (v) {
                // 把套餐的数据拿到
                this.setQuery.shareJsons = []
                this.options.map(item=>{
                    this.setQuery.shareJsons.push({
                        commission:item.commission,
                        setMealId:item.value
                    })
                })
            addOrUpdate(this.setQuery).then((res) => {
                this.btnLoading = false;
                if (res.state == "Success") {
                    this.show = false
                this.$notify({
                    title: "成功",
                    message: "操作成功",
                    type: "success",
                });
               
                this.getRuleList();
                }
            });
            }
        });
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.getRuleList();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getRuleList();
    },

    search() {
      this.query.currentPage = 1;
      this.getRuleList();
    },
    getRuleList() {
    console.log(55555555555);

      this.loading = true
      getRuleList(this.query).then((res) => {
        this.loading = false
        if (res.state == "Success") {
            let data = res.content.records
            // 动态套餐列表
            data.map(item=>{
                item.setMealJson = JSON.parse(item.setMealJson)
                item.setMealJson.map(i=>{
                    this.$set(item,'setMealId'+i.setMealId,i.commission)
                })
            })
            this.tableData = data.filter(item=>item.shareType == 1)
            this.tableData1 = data.filter(item=>item.shareType == 2)
       
        }
      });
    },
    getSetMealNameAndId(){
        return new Promise((resolve,reject)=>{
            getSetMealNameAndId().then(res=>{
                if (res.state == "Success") {
                    for (const key in res.content) {
                        if (Object.hasOwnProperty.call(res.content, key)) {
                            this.options.push({
                                label:res.content[key],
                                value:key
                            })
                        }
                    }
                    resolve()   
                }
            })
        })
    }
  },
  created() {
    console.log(4444444444);
      this.getSetMealNameAndId().then(()=>{
        this.getRuleList();
    })
  },
};
</script>
  
<style lang="scss" >
.commission {
    .btn-group{
        display: flex;
        justify-content: center;
    }

    .title{
        font-weight: bold;
        font-size: 16px;
        color: #666;
        margin: 20px 0 10px;
    }
}
</style>
  