<template>
  <div class="memberGroupList-wrap common-wrap">
    <nav-crumb :navpath="navpath"></nav-crumb>
    <div class="right-content">
      <div class="right-box">
        <div class="common-wrap__opt">
          <el-input
            class="w-220"
            placeholder="请输入关键字搜索人群"
            prefix-icon="el-icon-search"
            v-model="tagSearch"
            clearable
            @clear="clearSearch"
            @keyup.enter.native="searchEnterFun">
          </el-input>
          <el-button type="primary" @click="toAddGroup" class="fr">新增分组</el-button>
        </div>
        <div class="common-wrap__table m-t-20">
          <el-table
            ref="multipleTable"
            :data="groupTableData"
            tooltip-effect="dark"
            style="width: 100%">
            <el-table-column
              label="分组名称"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                <el-popover
                  placement="top-start"
                  width="200"
                  trigger="hover">
                  <span>{{ scope.row.describle }}</span>
                  <a slot="reference" class="pointer" :href="'/memberGroupDetail?memberTagGroupId='+scope.row.memberTagGroupId">{{ scope.row.groupName }}</a>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
              label="是否实时"
              width="100px"
              >
              <template slot-scope="scope">
                {{scope.row.isRealTime == 1? '实时':'非实时'}}
              </template>
            </el-table-column>
            <el-table-column
              label="最近更新时间"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.latestUpdateTime | formatTimeYMD }}
                <p>{{ scope.row.latestUpdateTime | formatTimeHMS  }}</p>
              </template>
            </el-table-column>
            <el-table-column
              label="更新频率"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.updateRate }}
              </template>
            </el-table-column>
            <el-table-column
              prop="state"
              label="状态"
              width="50px"
              >
              <template slot-scope="scope">
                {{scope.row.effectiveStatus == 1? '有效':'失效'}}
              </template>
            </el-table-column>
            <el-table-column
              prop="peopleCover"
              label="覆盖人数"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                {{scope.row.memberCount | formatNum }} 人
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.createTime | formatTimeYMD }}
                <p>{{ scope.row.createTime | formatTimeHMS }}</p>
              </template>
            </el-table-column>
            <el-table-column
              prop="recentUpdateDate"
              label="到期时间"
              width="150px"
              show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.effectiveDate | formatTimeYMD }}
                <p>{{ scope.row.effectiveDate | formatTimeHMS }}</p>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
              <template slot-scope="scope">
                <el-button type="text" size="small" class="p-r-20" v-if="scope.row.effectiveStatus == 1" @click="toInvalid(scope.row)">失效</el-button>
                <router-link :to="{path:'/memberGroupDetail',query:{memberTagGroupId:scope.row.memberTagGroupId}}" class="edit-btn el-button--text p-r-20">详情</router-link>

                <router-link :to="{path:'/memberGroupEdit',query:{memberTagGroupId:scope.row.memberTagGroupId}}" class="edit-btn el-button--text p-r-20">编辑</router-link>
                <!-- <el-popover
                  placement="top"
                  width="160"
                  v-model="scope.row.popVisible">
                  <p style="line-height: 1.5; padding: 10px 10px 20px;">确认删除吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="cancelDelTag(scope.$index,scope.row)">取消</el-button>
                    <el-button type="primary" size="mini" @click="toDelTag(scope.$index,scope.row)">确定</el-button>
                  </div>
                  <el-button slot="reference" type="text">
                    删除
                  </el-button>
                </el-popover> -->
                <el-button type="text" size="small" @click="toDelTag(scope.row, scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block common-wrap__page text-right" v-if="groupTableData.length!=0">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
    <vue-gic-footer></vue-gic-footer>
  </div>
