appendFormHtml = ()=>{
    let html = `
<div class="modal fade" id="operateDialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="opacity: 1" >
    <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" onclick="$('#operateDialog').hide()"><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 " role="form" id="downloadRewardsForm"  method="post"   >
                <div class="modal-body">

                <input type="hidden" id="id" class="form-control col-sm-4"  name="id">
<!--备注-->
                    <div class="form-group">
                        <label class="col-sm-3 control-label">备注:</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control col-sm-4" placeholder="备注" name="remark">
                        </div>
                    </div>

<!--跟踪链-->
                    <div class="form-group">
                        <label class="col-sm-3 control-label">广告跟踪链:</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control col-sm-4" placeholder="广告跟踪链" name="track">
                        </div>
                    </div>

<!--邮件模板id-->
                    <div class="form-group">
                        <label class="col-sm-3 control-label">邮件模板id:</label>
                        <div class="col-sm-4">
                            <select  class="form-control col-sm-4 select2" name="mail_id">
                            </select>
                        </div>
                    </div>

<!--奖励-->
                    <div class="form-group addOrDelControl" id="vipDiv">
                        <label class="col-sm-3 control-label">奖励道具:</label>
                        <div class="col-sm-9">
                            <label class="select-inline">
                                <table class="col-sm-12">
                                    <tr>
                                        <td class="formPropsRewardsTag" data-value='[]' data-name="rewards"></td>
                                        <td class="text-right">
                                            <button type="button"
                                                    class="btn btn-outline btn-success"
                                                    onclick="propsJs.initEdit(this, 'formPropsRewardsTag')">
                                                奖励配置
                                            </button>
                                        </td>
                                    </tr>
                                </table>
                            </label>
                        </div>
                    </div>
                    
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" onclick="$('#operateDialog').hide()">关闭</button>
                    <button type="button" class="btn btn-primary " onclick="submitForm()">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
    `
    console.log($('#operateDialog'))
    if ($('#operateDialog').length < 1){
        $('body').append(html)
    }
}
getMsgTemplates = ()=>{
    $.get('/systemMail/getSysMailList', (response)=>{
        response = jsonParse(response)
        msgList = response.data.list ??[]
        console.log(msgList);
        $('.mail_idTd').each(function(i, o){
            let mid = $(o).data('value')
            console.log(msgList[mid]);
            $(o).text(msgList[mid] ?? '-')
        })

        let optionsHtml = ''
        $.each(msgList, (i, o)=>{
            optionsHtml += `<option value="${i}">${o}</option>`
        })
        $('select[name="mail_id"]').append(optionsHtml)
        $('.select2').select2({})
    })
}
appendFormHtml()
getMsgTemplates()
propsJs.initTag('rewardsTd')


// 新增操作
newRewards = ()=>{
    let data = {
        id : 0,
        remark : '',
        track : '',
        mail_id : 0,
        rewards : [],
    }
    renderOperateDialog(data)
}
// 新增操作
submitForm  = ()=>{
    const data = $('#downloadRewardsForm').toObject()
    $.post('/config/saveDownloadReward', data, (response)=>{
        response = jsonParse(response)
        alert(response.msg)
        if (response.ret == 0){
            window.location.reload()
        }
    })
}

$('.editRewards').click(function(){
    let data = $(this).parent('td').data()
    renderOperateDialog(data)
})

renderOperateDialog = (data) => {
    $('input[name=track]').val(data.track ?? '')
    $('input[name=remark]').val(data.remark ?? '')
    $('input[name=id]').val(data.id ?? 0)
    $('select[name="mail_id"]').val(data.mail_id ?? 0)
    $('.formPropsRewardsTag').data('value', data.rewards)
    propsJs.initTag('formPropsRewardsTag')
    $('.select2').select2({})
    $('#operateDialog').show()
}