<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>手机号：</label>
                            <input type="number" name="phone"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
<!--                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i-->
<!--                                    class="fa fa-refresh"></i>&nbsp;重置</a>-->
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="create()"><i
                                    class="fa"></i>创建特邀代理</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-bordered">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    let uid = null
    let perId = null
    let hostname = null
    var title = null
    var prefix2 = ctx + "local";
    let dialog = `<div class="dialog" style="height:200px;width:500px;display: flex;flex-direction: column;justify-content: space-around;align-items: center;">
        <div class="title">请输入拉黑原因</div>
        <textarea name="" id="" cols="50" rows="10"></textarea>
    </div>`

    let add = `<div style="height:300px;display: flex;flex-direction: column;justify-content: space-around;align-items: center;">
        <p style="border-bottom: 1px solid #000;width: 100%;text-align: center;">创建特邀代理</p>
        <input id="name" type="text" placeholder="特邀代理姓名" style="width:200px;height: 30px;">
        <input id="phone" maxlength="11" type="text" placeholder="特邀代理手机号" style="width:200px;height: 30px;">
        <input id="text" type="text" placeholder="代理描述" style="width:200px;height: 30px;">
        <select name="" id="performance" style="width:200px;height: 30px;">
            <option value="" disabled selected>选择演出</option>
        </select>
        <input id="ticket" type="text" placeholder="票提设置%" style="width:200px;height: 30px;">
    </div>`

    var detailFlag = [[${@permission.hasPermi('smile:performances:detail')}]];
    var smilePrefix = ctx + "smile/user";
    console.log(smilePrefix);
    var performancesId = null
    //     // createUrl: prefix + "/add",
    //     // updateUrl: prefix + "/edit/{id}",
    //     // removeUrl: prefix + "/remove",
    //     // exportUrl: prefix + "/export",
    //     // modalName: "订单",
    $(function() {
        var options = {
            updateUrl: smilePrefix + "/smileDetail",
            url: smilePrefix + "/list",
            columns: [{
                checkbox: true
            },
                {
                    field: 'uid',
                    title: '用户id'
                },
                {
                    field: 'img',
                    title: '生活照',
                    formatter: function(value, row, index) {
                        return $.table.imageView(value,"300", "300");
                    }
                },
                {
                    field: 'name',
                    title: '姓名'
                },
                {
                    field: 'sex',
                    title: '性别',
                    formatter: function(value, row, index) {
                        if (value == 1) {
                            return '男';
                        } else if (value == 2){
                            return '女';
                        }else {
                            return '-';
                        }
                    }
                },
                {
                    field: 'birthday',
                    title: '生日'
                },
                {
                    field: 'phone',
                    title: '手机号'
                },
                {
                    field: 'identity',
                    title: '身份',
                    formatter: function(value, row, index) {
                        if (value == 1) {
                            return '在校学生';
                        } else {
                            return '已毕业';
                        }
                    }
                },
                {
                    field: 'schoolName',
                    title: '学校/公司名称'
                },
                {
                    field: 'province',
                    title: '地区'
                },
                {
                    field: 'type',
                    title: '类型',
                    formatter: function(value, row, index) {
                        if (value === 1) {
                            return '总代';
                        } else if(value === 2){
                            return '普代';
                        }else{
                            return '特邀代理'
                        }
                    }
                },
                {
                    field: 'state',
                    title: '状态',
                    formatter: function(value, row, index) {
                        if (value === 0) {
                            return '正常';
                        } else {
                            return '黑名单';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="goDetail(\''+row.type+'\',\''+row.uid+'\')"><i class="fa fa-edit"></i>查看</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="updateUserStateById(\'' + row.uid + '\',\'' + 1 + '\')"><i class="fa fa-edit"></i>加入黑名单</a> ');
                        if(row.type === 5){
                            actions.push('<a class="btn btn-success btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="showQRcode(\'' + row.uid + '\')"><i class="fa fa-edit"></i>查看二维码</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    function goDetail (type,id) {
        if(type === '5'){
            let url = ctx+'local/specialDetail?id=' + id;
            $.modal.openTab("特邀用户详情", url);
        }else{
            let url = ctx+'local/smileDetail?id=' + id;
            $.modal.openTab("用户详情", url);
        }
    }

    /*加入黑名单*/
    function updateUserStateById(uid,state) {
        $.modal.confirm(dialog,() =>{
            let str = $('textarea').val()
            let postData = {
                uid: uid,
                state: state,
                reason:str
            }
            $.ajax({
                type: 'post',
                url: ctx + 'smile/user/updateUserStateByUid',
                headers: {"Content-Type": "application/json;charset=UTF-8"},
                data: JSON.stringify(postData),
                success: function (e) {
                    // alert(e.msg)
                    location.reload();
                }
            });
        });
    }

    function create(){
        $.modal.open('创建特邀代理',prefix2+'/smileUserListAdd', 450, 400, (index, layero) => {
            let body = layer.getChildFrame('body', index);
            let data = {
                describe: body.find('#text').val(),
                name: body.find('#name').val(),
                performancesId: body.find('#performance').val(),
                phone: body.find('#phone').val(),
                ticket: Number(body.find('#ticket').val())
            }
            for(let i in data) {
                if (!data[i]) return layer.msg('请将必填项填写完整！')
            }
            $.ajax({
                url:'/smile/user/saveInvitedAgent',
                type:'post',
                data:JSON.stringify(data),
                headers: {"Content-Type": "application/json;charset=UTF-8"},
                success:function(res){
                    location.reload();
                }
            })
        })
    }

    function showQRcode(id){
        if(window.location.hostname.indexOf('dev') !== -1){
            hostname = 'https://devm.zhengzai.tv'
        }else if(window.location.hostname.indexOf('test') !== -1){
            hostname = 'https://testm.zhengzai.tv'
        }else{
            hostname = 'https://m.zhengzai.tv'
        }
        uid = id
        $.ajax({
            type: 'post',
            url: smilePrefix + "/getUserById",
            data: {
                uid: id
            },
            success:function(res){
                perId = res.data.perId
                title = res.data.title
            }
        })
        setTimeout(() =>{
            let qrCode = `<div style="height:300px;display: flex;flex-direction: column;justify-content: space-around;align-items: center;">
            <p style="width: 100%;text-align: center;">查看二维码</p>
            <img src="https://api.qrserver.com/v1/create-qr-code?data=${hostname}/%23/ticket/detail?id=${perId}%26agentId=${uid}" style="width: 200px;height: 200px;">
            <p>地址链接：${hostname}/#/ticket/detail?id=${perId}&agentId=${uid}</p>
            <p>演出名称：${title}</p>
        </div>`
            $.modal.confirm(qrCode,() =>{

            })
        },100)
    }
    // var updateFlag = [[${@permission.hasPermi('sweet:integralActivity:create')}]];
    // var isOnlineFlag = [[${@permission.hasPermi('sweet:integralActivity:isOnline')}]];
    // var drawListFlag = [[${@permission.hasPermi('sweet:integralActivityDraw:list')}]];
    // var exportExcelFlag = [[${@permission.hasPermi('sweet:integralActivityDraw:exportExcel')}]];

    // var prefix = ctx + "sweet/integralActivity";

</script>
</body>
</html>