</template>
<script>
  import navCrumb from '@/components/nav/nav.vue';
  import strLength from '@/common/js/strlen';
  import showMsg from '@/common/js/showmsg';
  import errMsg  from '@/common/js/error';
  import timeFormat  from '@/common/js/timeFormat';
  import { _debounce } from "@/common/js/public";
  import { getRequest, postRequest, postJson, postForm } from '@/api/api';
  export default {
      name: 'memberGroupList',
      data() {
        return {
          // 面包屑参数
          navpath: [
            {
              name: '首页',
              path: ''
            },
            {
              name: '会员标签',
              path: ''
            }
          ],

          tagSearch: '', // 搜索值绑定的参数

          // 会员分组列表数据
          groupTableData:[
            {
              memberTagGroupId: 1,
              describle: '标签列表数据',
              groupName: '123',
              isRealTime: 1,
              latestUpdateTime: '2018-09-30 14:30:28',
              updateRate: '-',
              effectiveStatus: 1,
              memberCount: '456566',
              createTime: '2018-09-30 14:30:28',
              expiredTime: '2018-09-30 14:30:28',
              popVisible: false,
            }
          ],

          // 分页参数
          currentPage: 1,
          pageSize: 20,
          total: 0,
        }
      },
      filters: {
        formatTimeYMD(data) {
          return data && data != '- -' ? data.split(' ')[0] : '--';
        },
        formatTimeHMS(data) {
          return data && data != '- -' ? data.split(' ')[1] : '--';
        },
        formatNum(data){
          const reg=/\d{1,3}(?=(\d{3})+$)/g;
          return (data + '').replace(reg, '$&,');
        }
      },
      methods: {
        /**
         * 路由跳转
         */
        changeRoute(route) {
          this.$router.push(route);
        },

        /**
         * 新增分组
         */
        toAddGroup() {
          const that = this
          that.changeRoute('/memberGroupEdit')
        },

        /**
         * 搜索分组清空
         */
        clearSearch() {
          const that = this
          that.currentPage = 1;
          that.getGroupList()
        },

        /**
         * 搜索分组
         */
        searchEnterFun(e) {
          const that = this
          console.log(e)
          that.currentPage = 1;
          that.getGroupList()
        },

        /**
         * 分页---页码变化
         */
        handleSizeChange(val) {
          const that = this
          console.log(`每页 ${val} 条`);
          that.getGroupList()
        },
        /**
         * 分页---当前页变化
         */
        handleCurrentChange(val) {
          const that = this
          console.log(`当前页: ${val}`);
          that.getGroupList()
        },

        /**
         * 点击失效
         */
        toInvalid(info) {
          console.log(info, "thisGroup");
          const that = this
          that.$confirm('该分组失效后，不再更新数据，确认要失效吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let pramas = {
              effectiveStatus: '0',
              memberTagGroupId: info.memberTagGroupId
            }
            that.updateGroup(pramas, info);
          }).catch(() => {

          });
        },
        /**
         * 简单更新当前分组 -- 失效/更改人数
         */
        updateGroup(pramas, info) {
          const that = this;
          getRequest('/member-tag-group/simpleUpdate.json', pramas)
          .then(res => {
            console.log(res, 'updateResult');
            info.effectiveStatus = pramas.effectiveStatus || info.effectiveStatus;
            info.memberCount = pramas.memberCount || info.memberCount;
          })
          .catch(e => {
            console.log(e, 'error');
          });
        },
        /**
         * 删除当前分组 -- 取消
         */
        cancelDelTag(index,row) {
          const that = this
          row.popVisible = false
        },

        /**
         * 删除当前分组 -- 确定
         */
        toDelTag(obj, index) {
          const that = this;
          that.$confirm(`该分组覆盖${that.$options.filters['formatNum'](obj.memberCount)}人，一旦删除无法恢复，确认要删除吗？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // that.groupTableData.splice(index,1)
            that.delGroup(obj.memberTagGroupId, index);
          }).catch(() => {});
        },

        /**
         * 删除当前分组 -- API
         */
        delGroup(id, index) {
          const that = this;
          const para = {
            memberTagGroupId: id, // 搜索字段
          };
          getRequest('/member-tag-group/delete.json',para)
          .then((res) => {
            console.log(res, "delResult");
            var resData = res.data
            if (resData.errorCode == 1) {
              that.groupTableData.splice(index,1);
              return;
            }
            errMsg.errorMsg(resData);
          })
          .catch(function (error) {
            console.log(error);
            that.$message.error({
              duration: 1000,
              message: error.message
            })
          });
        },

        /**
         * 获取列表数据
         */
        getGroupList() {
          const that = this;
          const para = {
            groupName: that.tagSearch, // 搜索字段
            pageSize: that.pageSize,
            pageNum: that.currentPage
          };
          getRequest('/member-tag-group/findList.json',para)
          .then((res) => {
            console.log(res, "groupList");
            var resData = res.data
            if (resData.errorCode == 1) {
              that.total = resData.result.totalCount;
              resData.result.result.forEach(function(ele,index){
                if (!ele.latestUpdateTime) {
                  ele.latestUpdateTime = '- -'
                }

                if (ele.createTime) {
                  ele.createTime = timeFormat.timeToDateTime(ele.createTime)
                }
              })

              that.groupTableData = resData.result.result
              // showMsg.showmsg('保存成功','success')
              return;
            }

            errMsg.errorMsg(resData)

          })
          .catch(function (error) {
            console.log(error);
            that.$message.error({
              duration: 1000,
              message: error.message
            })
          });
        }
      },
      mounted() {
        const that = this
        that.getGroupList()
      },
      components: {
        navCrumb
      }
    }
</script>
<style lang="scss" scoped>


</style>
