$('.blessingWeightDetail').click(function(){
    let data = $(this).parent('td').data('data')
    data = JSON.parse(unescape(data))
    window.location.href = '/#exchange/blessingWeightDetail/'+data.iid
})
$('.edit').click(function(){
    let data = $(this).parent('td').data('data')
    data = JSON.parse(unescape(data))
    addBlessingTemplateDialog()
    $('input[name=iid]').val(data.iid)
    $('input[name=title]').val(data.title)
    $('#addBlessingTemplateDialog').fadeIn()
})

$('#addBlessingTemplate').click(function(){
    console.log(1)
    addBlessingTemplateDialog()
    $('#addBlessingTemplateDialog').fadeIn()
})

function addBlessingTemplateDialog(){
    if($('#addBlessingTemplateDialog').length > 0)return;
    let addBlessingTemplateDialogHtml = `
   
        <div class="modal" id="addBlessingTemplateDialog" 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="blessingTemplateForm" 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="iid">
                                    <input type="text" class="form-control col-sm-4" placeholder="祝福语模板标题" name="title">
                                </div>
                            </div>
        
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" onclick="$('#addBlessingTemplateDialog').fadeOut()">关闭</button>
                            <button type="button" class="btn btn-primary" onclick="submitForm()">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    `
    $('body').append(addBlessingTemplateDialogHtml)
}


function submitForm() {
    const data = $('#blessingTemplateForm').toObject()
    console.log(data)
    showLoading()
    $.post( rootPath + 'exchange/saveBlessingTemplateDialog', data, response=>{
        hideLoading()
        if(typeof response == 'string')response = JSON.parse(response)
        alert(response.msg)
        if(response.ret == 0){
            window.location.reload()
        }
    });
}