<template>
  <div class="distributors usersList">
    <div class="head-title">
      分销商列表
      <el-button @click="dialogVisible=true" style="float:right;margin-top: -5px;" size="small" type="primary">批量导出
      </el-button>
    </div>
    <div class="content">
      <div>
        <el-select class="w100" @change="getList" style="margin-right: 10px;" v-model="msg.GradeId" size="small"
          placeholder="请选择">
          <el-option label="全部等级" :value="-1"></el-option>
          <el-option label="默认等级" :value="0"></el-option>
          <el-option v-for="item in DropdownList" :key="item.Id" :label="item.Name" :value="item.Id">
          </el-option>
        </el-select>
        <el-select class="w100" @change="getList" style="margin-right: 10px;" v-model="msg.Source" size="small"
          placeholder="请选择">
          <el-option label="全部平台" :value="0"></el-option>
          <el-option v-for="item in platList" :key="item.Id" :label="item.Name" :value="item.Id">
          </el-option>
        </el-select>
        <div class="searchInput" style="width:150px">
          <el-input style="display:inline-block;width:125px;height:30px" placeholder="请输入ID" v-model="msg.Id"
            size="small" clearable>
          </el-input>
          <span @click="getList" class="el-icon-search"
            style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
        </div>
        <div class="searchInput" style="width:150px">
          <el-input style="display:inline-block;width:125px;height:30px" placeholder="请输入昵称" v-model="msg.Name"
            size="small" clearable>
          </el-input>
          <span @click="getList" class="el-icon-search"
            style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
        </div>
        <div class="searchInput" style="width:150px">
          <el-input style="display:inline-block;width:125px;height:30px" placeholder="请输入手机号" v-model="msg.Moblie"
            size="small" clearable>
          </el-input>
          <span @click="getList" class="el-icon-search"
            style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
        </div>
        <el-button @click="plSet" size="small" type="primary">批量设置</el-button>
        <el-button @click="addUserBtn" style="float:right" size="small" type="primary">添加分销商</el-button>

      </div>
      <div style="margin-top:20px">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="全部" name="first" :dataId="0"></el-tab-pane>
          <el-tab-pane v-for="(item,index) in AuditStatusList" :dataId="item.Id" :key="index" :label="item.Name"
            :name="item.Name"></el-tab-pane>
        </el-tabs>
      </div>
      <el-table class="table_samll" :data="tableData" v-loading="loading" border @select-all="CheckAllTable"
        @select="SingelCheck" style="width: 100%;margin:0 0 20px 0">
        <el-table-column type="selection">
        </el-table-column>
        <el-table-column prop="Id" label="用户ID">
        </el-table-column>
        <el-table-column prop="address" label="基本信息">
          <template slot-scope="scope">
            <div class="app-image" :style="{backgroundImage:'url(' + scope.row.Photo + ')',backgroundSize:'cover'}">
            </div>
            <div flex="dir:left cross:center">
              {{scope.row.NickName}}
            </div>
            <img v-if="scope.row.Source==1" style="width:24px;height:24px" src="../../assets/img/userman/wx.png" alt=""
              class="platform-img">
          </template>
        </el-table-column>
        <el-table-column prop="Moblie" label="姓名">
          <el-table-column prop="Moblie" label="手机号">
            <template slot-scope="scope">
              <p>{{scope.row.Name}}</p>
              <p>{{scope.row.Mobile}}</p>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="Moblie" label="可提现佣金">
          <el-table-column prop="Moblie" label="累积佣金">
            <template slot-scope="scope">
              <p>{{scope.row.CommissionWithdrawal.toFixed(2)}}</p>
              <p>{{scope.row.TotalCommission.toFixed(2)}}</p>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column prop="SuperiorName" label="推荐人">
        </el-table-column>
        <el-table-column prop="name" label="下级用户">
          <template slot-scope="scope">
            <p @click="getReferrals(1,scope.row)" class="blue point">一级：{{scope.row.OneNum}}</p>
            <p @click="getReferrals(2,scope.row)" class="blue point">二级：{{scope.row.TwoNum}}</p>
            <p @click="getReferrals(3,scope.row)" class="blue point">三级：{{scope.row.ThreeNum}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="分销商等级">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.GradeName=='默认等级'" type="info">{{scope.row.GradeName}}</el-tag>
            <el-tag v-else>{{scope.row.GradeName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.AuditStatusName=='待审核'" type="info">{{scope.row.AuditStatusName}}</el-tag>
            <el-tag v-else>{{scope.row.AuditStatusName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="时间">
          <template slot-scope="scope">
            <p>申请时间：</p>
            <span>{{scope.row.CreateDate}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Remark" label="备注信息">
        </el-table-column>
        <el-table-column prop="address" width="200" label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="同意" placement="top-start">
              <img v-if="scope.row.AuditStatus==1" @click="openReason(1,scope.row)" style="width:32px;height:32px"
                src="../../assets/img/userman/pass.png" alt="">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="拒绝" placement="top-start">
              <img v-if="scope.row.AuditStatus==1" @click="openReason(2,scope.row)" style="width:32px;height:32px"
                src="../../assets/img/userman/nopass.png" alt="">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="修改分销商等级" placement="top-start">
              <img @click="openReason(4,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-start">
              <img @click="openRemark(5,scope.row)" style="width:32px;height:32px"
                src="../../assets/img/userman/add_remark.png" alt="">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
              <img @click="Delete(3,scope.row)" style="width:32px;height:32px" src="../../assets/img/userman/del.png"
                alt="">
            </el-tooltip>
            <img @click="OpenczjfDig(scope.row,1)" style="width:32px;height:32px;"
              src="../../assets/img/userman/integral.png" alt="">
            <img @click="OpenczjfDig(scope.row,2)" style="width:32px;height:32px"
              src="../../assets/img/userman/balance.png" alt="">
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align:right" background @current-change="handleCurrentChange" :page-size="msg.pageSize"
        layout="prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <!-- 选择导出信息 -->
    <el-dialog title="选择导出信息" :visible.sync="dialogVisible" width="960px">
      <el-form style="border: 1px solid #F0F2F7;" :model="addMsg" ref="addMsg" label-width="100px">
        <div
          style="box-sizing: border-box;background-color: #F3F5F6;width: 100%;padding-left: 20px;height: 50px;line-height: 50px;">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
          </el-checkbox>
        </div>
        <div style="margin: 15px 0;"></div>
        <div style="padding: 10px 25px 20px;">
          <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
            <el-checkbox style="margin-bottom:10px" v-for="city in cities" :label="city.Id" :key="city.Name">
              {{city.Name}}</el-checkbox>
          </el-checkbox-group>
        </div>

      </el-form>
      <div style="text-align:right;margin-top:20px">
        <el-button size="small" type="primary" @click="Export">导出</el-button>
      </div>
    </el-dialog>
    <!-- 审批同意或拒绝单个修改等级 -->
    <el-dialog title="提示" :visible.sync="reasonDig" width="500px">
      <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="0">
        <template v-if="addMsg.Type==1 || addMsg.Type==2">
          <p style="margin-bottom:10px">请输入原因</p>
          <el-form-item>
            <el-input size="small" v-model="addMsg.Remark"></el-input>
          </el-form-item>
        </template>

        <el-form-item label-width="100px" v-if="addMsg.Type==4" label="分销商等级">
          <el-select class="w200" v-model="addMsg.NewGradeId" size="small" placeholder="请选择">
            <el-option label="默认等级" :value="0"></el-option>
            <el-option v-for="item in DropdownList" :key="item.Id" :label="item.Name" :value="item.Id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="reasonDig = false">取 消</el-button>
        <el-button size="small" type="primary" @click="czsubmitForm('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 批量修改 -->
    <el-dialog custom-class="plxg" title="批量修改" :visible.sync="plDig" width="960px">
      <span class="absoSpan">已选分销商{{checllen.length}}个</span>
      <el-form class="app-batch" :model="addMsg" :rules="rules" ref="addMsg" label-width="100px">
        <div class="batch-remark">注：每次只能修改一项，修改后点击确定即可生效。如需修改多项，需多次操作。</div>
        <div style="margin-top:15px" flex="dir:left box:first">
          <div flex="dir:top" class="batch-box-left">
            <div flex="main:center" class="batch-div-active">
              分销商等级
            </div>
          </div>
          <div class="batch-box-right">
            <el-form-item label="分销商等级">
              <el-select class="w200" v-model="addMsg.NewGradeId" size="small" placeholder="请选择">
                <el-option label="默认等级" :value="0"></el-option>
                <el-option v-for="item in DropdownList" :key="item.Id" :label="item.Name" :value="item.Id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="plDig = false">取 消</el-button>
        <el-button size="small" type="primary" @click="czsubmitForm('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加备注 -->
    <el-dialog title="添加备注" :visible.sync="remarkDig" width="850px">
      <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="0">
        <p style="margin:0 0 15px 0">备注</p>
        <el-form-item>
          <el-input type="textarea" row="2" size="small" v-model="addMsg.Remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="remarkDig = false">取 消</el-button>
        <el-button size="small" type="primary" @click="czsubmitForm('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分销商 -->
    <el-dialog title="添加备注" :visible.sync="adduserDig" width="600px">
      <el-form :model="adduserMsg" :rules="userrules" ref="adduserMsg" label-width="80px">
        <el-form-item label="用户昵称">
          <el-select size="small" v-model="adduserMsg.UserId" :filter-method="ChangeListName" filterable
            placeholder="请选择">
            <el-option v-for="item in userData" :key="item.Id" :label="item.Name" :value="item.Id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="adduserDig = false">取 消</el-button>
        <el-button size="small" type="primary" @click="adduserForm('adduserMsg')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 下线情况 -->
    <el-dialog title="下线情况" :visible.sync="referDig" width="768px">
      <el-table v-loading="loading1" :data="tableData1" border style="width: 100%">
        <el-table-column prop="ReferralsType" label="序号">
        </el-table-column>
        <el-table-column prop="DistributorName" label="分销商">
        </el-table-column>
        <el-table-column prop="Name" label="昵称">
        </el-table-column>
        <el-table-column prop="CreateDate" label="成为下线时间">
        </el-table-column>
      </el-table>
      <div style="text-align:right;margin-top:20px">
        <el-button size="small" @click="referDig=false">取消</el-button>
        <el-button size="small" type="primary" @click="referDig=false">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";
  export default {
    components: {
      ChooseImg
    },
    data() {
      return {
        adduserDig: false,
        remarkDig: false,
        plDig: false,
        activeName: 'first',
        czType: 1,
        changeState: false,
        loading: false,
        checkAll: false,
        checkedCities: [],
        cities: [],
        cityOptions: [],
        isIndeterminate: true,
        dialogVisible: false,
        value: '',
        options: [],
        tableData: [],
        msg: {
          pageIndex: 1,
          pageSize: 15,
          UserId: 0,
          AuditStatus: 0,
          GradeId: -1,
          Name: '',
          Source: 0,
          NickName: '',
          Moblie: ''
        },
        total: 0,
        platList: [],
        memberGradeList: [],
        userrules: {
          UserId: [{
            required: true,
            message: '请选择用户',
            trigger: 'change'
          }],
        },
        rules: {
          Type: [{
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }],
          Integral: [{
            required: true,
            message: '请输入积分',
            trigger: 'blur'
          }],
        },
        addMsg: {
          UserIds: 0,
          Type: 1,
          NewGradeId: 0,
          Remark: '',
        },
        imgurl: '',
        AuditStatusList: [],
        reasonDig: false,
        DropdownList: [],
        checktableList: [],
        checllen: 0,
        usermsg: {
          pageIndex: 1,
          pageSize: 20,
          Name: '',
          Source: 0,
          MemberGrade: 0,
          Id: 0,
          Moblie: ''
        },
        userData: [],
        adduserMsg: {
          UserId: '',
          IsFormSelfApplication: 2,
          Name: '',
          Mobile: '',
        },
        referMsg: {
          UserId: 0,
          ReferralsType: 0,
        },
        referDig: false,
        loading1: false,
        tableData1: [],
      };
    },
    created() {
      this.getList();
      this.getplat();
      this.getDown();
      this.GetMemberGradeList();
      this.GetAuditStatus();
      this.GetDropdownList();
      this.getUserList();
    },
    methods: {
      getReferrals(type, row) {
        this.referMsg.ReferralsType = type;
        this.referMsg.UserId = row.Id;
        this.referDig = true;
        this.getReferralsList();
      },
      getReferralsList() {
        this.loading1 = true;
        this.apipost("/api/user/GetDistributorInfoReferralsList", this.referMsg, res => {
          this.loading1 = false;
          if (res.data.resultCode == 1) {
            let pageData = res.data.data.pageData;
            this.tableData1 = pageData;
          }

        })
      },
      // 申请
      adduserForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let cmd = "/api/user/SetDistributorInfo";
            this.apipost(cmd, this.adduserMsg,
              res => {
                if (res.data.resultCode === 1) {
                  this.getList();
                  this.Success(res.data.message)
                  this.adduserDig = false;
                } else {
                  this.Error(res.data.message);
                }
              },
              null
            );
          } else {
            return false;
          }
        });
      },
      addUserBtn() {
        this.adduserDig = true;
        this.adduserMsg = {
          UserId: '',
          IsFormSelfApplication: 2,
          Name: '',
          Mobile: '',
        }
      },
      ChangeListName(val) {
        this.usermsg.Name = val;
        this.getUserList();
      },
      getUserList() {
        this.loading = true;
        this.apipost("/api/user/GetMemberUserDropDownList", this.usermsg, res => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            let pageData = res.data.data.pageData;
            this.userData = pageData;
          }

        })
      },
      Delete(num, row) {
        this.addMsg.Type = num;
        this.addMsg.UserIds = row.UserId;
        let that = this;
        that.Confirm("是否删除?", function () {
          that.apipost(
            "/api/user/SetDistributorAudit",
            that.addMsg,
            res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getList();
              } else {
                that.Error(res.data.message);
              }
            },
            null
          );
        });
      },
      plSet() {
        let arr = this.unique(this.checktableList);
        this.checllen = this.unique(this.checktableList);

        if (arr.length == 0) {
          this.Error("至少选择一项！");
          return;
        }
        this.addMsg.UserIds = arr.join(",");
        this.plDig = true;
        this.addMsg.Type = 4;
      },
      // 数组去重
      unique(arr) {
        return arr.filter(function (item, index, arr) {
          //当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素
          return arr.indexOf(item, 0) === index;
        });
      },
      CheckAllTable(selection) {
        selection.forEach(item => {
          this.checktableList.push(item.UserId);
        })
      },
      SingelCheck(selection, row) {
        this.checktableList.push(row.UserId);
      },
      openRemark(num, row) {
        this.addMsg.Type = num;
        this.addMsg.UserIds = row.UserId;
        this.addMsg.Remark = row.Remark;
        this.remarkDig = true;
      },
      openReason(num, row) {
        this.addMsg.Type = num;
        this.addMsg.UserIds = row.UserId;
        this.addMsg.NewGradeId = row.GradeId;
        this.reasonDig = true;
      },
      handleClick(val) {
        this.msg.AuditStatus = val.$attrs.dataId;
        this.getList();
      },
      SelectId(msg) {
        this.changeState = false;
        this.addMsg.Image = msg.id;
        this.imgurl =this.getIconLink(msg.url);
      },
      EditgoUrl(row) {
        this.$router.push({
          name: 'usersListEdit',
          query: {
            UserId: row.Id,
            blank: "y"
          }
        });
      },
      czsubmitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let cmd = "/api/user/SetDistributorAudit";
            this.apipost(cmd, this.addMsg,
              res => {
                if (res.data.resultCode === 1) {
                  this.getList();
                  this.Success(res.data.message)
                  this.reasonDig = false;
                  this.remarkDig = false;
                  this.plDig = false;

                } else {
                  this.Error(res.data.message);
                }
              },
              null
            );
          } else {
            return false;
          }
        });
      },
      openChangeDig() {
        this.changeState = true;
        setTimeout(() => {
          this.$refs.mychild.InitData(this.czType);
        }, 10)
      },
      Export() {
        this.msg.ExcelEnumIds = this.checkedCities;
        let msg = JSON.parse(JSON.stringify(this.msg));
        this.JavaGetLocalFile(
          "/api/user/GetDistributorListToExcel",
          msg,
          "分销商.xls"
        );
      },
      ChangeOpenidState(index, row) {
        this.tableData[index].OpenIdState = !this.tableData[index].OpenIdState;
      },
      getList() {
        this.loading = true;
        this.apipost("/api/user/GetDistributorInfoPageList", 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;
          }
        })
      },
      GetDropdownList() {
        let msg = {
          Id: 0,
          Grade: 0,
          Name: '',
          Enabled: 0,
        }
        this.apipost("/api/user/GetDistributorGradeDropdownList", msg, res => {
          this.DropdownList = res.data.data;
        })
      },
      GetAuditStatus() {
        this.apipost("/api/user/GetDistributorAuditStatusEnumList", {}, res => {
          this.AuditStatusList = res.data.data;
        })
      },
      getDown() {
        this.cityOptions = [];
        this.apipost("/api/user/GetDistributorExportEnumList", {}, res => {
          this.cities = res.data.data;
          let data = res.data.data;
          data.forEach(item => {
            this.cityOptions.push(item.Id)
          })

        })
      },
      GetMemberGradeList() {
        let msg = {
          Grade: 0,
          Name: '',
          Enabled: 0,
        };
        this.apipost("/api/user/GetMemberGradeList", msg, res => {
          this.memberGradeList = res.data.data;
        })
      },
      getplat() {
        this.apipost("/api/user/GetMemberUserSourceEnumList", {}, res => {
          this.platList = res.data.data;
        })
      },
      handleCheckAllChange(val) {
        this.checkedCities = val ? this.cityOptions : [];
        this.isIndeterminate = false;
      },
      handleCheckedCitiesChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.cities.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },

    },
    mounted() {

    }
  };

