<template>
    <div class="template_content">
        <!-- 搜索 -->
        <div class="search_box">
            <a-form layout="inline">
              <a-form-item label="用户手机号">
                  <a-input  v-model="searchPage.username" placeholder="请输入用户手机号" allowClear></a-input>
              </a-form-item>
              <a-form-item label="用户标签" >
                  <a-select mode="default" :value="selectedQueryTag" style="width: 150px" placeholder="请选择" @change="queryUserTagChange" allowClear>
                    <a-select-option v-for="item in userTagList" :key="item.id" :value="item.id">
                    {{ item.name}}
                    </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item>
                  <a-button type="primary" icon="search" @click="handlSearch">查询</a-button>
              </a-form-item>
            </a-form>
        </div>
        <!-- 表格内容 -->
        <div class="content_box">
            <a-table
                :columns="columns"
                :dataSource="data"
                :loading="loading"
                :pagination="pagination"
                bordered
                size="small"
                @change="handleTableChange"
                rowKey="id"
            >
                <!-- 用户 -->
                <div slot="nickName" slot-scope="text,record">
                    <a-row>
                        <!-- 头像 -->
                    <a-col :span="4">
                        <a-avatar v-if="!record.avatarUrl || record.avatarUrl  == ''" shape="square" :size="64" icon="user" />
                        <a-avatar v-if="record.avatarUrl && record.avatarUrl != ''" shape="square" :size="64" :src="record.avatarUrl" />
                    </a-col>
                    <!-- 用户姓名、昵称，注册时间 -->
                    <a-col :span="19" offset="1">
                        <a-row>
                          <a-col :span="24"><span>昵称：{{record.nickName}}</span></a-col>
                          <a-col :span="24"><span>注册时间：{{ timestampToTime(record.createTime) }}</span></a-col>
                        </a-row>
                    </a-col>
                    </a-row>
                </div>
                <!-- 用户状态 -->
                <div slot="status" slot-scope="text,record">
                    <a-tag v-if="record.status == 'ENABLE'" color="green">启用</a-tag>
                    <a-tag v-if="record.status == 'DISABLE'" color="red">禁用</a-tag>
                    <a-tag v-if="record.status == 'UNACTIVATE'" color="orange">未激活</a-tag>
                </div>
                <!-- 用户标签 -->
                <div slot="userTags" slot-scope="text, record">
                    <span v-if="record.userTags.length == 0">-</span>
                    <a-tag v-for="(item,index) in record.userTags" :key="index" color="blue">{{item.name}}</a-tag>
                </div>
                <!-- 操作 -->
                <template slot="action" slot-scope="text, record">
                    <a-button
                        icon="edit"
                        type="link"
                        @click="editUserTag({record})"
                    >编辑标签</a-button>
                </template>
            </a-table>
        </div>
        <!-- 弹窗 -->
        <a-modal title="编辑标签" v-model="modalVisible" :footer="null">
        <a-form @submit="modalHandleSubmit">
            <a-form-item label="请选择标签：" :label-col="{ span: 5 }" :wrapper-col="{ span: 19 }">
            <a-select mode="multiple" v-model="selectedTags" style="width: 100%" placeholder="请选择" @change="userTagHandleChange">
                    <a-select-option v-for="item in userTagList" :key="item.id" :value="item.id">
                    {{ item.name}}
                    </a-select-option>
                </a-select>
            </a-form-item>
            
            <a-form-item :wrapper-col="{ span: 19, offset: 5 }">
            <a-button type="primary" html-type="submit">保存</a-button>
            </a-form-item>
        </a-form>
        </a-modal>
    </div>
    
</template>
    
<script>

import {fetchList,addUserTags} from '@/api/biz/member'
import {fetchList as fetchUserTagList} from "@/api/admin/user_tag";
import { disposereq,timestampToTime } from '@/utils/util'

