<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级邀请码" prop="parentCode">
        <el-input
          v-model="queryParams.parentCode"
          placeholder="请输入上级邀请码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开通指南" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="请选择是否开通指南" clearable>
          <el-option
            v-for="dict in dict.type.member_user_is_open"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否开通帐号" prop="isSysUser">
        <el-select v-model="queryParams.isSysUser" placeholder="请选择" clearable size="small">
          <el-option label="未开通" :value="0" />
          <el-option label="已开通" :value="1" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="开通时间" prop="openAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.openAt"-->
<!--          placeholder="请输入开通时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="到期时间" prop="expireAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.expireAt"-->
<!--          placeholder="请输入到期时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- 时间筛选 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateAt"
          size="small"
          style="width: 215px !important"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始"
          end-placeholder="结束"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <div class="statisticsTitle">开通指南人数： {{ openTotal }}</div>
      <div class="statisticsTitle">未开通指南人数： {{ noOpenTotal }}</div>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['mall:mallMemberUser:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['mall:mallMemberUser:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:mallMemberUser:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mall:mallMemberUser:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallMemberUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="用户名称" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="用户头像" align="center" prop="avatar" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" width="50px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column label="邀请人" align="center" prop="introUserName" >
        <template slot-scope="scope">
          <span v-if="scope.row.introUserName !=null">{{scope.row.introUserName}}</span>
          <span v-if="scope.row.introUserName ==null">自主搜索</span>
        </template>
      </el-table-column>

      <el-table-column label="邀请码" align="center" prop="invitationCode" />
      <!--<el-table-column label="小程序码" align="center" prop="qrCode" >-->
      <!--  <template slot-scope="scope">-->
      <!--    <image-preview :src="scope.row.qrCode" width="50px" height="50px"/>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column label="下级人数" align="center" prop="childrenNum" />
      <el-table-column label="我的下级" align="center" >
        <template slot-scope="scope">
          <el-link type="primary" @click="getChildren(scope.row)"  >下级详情</el-link>
        </template>
      </el-table-column>
<!--      <el-table-column label="openId" align="center" prop="openid" />-->
<!--      <el-table-column label="微信号" align="center" prop="wechat" />-->
<!--      <el-table-column label="性别：0-未知，1-男，2-女" align="center" prop="sex" />-->
<!--      <el-table-column label="用户密码(授权登录的话 暂时没用)" align="center" prop="password" />-->
<!--      <el-table-column label="token" align="center" prop="token" />-->
<!--      <el-table-column label="账户状态：0-正常，1-冻结" align="center" prop="status" />-->
<!--      <el-table-column label="余额" align="center" prop="money" />-->
<!--      <el-table-column label="历史消费金额" align="center" prop="allMoney" />-->
<!--      <el-table-column label="分享时的二维码图片" align="center" prop="qrCode" />-->
<!--      <el-table-column label="用户等级" align="center" prop="userLevel" />-->
<!--      <el-table-column label="省id" align="center" prop="provinceId" />-->
<!--      <el-table-column label="市id" align="center" prop="cityId" />-->
<!--      <el-table-column label="区" align="center" prop="areaId" />-->
<!--      <el-table-column label="省名" align="center" prop="provinceName" />-->
<!--      <el-table-column label="市名" align="center" prop="cityName" />-->
<!--      <el-table-column label="区名" align="center" prop="areaName" />-->
<!--      <el-table-column label="详细地址" align="center" prop="address" />-->
<!--      <el-table-column label="科目ID" align="center" prop="subjectId" />-->
<!--      <el-table-column label="科目名" align="center" prop="subjectName" />-->
      <el-table-column label="开通指南" align="center" prop="isOpen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.member_user_is_open" :value="scope.row.isOpen"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="开通指南类型ID" align="center" prop="openTypeId" />-->
<!--      <el-table-column label="开通指南类型名" align="center" prop="openTypeName" />-->
      <el-table-column label="开通时间" align="center" prop="openAt" />
      <el-table-column label="到期时间" align="center" prop="expireAt" />
      <el-table-column label="注册时间" align="center" prop="createAt" />
      <el-table-column label="小程序首页名" align="center" prop="indexName" />
      <el-table-column label="小程序二维码" align="center" prop="indexCode" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.indexCode" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="小程序图" align="center" prop="accept" >
        <template slot-scope="scope">
          <el-link type="primary" @click="getMemberBanner(scope.row)">小程序图</el-link>
        </template>
      </el-table-column>

      <!--<el-table-column label="登录时间" align="center" prop="loginAt" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:mallMemberUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goPersonReport(scope.row)"
            v-hasPermi="['mall:mallMemberUser:memberReport']"
          >志愿报告</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallMemberUser:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddUser(scope.row)"
            v-hasPermi="['mall:mallMemberUser:addUser']"
          >{{scope.row.sysUserId === 0 ? '新增账号' : '修改账号户'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <image-upload v-model="form.avatar" limit="1" />
        </el-form-item>

        <el-form-item label="推荐人" prop="introUserName">
          <el-input v-model="form.introUserName" placeholder="请输入推荐人"  />
        </el-form-item>
        <el-form-item label="邀请码" prop="invitationCode">
          <el-input v-model="form.invitationCode" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item label="小程序码" prop="qrCode" v-show="form.qrCode !=null">
          <image-upload v-model="form.qrCode" limit="1" />
        </el-form-item>
        <el-form-item label="开通指南">
          <el-radio-group v-model="form.isOpen">
            <el-radio
              v-for="dict in dict.type.member_user_is_open"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="下级列表" :visible.sync="openChildren" width="1000px" append-to-body>
      <el-form :model="childrenQueryParams" ref="childrenQueryParams" size="small" :inline="true" v-show="showSearch" label-width="90px">
            <el-form-item label="开通指南" prop="isOpen">
              <el-select v-model="childrenQueryParams.isOpen" placeholder="请选择是否开通指南" clearable>
                <el-option
                  v-for="dict in dict.type.member_user_is_open"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="selectChildrenList">搜索</el-button>
              <!-- <el-button icon="el-icon-refresh" size="mini" @click="subresetQuery">重置</el-button> -->
            </el-form-item>
      </el-form>
      <el-table  :data="childrenList" @selection-change="handleSelectionChange">
        <el-table-column label="用户名称" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="用户头像" align="center" prop="avatar" width="120">
          <template slot-scope="scope">
            <image-preview :src="scope.row.avatar" width="50px" height="50px"/>
          </template>
        </el-table-column>
        <el-table-column label="邀请人" align="center" prop="introUserName" />
        <el-table-column label="开通指南" align="center" prop="isOpen">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.member_user_is_open" :value="scope.row.isOpen"/>
            </template>
        </el-table-column>
        <el-table-column label="邀请码" align="center" prop="invitationCode" />
        <el-table-column label="下级人数" align="center" prop="childrenNum" />
        <el-table-column label="我的下级" align="center" >
          <template slot-scope="scope">
            <el-link type="primary" @click="getChildren(scope.row)"  >下级详情</el-link>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" align="center" prop="createAt" />

      </el-table>
      <pagination
        v-show="childrenTotal>0"
        :total="childrenTotal"
        :page.sync="childrenQueryParams.pageNum"
        :limit.sync="childrenQueryParams.pageSize"
        @pagination="selectChildrenList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改系统用户对话框 -->
    <el-dialog :title="title" :visible.sync="openAddUser" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="小程序首页名" prop="indexName">
          <el-input v-model="form.indexName" placeholder="请输入小程序首页名" />
        </el-form-item>
        <el-form-item label="小程序首页logo" prop="indexLogo">
          <image-upload v-model="form.indexLogo" limit="1" />
        </el-form-item>
        <el-form-item label="小程序邀请价" prop="indexPrice">
          <el-input v-model="form.indexPrice" placeholder="请输入小程序邀请价" type="number" :min="0.1" />
        </el-form-item>
        <el-form-item label="账号" prop="sysUserName">
          <el-input v-model="form.sysUserName" placeholder="请输入账号" maxlength="30" />
        </el-form-item>
        <el-form-item label="密码" prop="sysUserPassword">
          <el-input v-model="form.sysUserPassword" placeholder="请输入密码" type="password" maxlength="20" show-password/>
        </el-form-item>
        <el-form-item label="帐号状态" prop="sysUserStatus">
          <el-radio-group v-model.number="form.sysUserStatus">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddUser">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--小程序图数据-->
    <el-dialog title="小程序图" :visible.sync="openMemberBanner" width="1000px" append-to-body>
      <mall-member-banner :member-user-id="memberUserId" :key="memberUserId" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="diaCancel">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listMallMemberUser,
  getMallMemberUser,
  delMallMemberUser,
  addMallMemberUser,
  updateMallMemberUser,
  getMyChildren, submitUser, setIsOpenStatisticsNum
} from "@/api/mall/mallMemberUser";
import {getMallSchool} from "@/api/mall/mallSchool";
import MallMemberBanner from "@/views/mall/mallMemberBanner";

export default {
  name: "MallMemberUser",
  components: {MallMemberBanner},
  dicts: ['member_user_is_open'],
  data() {
    return {
      // 小程序图开关
      openMemberBanner: false,
      // 开通指南人数
      openTotal: 0,
      // 未开通指南人数
      noOpenTotal: 0,
      openAddUser: false,
      // 下级弹窗开关
      openChildren: false,
      // 下级列表
      childrenList: [],
      // 下级数据总数
      childrenTotal: 0,
      // 创建时间时间范围
      daterangeCreateAt: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户会员表格数据
      mallMemberUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        avatar: null,
        name: null,
        phone: null,
        openid: null,
        wechat: null,
        sex: null,
        password: null,
        token: null,
        status: null,
        introUser: null,
        invitationCode: null,
        parentCode: null,
        money: null,
        allMoney: null,
        qrCode: null,
        userLevel: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        provinceName: null,
        cityName: null,
        areaName: null,
        address: null,
        subjectId: null,
        subjectName: null,
        isOpen: null,
        openTypeId: null,
        openTypeName: null,
        openAt: null,
        expireAt: null,
        createAt: null,
        updateAt: null,
        loginAt: null,
        isSysUser: null,
      },
      childrenQueryParams: {
        pageNum: 1,
        pageSize: 10,
        introUser: null,
        isOpen: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysUserName: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          { min: 5, max: 20, message: '账号长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        sysUserPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: '密码长度必须介于 6 和 20 之间', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 小程序图 */
    getMemberBanner(row){
      this.memberUserId = row.id;
      this.openMemberBanner = true;
    },
    diaCancel(){
      this.openMemberBanner = false;
    },

    getIsOpenNum() {
      setIsOpenStatisticsNum().then((response) => {
        this.openTotal = response.data.openTotal;
        this.noOpenTotal = response.data.noOpenTotal;
      });
    },
    /** 新增登录用户按钮操作 */
    handleAddUser(row) {
      this.reset();
      const id = row.id
      getMallMemberUser(id).then(response => {
        this.form = response.data;
        this.openAddUser = true;
        this.title = "新增登录用户";
      });
    },
    /** 新增登录用户提交按钮 */
    submitAddUser() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.schoolId != null) {
            submitUser(this.form).then(response => {
              this.$modal.msgSuccess("成功");
              this.openAddUser = false;
              this.getList();
            });
          }
        }
      });
    },

    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.form.avatar = response.url;
    },
    /** 查询用户会员列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateAt && "" != this.daterangeCreateAt) {
        this.queryParams.params["beginCreateAt"] = this.daterangeCreateAt[0];
        this.queryParams.params["endCreateAt"] = this.daterangeCreateAt[1];
      }

      listMallMemberUser(this.queryParams).then(response => {
        this.mallMemberUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getIsOpenNum();
    },
    // 取消按钮
    cancel() {
      this.openAddUser = false;
      this.open = false;
      this.openChildren = false;
      this.childrenList = []
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        indexPrice: null,
        indexName: null,
        indexLogo: null,
        sysUserName: null,
        sysUserPassword: null,
        sysUserStatus: 0,
        id: null,
        avatar: null,
        name: null,
        phone: null,
        openid: null,
        wechat: null,
        sex: null,
        password: null,
        token: null,
        status: "0",
        introUser: null,
        introUserName: null,
        invitationCode: null,
        money: null,
        allMoney: null,
        qrCode: null,
        userLevel: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        provinceName: null,
        cityName: null,
        areaName: null,
        address: null,
        subjectId: null,
        subjectName: null,
        isOpen: "0",
        openTypeId: null,
        openTypeName: null,
        openAt: null,
        expireAt: null,
        createAt: null,
        updateAt: null,
        loginAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateAt = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户会员";
    },
    /** 个人报告 */
    goPersonReport(row){
        this.$router.push({path:'/report/MemberReportIntelligence',query:{userId:row.id}});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMallMemberUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户会员";
      });

      this.upload.fileList = [];
      this.upload.fileList = [{ name: this.form.fileName, url: row.avatar }];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMallMemberUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallMemberUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户会员编号为"' + ids + '"的数据项？').then(function() {
        return delMallMemberUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/mallMemberUser/export', {
        ...this.queryParams
      }, `mallMemberUser_${new Date().getTime()}.xlsx`)
    },
    /** 我的下级 */
    getChildren(row){
      this.introUser = row.id;
      this.selectChildrenList()
    },
    selectChildrenList(){
      this.childrenQueryParams.introUser = this.introUser;
      getMyChildren(this.childrenQueryParams).then(response => {
        this.childrenList = response.rows;
        this.childrenTotal = response.total;
        this.openChildren = true;
      });
    }
  }
};
</script>
