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

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="分账规则设置"
        prop="shareRule"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="shareType"
        align="center"
        label="分账类型"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
            {{scope.row.shareType == 1?'比例分账':'固定金额分账'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="cycle"
        align="center"
        label="分账周期T+"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="platformRate"
        align="center"
        label="平台"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
            {{scope.row.shareType == 1?scope.row.platformRate+ '%':scope.row.platformRate}}
        </template>
      </el-table-column>
      <el-table-column
        prop="shopRate"
        align="center"
        label="购买店铺"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
            {{scope.row.shareType == 1?scope.row.buyShopRate+ '%':'-'}}
        </template>
      </el-table-column>
        <el-table-column
        prop="shopRate"
        align="center"
        label="核销店铺"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
            {{scope.row.shareType == 1?scope.row.checkShopRate+ '%':'-'}}
        </template>
      </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="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" width="550px" :visible.sync="show">
        <el-form
        :model="setQuery"
        ref="setQuery"
        :rules="rules"
        label-width="130px"
        >
        <el-form-item label="分账规则名称：" prop="shareRule">
            <el-input
          size="small"
            v-model="setQuery.shareRule"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        <el-form-item label="分账类型：" prop="shareType">
           <el-select
          v-model="setQuery.shareType"
          size="small"
          class="item-width-300"
          placeholder="请选择分账类型"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="分账周期：" prop="cycle">
           <el-input
            v-model.number="setQuery.cycle"
          size="small"
            :maxlength="3"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        <el-form-item label="平台分账：" prop="platformRate">
           <el-input
            v-model.number="setQuery.platformRate"
            type="number"
            class="item-width-300"
            ></el-input>
        </el-form-item>
         <el-form-item label="购买店铺分账：" prop="buyShopRate">
           <el-input
            v-model.number="setQuery.buyShopRate"
            size="small"
            type="number"
            class="item-width-300"
            ></el-input>
        </el-form-item>
         <el-form-item label="核销店铺分账：" prop="checkShopRate">
           <el-input
            v-model.number="setQuery.checkShopRate"
            size="small"
            type="number"
            class="item-width-300"
            ></el-input>
        </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/rule";
export default {
  name: "comboManage",
  data() {
    return {
      options: [
        {
          value: 1,
          label: "比例分账",
        },
        // {
        //   value: 2,
        //   label: "固定金额分账",
        // },
       
      ],
      title:'',
      tableData: [],
      loading: false,
      btnLoading: false,
      show: false,
      query: {
        currentPage: 1,
        pageSize: 10,
      },
       setQuery: {
        "cycle": 0,
        "platformRate": 0,
        "shareRule": "",
        "shareType": 1,
        "buyShopRate": 0,
        "checkShopRate": 0,
      },
      rules: {
        shareRule: [
          { required: true, message: "请输入分账规则名称", trigger: "blur" },
        ],
        cycle: [
          { required: true, message: "请输入分账周期", trigger: "blur" },
        ],
        buyShopRate: [
          { required: true, message: "请输入购买商户分账", trigger: "blur" },
        ],
         checkShopRate: [
          { required: true, message: "请输入核销商户分账", trigger: "blur" },
        ],
        platformRate: [
          { 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
    },
    handleAdd() {
     this.title = '新增规则'
     this.show = true
     this.setQuery = {
        "cycle": 0,
        "platformRate": 0,
        "shareRule": "",
        "shareType": 1,
        "buyShopRate": 0,
        "checkShopRate": 0,
      }
    },
    save(){
      this.$refs.setQuery.validate((v) => {
        if (v) {
              this.btnLoading = true;
            addOrUpdate(this.setQuery).then((res) => {
                this.btnLoading = false;
                if (res.state == "Success") {
                    this.show = false
                this.$notify({
                    title: "成功",
                    message: "操作成功",
                    type: "success",
                });
                this.setQuery = {
                    setMealCode: 0,
                    setMealDetail: "",
                    setMealId: 0,
                    setMealName: "",
                };
                  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() {
      this.loading = true
      getRuleList(this.query).then((res) => {
        this.loading = false
        if (res.state == "Success") {
          this.tableData = res.content.records;
          this.total = res.content.total
        }
      });
    },
  },
  created() {
    this.getRuleList();
  },
};
</script>
  
<style lang="scss" >
.rulesManage {
    .btn-group{
        display: flex;
        justify-content: center;
    }
}
</style>
  