let columns = [
    {
        title: "用户",
        dataIndex: "nickName",
        align: "left",
        width: '30%',
        scopedSlots: {customRender: 'nickName'}
    },
    {
        title: "注册手机号",
        dataIndex: "username",
        align: "center",
    },
    {
        title: "用户身份",
        dataIndex: "type",
        align: "center",
        customRender: (text,record) => {
            let str = ''
            // console.log(text)
            // console.log(record)
            if(record.type == 'USER'){
                str = '普通用户'
            }else if(record.type == 'VIP'){
                str = '会员'
            }else if(record.type == 'ENTERPRISE_VIP'){
                str = '企业会员'
            }
            if(record.enterpriseName){
                str = str + '(' + record.enterpriseName + ')'
            }
            return str;
        }
    },
    {
        title: '最后登陆时间',
        dataIndex: 'lastLoginTime',
        align: 'center',
        customRender: (text) => {
            return text ? timestampToTime(text) : '-'
        }
    },
    {
        title: '用户状态',
        dataIndex: 'status',
        align: 'center',
        scopedSlots: {customRender: 'status'}
    },
    {
        title: '会员生效时间',
        dataIndex: 'vipStartTime',
        align: 'center',
        customRender: (text) => {
            return text ? timestampToTime(text) : '-'
        }
    },
    {
        title: '会员失效时间',
        dataIndex: 'vipEndTime',
        align: 'center',
        customRender: (text) => {
            return text ? timestampToTime(text) : '-'
        }
    },
    {
        title: '用户标签',
        dataIndex: 'userTags',
        align: 'center',
        scopedSlots: {customRender: 'userTags'}
    },
    {
    title: "操作",
    key: "action",
    scopedSlots: { customRender: "action" },
    align: "center",
    width: 180,
  }
]

export default {
    data(){
        return{
            columns: columns,
            data: [],
            searchPage: {
                pageIndex: 1,
                pageSize: 10,
                username: '',
                userTagId: null
            },
            pagination: {
                showQuickJumper: true,
                showSizeChanger: true
            },
            loading: false,
            modalVisible: false,
            userTagList: [],
            currentUserId: 0,
            selectedTags: [],
            selectedQueryTag: null,

        }
    },
    // 创建完毕
    created() {
        this.getList(this.searchPage);
        this.getUserTagList();
    },
    // 即将更新渲染
    beforeUpdate() {
        
    },
    methods: {
        getList(query){
            this.loading = true;
            fetchList(query).then(res => {
                if(res.code == 200){
                    this.data = res.data
                    let paper = {...this.pagination}
                    paper.current = query.pageIndex
                    paper.total = res.count
                    this.pagination = paper
                }else{
                    this.$message.info(res.resp_msg)
                }
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                disposereq(this,err)
            })

        },
        getUserTagList(){
            let query = {};
            query['pageIndex'] = 1;
            query['pageSize'] = 10000;
            fetchUserTagList(query).then(res => {
                if(res.code == 200){
                    this.userTagList = res.data;
                }else{
                    this.$message.info(res.resp_msg)
                }
            }).catch(err => {
                disposereq(this,err)
            })
        },
        handleTableChange(pagination,filters,sorter){
            let queryParam = {...this.searchPage}
            queryParam.pageIndex = pagination.current
            queryParam.pageSize = pagination.pageSize
            this.getList(queryParam)
        },
        handlSearch(){
            let queryParam = {...this.searchPage}
            this.getList(queryParam)
        },
        timestampToTime,
        editUserTag(row){
            this.modalVisible = true;
            this.selectedTags = row.record.userTags.map(item => item.id);
            // console.log(this.formModel.userTagIds)
            this.currentUserId = row.record.id;
        },
        userTagHandleChange(value){
            this.selectedTags = value
        },
        modalHandleSubmit(e){
            this.loading = true;
            addUserTags(this.currentUserId,this.selectedTags).then(res => {
                this.loading = false;
                if(res.resp_code == 200){
                    this.modalVisible = false;
                    this.getList(this.searchPage);
                }else {
                    this.$message.info(res.resp_msg)
                }
            }).catch(err => {
                disposereq(this,err)
            });

        },
        queryUserTagChange(value){
            this.selectedQueryTag = value;
            if(value == 0){
                this.searchPage.userTagId = null;
            }else{
                this.searchPage.userTagId = value;
            }
            
        },
    }
}
</script>
<style>
.template_content {
  background-color: #f0f2f5;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
.search_box {
  width: 100%;
  box-sizing: border-box;
  padding: 14px;
  background-color: #fff;
}
.content_box {
  width: 100%;
  background-color: #fff;
  margin-top: 10px;
  padding: 14px;
}
.table_top {
  padding-bottom: 14px;
}
.btn_margin {
  margin: 0px 5px;
}
</style>