</script>
<style>
  .table_samll .cell img {
    margin-right: 10px;
  }

  .plxg .el-dialog__body {
    padding-top: 10px !important;
  }

  .plxg .app-batch .batch-box-left div {
    padding: 5px 0;
    margin: 5px 0;
    cursor: pointer;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
  }

  .plxg .app-batch .batch-div-active {
    background-color: #e2e2e2;
  }

  .plxg .app-batch .batch-box-right {
    padding: 5px 20px;
  }

  .plxg .app-batch .batch-box-left {
    width: 120px;
    border-right: 1px solid #e2e2e2;
    padding: 0 20px;
  }

  .plxg .app-batch .batch-remark {
    margin-top: 5px;
    color: #999999;
    font-size: 14px;
  }

  .distributors .absoSpan {
    position: absolute;
    top: 22px;
    left: 100px;
  }

  .table_samll td,
  .table_samll th {
    padding: 8px 0 !important;
    font-size: 12px;
  }

  .usersList .remark_name {
    color: #888888;
    font-size: 12px;
    margin-left: 10px;
    float: right;
  }

  .usersList .app-image {
    background-position: center center;
    width: 50px;
    height: 50px;
    border-radius: 0%;
    float: left;
    margin-right: 8px;
  }

  .usersList .blue {
    color: #409EFF;

  }

  .usersList .content .searchInput {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }

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

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

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

</style>
