getShushuGroups().then()
function editGroupDialogShow(){
    if($('#editGroupDialog').length < 1){
        generateDialog()
    }
    resetForm()
    $(`#editGroupDialog`).fadeIn()
}
function resetForm(){
    $(`input[name=id]`).val('')
    $(`input[name=name]`).val('')
    $(`select[name=gids]`).val([])
    $('.select2').select2({width: '200'})
}
function generateDialog(){
    let optionsHtml = getShushuSelectorOptions()
    let editGroupDialogHtml = `
        <div class="modal" id="editGroupDialog" style="display: none">
            <div class="modal-dialog" style="top: 10%;width: 50%;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">添加祝福语模板</h4>
                    </div>
                    <form class="form-horizontal" id="editForm" role="form" method="post">
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">分组名:</label>
                                <div class="col-sm-4">
                                    <input type="hidden" class="form-control col-sm-4" placeholder="" name="id">
                                    <input type="text" class="form-control col-sm-4" placeholder="分组名" name="name">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">数数分组:</label>
                                <div class="col-sm-4">
                                    <select class="form-control select2" name="gids" multiple>
                                        ${optionsHtml}
                                    </select>
                                </div>
                            </div>
        
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" onclick="$('#editGroupDialog').fadeOut()">关闭</button>
                            <button type="button" class="btn btn-primary" onclick="submitForm()">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    `
    $('body').append(editGroupDialogHtml)
    $('.select2').select2({width: '200'})
}

async function getShushuGroups() {
    showLoading()
    await $.get('/config/getShushuList',function(response){
        hideLoading()
        if(typeof response == 'string')response=JSON.parse(response)
        shushuGroups = response.data
    })
}

function getShushuSelectorOptions() {
    let html = ''
    $.each(shushuGroups, function (i, o) {
        html += `<option value="${o.id}">${o.name}</option>`
    });
    return html
}
function submitForm() {
    const data = $('#editForm').toObject()
    data.gids = $('select[name=gids]').val()
    console.log(data)
    showLoading()
    $.post( rootPath + 'jackpot/saveGroup', data, response=>{
        hideLoading()
        if(typeof response == 'string')response = JSON.parse(response)
        alert(response.msg)
        if(response.ret == 0){
            window.location.reload()
        }
    });
}

function addGroup(){
    editGroupDialogShow()
}
function editGroup(obj){
    let data = $(obj).parent('td').data('data')
    data = JSON.parse(unescape(data))
    let gids = data.gids.split(',')
    console.log(data);
    console.log(gids);
    editGroupDialogShow()
    $(`input[name=id]`).val(data.id)
    $(`input[name=name]`).val(data.name)
    $(`select[name=gids]`).val(gids)
    $('.select2').select2({width: '200'})
}

function groupWeightsDetail(obj){
    let url = '#jackpot/groupWeights/'
    let data = $(obj).parent('td').data('data')
    data = JSON.parse(unescape(data))
    url += data.id
    window.location.href = url
}