<template>
  <div class="memberGroup">
    <div class="head-title">
      用户分组
      <el-button @click="EditGroup(null)" style="float: right; margin-top: -5px; margin-right: 10px" size="small"
        type="primary">新增
      </el-button>
    </div>
    <div class="content">
      <div style="display: flex; align-items: center; flex-wrap: wrap">
        <div class="searchInput" style="width: 210px">
          <el-input @keyup.enter.native="(msg.pageIndex = 1), getList()" @clear="(msg.pageIndex = 1), getList()"
            style="display: inline-block; width: 180px; height: 30px" placeholder="请输入名称" v-model="msg.GroupName"
            size="small" clearable maxlength="20">
          </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="GroupId" label="ID" width="100"> </el-table-column>
        <el-table-column prop="GroupName" label="分组名称" width="200"> </el-table-column>
        <el-table-column prop="LookGroupList" label="可查看分组" width="300">
          <template slot-scope="scope">
            <el-tag v-for="(item,index) in scope.row.LookGroupList" size="mini" style="margin-right:5px;" :key="index">
              {{item.GroupName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="LookGroupList" label="设为默认" width="300">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.IsDefault" active-color="#409EFF" :active-value="1" :inactive-value="0"
              @change="SetDefault(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="CreateDate" width="200" label="加入时间"></el-table-column>
        <el-table-column prop="address" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <img @click="EditGroup(scope.row)" style="width: 32px; height: 32px"
                src="../../assets/img/userman/edit.png" alt="" />
            </el-tooltip>

            <el-tooltip v-if="scope.row.IsDefault==0" class="item" effect="dark" content="删除" placement="top">
              <img @click="DeleteGroup(scope.row)" style="width: 32px; height: 32px"
                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" :current-page.sync="msg.pageIndex" layout="total,prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <!-- 新增修改用户分组 -->
    <el-dialog :title="commonTitle" :visible.sync="IsShowGroup" width="600px" :close-on-click-modal="false">
      <div>
        <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="150px">
          <el-form-item label="分组名称" prop="GroupName">
            <el-input v-model="addMsg.GroupName" class="w300" maxlength="30" placeholder="请输入分组名称"></el-input>
          </el-form-item>
          <el-form-item label="可查看分组">
            <el-select v-model="addMsg.GroupIdList" @visible-change="getgroupList" multiple class="w300">
              <el-option v-for="item in groupArray" :key="item.GroupId" :label="item.GroupName" :value="item.GroupId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="IsShowGroup = false">关 闭</el-button>
        <el-button size="small" type="primary" @click="SaveGroup('addMsg')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";
  import detail from "./components/seeDetail";
  export default {
    components: {
      ChooseImg,
      detail,
    },
    data() {
      return {
        loading: false,
        tableData: [],
        total: 0,
        commonTitle: '新增分组',
        msg: {
          pageIndex: 1,
          pageSize: 10,
          GroupName: "",
        },
        addMsg: {
          GroupId: 0,
          GroupName: "", //分组名称
          GroupIdList: [],
          IsDefault: 0,
        },
        IsShowGroup: false, //是否显示用户分组弹窗
        groupArray: [], //用户分组列表
        rules: {
          GroupName: [{
            required: true,
            message: '请填写分组名称',
            trigger: 'blur'
          }],
        },
      };
    },
    created() {
      this.getgroupList();
    },
    methods: {
      //新增修改用户分组
      EditGroup(obj) {
        if (obj) {
          this.commonTitle = '修改分组'
          this.addMsg.GroupId = obj.GroupId;
          this.addMsg.GroupName = obj.GroupName;
          this.addMsg.GroupIdList = obj.GroupIdList;
        } else {
          this.addMsg.GroupId = 0;
          this.addMsg.GroupName = '';
          this.addMsg.GroupIdList = [];
          this.addMsg.IsSpecial = 0;
          this.commonTitle = '新增分组'
        }
        this.IsShowGroup = true;
      },
      //设置默认分组
      SetDefault(item) {
        let that = this;
        var tipMsg = "是否设置【" + item.GroupName + "】为默认分组?";
        that.$confirm(tipMsg, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            that.apipost("/api/User/SetDefaultGroup", {
              GroupId: item.GroupId
            }, res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getList();
              }
            });
          })
          .catch(() => {
            if (item.IsDefault == 1) {
              item.IsDefault = 0;
            } else {
              item.IsDefault = 1;
            }
          });
      },
      //保存用户分组信息
      SaveGroup(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.apipost("/api/User/SetMemberGroup", this.addMsg, res => {
              if (res.data.resultCode == 1) {
                this.Success(res.data.message);
                this.getList();
                this.IsShowGroup = false;
              }
            });
          } else {
            return false;
          }
        });
      },
      //获取用户分组信息
      GetGroup(item) {
        this.apipost("/api/User/GetMemberGroup", {
          GroupId: item.GroupId
        }, res => {
        });
      },
      //删除分组
      DeleteGroup(item) {
        let that = this;
        that.Confirm("是否确定删除?", function () {
          that.apipost("/api/User/DeleteMemberGroup", {
            GroupId: item.GroupId
          }, res => {
            if (res.data.resultCode == 1) {
              that.Success(res.data.message);
              that.getList();
            }
          });
        });
      },
      getList() {
        this.loading = true;
        this.apipost("/api/User/GetMemberGroupPage", 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;
          } else {
            this.Error(res.data.message);
          }
        });
      },
      //分页改变
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      //获取分组下来列表
      getgroupList() {
        this.apipost("/api/User/GetMemberGroupList", {}, res => {
          if (res.data.resultCode == 1) {
            this.groupArray = res.data.data;
          }
        });
      }
    },
    mounted() {
      this.getList();
    },
  };

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

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

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

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

  .memberGroup .w300 {
    width: 300px !important;
  }

</style>