<template>
  <div class="app-container">
    <el-descriptions size="medium" >
      <el-descriptions-item label="用户总数">{{count.userNum}}</el-descriptions-item>
      <el-descriptions-item label="已注册用户">{{count.userRegisterNum}}</el-descriptions-item>
      <el-descriptions-item label="已绑定仪器用户数">{{count.userDeviceNum}}</el-descriptions-item>
    </el-descriptions>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" @keyup.enter.native="handleQuery" style="width: 50%">
          <el-option label="全部"></el-option>
          <el-option label="会员" :value="1"></el-option>
          <el-option label="游客" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入单个会员昵称进行搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="搜索多个请用英文逗号隔开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="搜索多个请用英文逗号隔开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
            clearable
            v-model="queryParams.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="省" prop="provinceId">
        <el-select v-model="queryParams.provinceId" @keyup.enter.native="handleQuery">
          <el-option label="广东省" value="19"></el-option>
          <el-option label="湖南省" value="18"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="cityId">
        <el-select v-model="queryParams.cityId" @keyup.enter.native="handleQuery">
          <el-option label="广州市" value="234"></el-option>
          <el-option label="珠海市" value="237"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区" prop="areaId">
        <el-select v-model="queryParams.areaId" @keyup.enter.native="handleQuery">
          <el-option label="天河区" value="2314"></el-option>
          <el-option label="黄埔区" value="2317"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户注册时间" prop="createTime">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仪器数量" prop="credit">
        <el-input-number v-model="queryParams.devicesNumStart" @keyup.enter.native="handleQuery"></el-input-number>
        至
        <el-input-number v-model="queryParams.devicesNumEnd" @keyup.enter.native="handleQuery"></el-input-number>
      </el-form-item>
      <el-form-item label="积分范围" prop="credit">
        <el-input-number v-model="queryParams.creditStart" @keyup.enter.native="handleQuery"></el-input-number>
        至
        <el-input-number v-model="queryParams.creditEnd" @keyup.enter.native="handleQuery"></el-input-number>
      </el-form-item>
      <el-form-item label="仪器绑定" prop="devicesTags">
        <el-select v-model="queryParams.devicesTags" multiple placeholder="请选择">
          <el-option
            v-for="item in devicesTags"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="购买渠道" prop="purchaseChannel">
        <el-select v-model="queryParams.purchaseChannel" multiple placeholder="请选择">
          <el-option
            v-for="item in purchaseChannel"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企微标签" prop="wecomTags">
        <el-select v-model="queryParams.wecomTags" multiple placeholder="请选择">
          <el-option
            v-for="item in miniProgramTags"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="小程序标签" prop="miniProgramTags">
        <el-select v-model="queryParams.miniProgramTags" multiple placeholder="请选择">
          <el-option
            v-for="item in wecomTags"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </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>
    </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="['system:member: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="['system:member: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="['system:member: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="['system:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="头像" align="center" prop="headimg" />
      <el-table-column label="会员昵称" align="center" prop="nickname" />
      <el-table-column label="用户编号" align="center" prop="id" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="生日" align="center" prop="birthday" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="area" />
      <el-table-column label="信息完善" align="center" prop="isCompleteInformation" />
      <el-table-column label="用户注册时间" align="center" prop="createTime" />
      <el-table-column label="仪器数量" align="center" prop="devicesNum" />
      <el-table-column label="小程序标签" align="center" prop="" />
      <el-table-column label="企微标签" align="center" prop="" />
      <el-table-column label="是否对接" align="center" prop="is_abutment" />
      <el-table-column label="积分" align="center" prop="credit" />
      <el-table-column label="即将过期积分" align="center" prop="" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="unionid" align="center" prop="unionid" />
      <el-table-column label="用户权限" align="center" prop="" />
      <el-table-column label="消息列表" align="center" prop="" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:member:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:member:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >开启打卡</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >开启活动</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >个人信息同步</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >积分同步</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="headimg">
          <el-input v-model="form.headimg" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="积分" prop="credit">
          <el-input v-model="form.credit" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="unionid" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入unionid" />
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-radio v-model="form.userType" label="0">游客</el-radio>
          <el-radio v-model="form.userType" label="1">会员</el-radio>
        </el-form-item>
        <el-form-item label="会员等级" prop="level">
          <el-input-number v-model="form.level" :min="1" :max="100" label="会员等级"></el-input-number>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机" />
        </el-form-item>
<!--        <el-form-item label="密码" prop="password">-->
<!--          <el-input v-model="form.password" placeholder="请输入密码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="个人二维码" prop="userQr">-->
<!--          <el-input v-model="form.userQr" placeholder="请输入个人二维码" />-->
<!--        </el-form-item>-->
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="area">
          <el-input v-model="form.area" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="参与打卡" prop="clock">
          <el-input v-model="form.clock" placeholder="请输入1可以2不可以" />
        </el-form-item>
        <el-form-item label="参与活动" prop="activity">
          <el-input v-model="form.activity" placeholder="请输入1可以2不可以" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="1 未对接到中控 2已经对接 3对接失败" prop="isAbutment">
          <el-input v-model="form.isAbutment" placeholder="请输入1 未对接到中控 2已经对接 3对接失败" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </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>
  </div>
</template>

<script>
import {
  listMember,
  getMember,
  delMember,
  addMember,
  updateMember,
  selectUserCount,
  getMiniProgramTags,
  getWecomTags,
  getPurchaseChannel,
} from "@/api/system/member";

export default {
  name: "Member",
  data() {
    return {
      devicesTags: [{
        value: '仪器标签1',
        label: '仪器标签1'
      }, {
        value: '仪器标签2',
        label: '仪器标签2'
      }, {
        value: '仪器标签3',
        label: '仪器标签3'
      }],
      miniProgramTags: [],
      wecomTags: [],
      purchaseChannel: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      count: {
        userNum: 0,
        userRegisterNum: 0,
        userDeviceNum: 0
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        headimg: null,
        username: null,
        devicesNumStart: undefined,
        devicesNumEnd: undefined,
        creditStart: undefined,
        creditEnd: undefined,
        openid: null,
        unionid: null,
        userType: null,
        level: null,
        mobile: null,
        password: null,
        userQr: null,
        provinceId: null,
        province: null,
        cityId: null,
        city: null,
        areaId: null,
        area: null,
        birthday: null,
        clock: null,
        activity: null,
        wechat: null,
        code: null,
        isAbutment: null,
        collagenDay: null,
        collagenMount: null,
        loginTime: null,
        status: null,
        // createTime: [],
        devicesTags: [],  //仪器绑定标签
        wecomTags:[], // 企微标签
        miniProgramTags: [], // 小程序标签
        purchaseChannel: null, // 购买渠道
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 获取用户统计信息
    this.getUserCount();
    // 获取表单数据
    this.getList();
    // 获取标签
    this.getTags();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listMember(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUserCount() {
      selectUserCount().then(Response => {
        this.count.userNum = Response.data[0];
        this.count.userRegisterNum = Response.data[1];
        this.count.userDeviceNum = Response.data[2];
      })
    },
    getTags() {
      // 小程序标签
      getMiniProgramTags().then(Response => {
        this.miniProgramTags = Response.data
      })
      // 微信标签
      getWecomTags().then(Response => {
        this.wecomTags = Response.data
      })
      // 购买渠道
      getPurchaseChannel().then(Response => {
        this.purchaseChannel = Response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickname: null,
        headimg: null,
        username: null,
        credit: null,
        openid: null,
        unionid: null,
        userType: null,
        level: null,
        mobile: null,
        password: null,
        userQr: null,
        province: null,
        city: null,
        area: null,
        birthday: null,
        clock: null,
        activity: null,
        wechat: null,
        code: null,
        isAbutment: null,
        collagenDay: null,
        collagenMount: null,
        loginTime: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      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 = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(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 delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
