<template>
  <div class="fxDistribution PdistributorLevel">
    <div class="head-title">
      粉象分销等级
      <el-button @click="AddDig" style="float:right;margin-top: -5px;" size="small" type="primary">
        新增
      </el-button>
      <el-button @click="getView" style="float:right;margin: -5px 20px 0 0;" size="small" type="primary">
        预览
      </el-button>
    </div>
    <div class="content">
      <div>
        <div class="searchInput" style="width:230px">
          <el-input style="display:inline-block;width:205px;height:30px" placeholder="请输入等级名称查询" v-model="msg.GradeName"
            @keyup.enter.native="msg.pageIndex=1,getList()" @clear="msg.pageIndex=1,getList()" size="small" clearable>
          </el-input>
          <span @click="msg.pageIndex=1,getList()" class="el-icon-search"
            style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
        </div>
      </div>
      <el-table :data="tableData" v-loading="loading" border style="width: 100%;margin:20px 0">
        <el-table-column prop="Id" label="ID" width="80">
        </el-table-column>
        <el-table-column prop="Grade" label="等级">
        </el-table-column>
        <el-table-column prop="Icon" label="图标">
          <template slot-scope="scope">
            <div class="zk_pic_box" flex="main:center cross:center" style="width:32px;height:32px;"
              :style="{backgroundImage:'url(' + getIconLink(scope.row.Icon) + ')'}">
              <i v-if="scope.row.Icon==''" class="el-icon-picture-outline"></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="返佣类型">
          <template slot-scope="scope">
            <span v-if="scope.row.CommissionType==1">正常返佣</span>
            <span v-if="scope.row.CommissionType==2">购买抵扣</span>
          </template>
        </el-table-column>
        <el-table-column prop="GradeName" label="等级名称">
        </el-table-column>
        <el-table-column prop="IsGuest" label="会员类型">
          <template slot-scope="scope">
            <span v-if="scope.row.IsGuest==1">普通</span>
            <span v-if="scope.row.IsGuest==2">其他</span>
            <span v-if="scope.row.IsGuest==3">Vip</span>
          </template>
        </el-table-column>
        <el-table-column prop="CommissionRatio" label="返佣比例">
          <template slot-scope="scope">
            <span>{{scope.row.CommissionRatio+'%'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SiblingRatio" label="同级返佣比例">
          <template slot-scope="scope">
            <span>{{scope.row.SiblingRatio+'%'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SiblingRebatesNum" label="可拿同级返佣的数量" width="150">
        </el-table-column>
        <el-table-column prop="IsCanBuy" label="是否可以购买">
          <template slot-scope="scope">
            <span>{{scope.row.IsCanBuy==1?'是':'否'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="BuyMoney" label="购买金额">
        </el-table-column>
        <el-table-column prop="DecimalType" label="小数类型">
          <template slot-scope="scope">
            <span v-if="scope.row.DecimalType==1">取整</span>
            <span v-if="scope.row.DecimalType==2">保留两位小数</span>
          </template>
        </el-table-column>
        <el-table-column prop="UpdateDate" label="操作时间">
        </el-table-column>
        <el-table-column prop="address" width="120" label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <img @click="Edit(scope.row)" style="width:32px;height:32px" src="../../assets/img/userman/edit.png"
                alt="">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <img @click="Delete(scope.row)" style="width:32px;height:32px;margin:0 10px"
                src="../../assets/img/userman/del.png" alt="">
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align:right" background @current-change="handleCurrentChange" :page-size="msg.pageSize"
        layout="prev, pager, next" :current-page.sync="msg.pageIndex" :total="total">
      </el-pagination>
    </div>
    <!-- 新增与修改-->
    <el-dialog :title="commonTitle" :visible.sync="dialogVisible" width="900px">
      <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="160px">
        <el-row>
          <el-col :span="12">
            <p style="padding-left:34px;margin-bottom:15px" class="cred">注：等级需连续，从等级1开始</p>
            <el-form-item label="分销商等级" prop="Grade">
              <el-select v-model="addMsg.Grade" placeholder="请选择" size="small">
                <el-option v-for="(item,index) in numList" :key="index" :label="`等级${index+1}`" :value="item.Id"
                  :disabled="item.dis">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <p style="padding-left:34px;margin-bottom:15px;visibility:hidden;" class="cred">.</p>
            <el-form-item label="等级名称" prop="GradeName">
              <el-input v-model="addMsg.GradeName" size="small" style="width:218px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员类型" prop="IsGuest">
              <el-select v-model="addMsg.IsGuest" placeholder="请选择" size="small">
                <el-option label="普通" :value="1"></el-option>
                <el-option label="其他" :value="2"></el-option>
                <el-option label="Vip" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="返佣比例">
              <el-input style="width:218px;" size="small" v-model="addMsg.CommissionRatio"
                @keyup.native="checkPrice(addMsg,'CommissionRatio')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="同级返佣比例">
              <el-input v-model="addMsg.SiblingRatio" size="small" style="width:218px;"
                @keyup.native="checkPrice(addMsg,'SiblingRatio')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="同级返佣数量">
              <el-input v-model="addMsg.SiblingRebatesNum" size="small" style="width:218px;"
                @keyup.native="checkInteger(addMsg,'SiblingRebatesNum')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否可以购买">
              <el-select v-model="addMsg.IsCanBuy" placeholder="请选择" size="small">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购买金额">
              <el-input v-model="addMsg.BuyMoney" size="small" style="width:218px;" @keyup.native="checkPrice(addMsg,'BuyMoney')">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年份">
              <el-select v-model="addMsg.Year" placeholder="请选择" size="small">
                <el-option label="1" :value="1"></el-option>
                <el-option label="2" :value="2"></el-option>
                <el-option label="3" :value="3"></el-option>
                <el-option label="4" :value="4"></el-option>
                <el-option label="5" :value="5"></el-option>
                <el-option label="6" :value="6"></el-option>
                <el-option label="7" :value="7"></el-option>
                <el-option label="8" :value="8"></el-option>
                <el-option label="9" :value="9"></el-option>
                <el-option label="10" :value="10"></el-option>
              </el-select>
              <span>年</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可以升级">
              <el-select v-model="addMsg.IsUpgrade" size="small" placeholder="请选择">
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="小数类型">
              <el-select v-model="addMsg.DecimalType" size="small" placeholder="请选择">
                <el-option label="取整" :value="1"></el-option>
                <el-option label="保留两位小数" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="升级所需下级等级">
              <el-select v-model="addMsg.UpgradeId" placeholder="请选择" size="small">
                <el-option v-for="(item,index) in UpgradeIdList" :key="index" :label="item.GradeName" :value="item.Id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="升级所需下级等级数量">
              <el-input v-model="addMsg.UpgradeNum" style="width:218px;" size="small"
                @keyup.native="checkInteger(addMsg,'UpgradeNum')"></el-input>
            </el-form-item>
            <el-form-item label="返佣类型">
              <el-select v-model="addMsg.CommissionType" size="small">
                <el-option label="正常返佣" :value="1"></el-option>
                <el-option label="购买抵扣" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图标">
              <el-tooltip class="item" effect="dark" content="建议尺寸:32*32" placement="top">
                <el-button size="mini" @click="choicImg=true">选择图片</el-button>
              </el-tooltip>
              <div class="zk_pic_box" flex="main:center cross:center" style="width:32px;height:32px;"
                :style="{backgroundImage:'url(' + getIconLink(addMsg.Icon) + ')'}">
                <i v-if="addMsg.Icon==''" class="el-icon-picture-outline"></i>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述">
              <el-input v-model="addMsg.Description" style="width:93%" type="textarea" :rows=4></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="Save('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预览弹窗 -->
    <el-dialog title="预览" :visible.sync="viewdialog" width="900px">
      <table class="commonTable" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <th v-for="(item,index) in viewData.TitleList" :key="index">{{item}}</th>
        </tr>
        <tr v-for="(item,index) in viewData.ContenList" :key="index">
          <td v-for="(subItem,subIndex) in item" :key="subIndex">{{subItem}}</td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="viewdialog = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 选择图片文件 -->
    <el-dialog title="选择文件" :visible.sync="choicImg" width="1240px">
      <ChooseImg @SelectId="SelectId"></ChooseImg>
    </el-dialog>
  </div>
</template>
<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";
  export default {
    data() {
      return {
        loading: false,
        commonTitle: '新增',
        disList: [],
        msg: {
          pageIndex: 1,
          pageSize: 15,
          GradeName: ''
        },
        total: 0,
        tableData: [],
        dialogVisible: false,
        //预览弹窗
        viewdialog: false,
        addMsg: {
          Id: 0, //等级id
          Grade: 1, //等级
          GradeName: '', //等级名称
          IsGuest: 2, //是否普通会员 1普通会员 2其他会员 3Vip
          CommissionRatio: 0, //返佣比例
          SiblingRatio: 0, //同级返佣比例
          SiblingRebatesNum: 0, //可拿同级返佣的数量
          IsCanBuy: 1, //是否可以购买 1是 2否
          BuyMoney: 0, //购买金额
          IsUpgrade: 1, //是否可以升级 1是 2否
          UpgradeNum: 0, //升级所需下级等级数量
          CommissionType: 1, //返佣类型
          UpgradeId: 1, //升序所需下级等级id
          DecimalType: 1, //小数类型 
          Description: '', //描述
          Year: 1, //年份
          Icon: "", //图标
        },
        //分销商选择等级
        numList: [],
        //升序所需下级等级id
        UpgradeIdList: [],
        rules: {
          Grade: [{
            required: true,
            message: '请选择分销商等级',
            trigger: 'change'
          }],
          GradeName: [{
            required: true,
            message: '请输入分销商等级名称',
            trigger: 'blur'
          }],
          IsGuest: [{
            required: true,
            message: '是否普通会员',
            trigger: 'change'
          }],
        },
        //预览数据
        viewData: [],
        choicImg: false, //选择图片
      };
    },
    components: {
      ChooseImg,
    },
    created() {
      this.numList = []
      for (let i = 1; i < 11; i++) {
        let obj = {
          Id: i,
          dis: false,
        }
        this.numList.push(obj)
      }
    },
    methods: {
      getList() {
        this.loading = true;
        this.apipost("/api/user/GetFXDistributorGradePageList", this.msg, res => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            this.total = res.data.data.count;
            let pageData = res.data.data.pageData;
            this.tableData = pageData;
            this.numList.forEach(item => {
              this.tableData.forEach(val => {
                if (item.Id == val.Id) {
                  item.dis = true;
                }
              })
            })
          }
        })
      },
      getUpgradeId() {
        let msg = {
          GradeName: ''
        }
        this.apipost("/api/user/GetFXDistributorGradeList", msg, res => {
          if (res.data.resultCode == 1) {
            this.UpgradeIdList = res.data.data;
          }
        })
      },
      //新增
      AddDig() {
        this.commonTitle = '新增';
        this.dialogVisible = true;
        this.addMsg = {
          Id: 0,
          Grade: 1,
          GradeName: '',
          IsGuest: 2,
          CommissionRatio: 0, //返佣比例
          SiblingRatio: 0, //同级返佣比例
          SiblingRebatesNum: 0, //可拿同级返佣的数量
          IsCanBuy: 2, //是否可以购买 1是 2否
          BuyMoney: 0, //购买金额
          IsUpgrade: 2, //是否可以升级 1是 2否
          UpgradeNum: 0, //升级所需下级等级数量
          CommissionType: 1, //返佣类型
          UpgradeId: 1, //升序所需下级等级id
          DecimalType: 1, //小数类型 
          Description: '', //描述
          Year: 1 //年份
        }
      },
      //修改
      Edit(item) {
        this.commonTitle = '修改';
        this.dialogVisible = true;
        this.addMsg = Object.assign({}, item);
      },
      //删除
      Delete(row) {
        let that = this;
        that.Confirm("是否删除?", function () {
          that.apipost(
            "/api/user/DelFXDistributorGradeInfo", {
              GradeId: row.Id
            },
            res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getList();
              } else {
                that.Error(res.data.message);
              }
            },
            null
          );
        });
      },
      //保存
      Save(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.apipost("/api/user/SetFXDistributorGradeInfo", this.addMsg, res => {
              if (res.data.resultCode == 1) {
                this.Success(res.data.message);
                this.getList();
                this.dialogVisible = false;
              } else {
                this.Error(res.data.message);
              }
            })
          } else {
            return false;
          }
        });
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      //预览
      getView() {
        this.viewdialog = true;
        this.apipost("/api/user/GetFXDistributorGradeView", {}, res => {
          if (res.data.resultCode == 1) {
            this.viewData = res.data.data;
          } else {
            this.Error(res.data.message);
          }
        })
      },
      //选择图片
      SelectId(msg) {
        this.addMsg.Icon = this.getIconLink(msg.url);
        this.choicImg = false;
      },
    },
    mounted() {
      this.getList();
      this.getUpgradeId();
    }
  };

</script>
<style>
  .fxDistribution .content .searchInput {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }

  .fxDistribution .content .searchInput .el-input__inner {
    border: none;
    outline: none;
    height: 30px;
    line-height: 30px;
  }

  .fxDistribution .content .searchInput {
    line-height: normal;
    display: inline-table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    width: 250px;
    margin-right: 20px;
  }

  .fxDistribution .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    box-sizing: border-box;
  }

</style>
