
<section class="admin-main">
    <div class="container-fluid">
        <div class="page-container">
            <div class="card">
                <div class="card-body">
                    <!-- class="col-lg-1 col-md-12 col-sm-12" -->
                    <div class="card-title row">
                        <div class="pl-4 pr-4">{$Title}</div>
                        <div class="col-lg-8 col-md-12 col-sm-12">
                            {foreach $PluginsAdminMenu as $v}
                                {if $v['custom']}
                                    <span  class="ml-2"><a  class="h5" href="{$v.url}" target="_blank">{$v.name}</a></span>
                                {else/}
                                    <span  class="ml-2"> <a  class="h5" href="{$v.url}">{$v.name}</a></span>
                                {/if}
                            {/foreach}
                        </div>
                    </div>

                    <div class="tab-content mt-4">

                        <div class="table-tools">
                            <a href="javascript:;" class="btn btn-success w-sm nohide add-activity">
                                <i class="fas fa-plus-circle"></i> 添加奖品
                            </a>
                           </div>
                        <div class="table-body auto-login-content" style="margin-top: 10px;">
                            <table class="table table-bordered table-hover activity-table">
                                <thead class="thead-light">
                                <tr>

                                    <th class="center t1" data-name="id" data-type="text" data-readonly="readonly">ID </th>

                                    <th data-type="text" data-name="winning">充值金额</th>
                                    <th>说明<span style="display: none;"><a href="addons?_plugin={$GzhxPluginPath}&_controller=admin_index&_action=set&id=@{id}">@{type}设置</a></span></th>
                                    <th data-type="set" class="center t5">操作<span style="display: none;"><a href="javascript:;" class="btn btn-link get-md5 edit"><i class="fas fa-edit"></i> 编辑</a></span></th>
                                </tr>
                                </thead>
                                <tbody>
                                {foreach $List as $key=>$item}
                                    <tr data-data='{$item|json_encode}'>
                                        <td class="center">{$item.id}</td>



                                        <td>{$item.winning}</td>
                                        <td><a href="addons?_plugin={$GzhxPluginPath}&_controller=admin_index&_action=set&id={$item.id}">{$item.type}设置{empty name="item.setting"}，未设置{/empty}</a></td>

                                        <td>
                                            <a href="javascript:;" class="btn btn-link get-md5 edit"><i class="fas fa-edit"></i> 编辑</a>
                                        </td>
                                    </tr>
                                {/foreach}
                                </tbody>
                            </table>
                        </div>

                        <div class="table-body">文档说明：<a href="https://html5code.org/type/zjmf.html" target="_blank">https://html5code.org/type/zjmf.html</a> </div>
                        <div class="table-body" id="renewMsg" style="color: #ff0000;"></div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    let ProductGroups={$Groups|json_encode};
    $(function () {
        let queryToJson=function (hash){
            let str=hash?window.location.hash:window.location.search
            if( !str ) return { };
            if(str) str=str.substr(1);
            if( !str ) return { };
            let arr = str.split('&');
            let data={ };
            $.each( arr, function (k,v) {

                if(v.indexOf("=")>-1){
                    let d=v.indexOf("=");
                    data[ decodeURIComponent(v.substr(0,d)) ]=decodeURIComponent(v.substr(d+1));

                }

            } );
            return data;
        }
        let jsonToQuery=function (json){
            return  Object.keys(json).map(function (key) {
                return json[key]? (key + "=" + encodeURIComponent(json[key])):"";
            }).join("&");
        }
        let ajax=function (option){
            $.ajax({
                    dataType: "json",
                    type: option.type||"post",

                    headers: {
                        "X-Requested-With": "XMLHttpRequest",

                    },
                    url: option.url||"",
                    data:option.data,
                    async:true,
                    success: function (t) {



                        if( t.status==1 ){
                            if(option.success)  option.success(t.info);
                        }else{
                            if(option.error){
                                option.error(t.info)
                            }else{
                                $('#renewMsg').html(t.info);
                            }
                        }
                    },

                    error: function (request, status, errorThrown) {
                        if(option.error){ option.error("网络错误，请重试") }else{
                            $('#renewMsg').html("网络错误，请重试");
                        }
                    }
                }
            );
        }



        let _setTable=function (table,val){

            let setVal=function (dom,item){
                let type=dom.attr('data-type'),name=dom.attr('data-name');
                if(!type){
                    return '<td>-</td>'
                }else if(type=='text'){

                    return '<td data-item="'+item+'"><input type="text"  class="form-control" name="'+name+'" value="'+(val&&val[name]?val[name]:'')+'" '+(dom.attr('data-readonly')?dom.attr('data-readonly'):'')+'></td>'
                }else if(type=='select'){
                    let s='<select class="form-control" name="'+name+'">';
                    $.each( dom.attr('data-list').split(','),function (k,v){
                        s +='<option value="'+v+'" '+(val&&val[name]&&val[name]==v?'selected':'')+'>'+v+'</option>';
                    } )
                    s +='</select>';
                    return '<td data-item="'+item+'">'+s+'</td>'
                }else if(type=='set'){
                    return '<td><div style="display: flex;align-items: center;">' +
                        (val?'<a href="javascript:;" class="delete" style="margin-right: 30px;color: #8a8a8a!important;display: flex;align-items: center;"><svg t="1624074109512" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8820" width="16" height="16"><path d="M378.727 760.733V430.101c0-15.627-12.736-28.318-28.363-28.318-15.656 0-28.364 12.69-28.364 28.318v330.632c0 15.628 12.708 28.333 28.364 28.333 15.627 0.001 28.363-12.704 28.363-28.333M534.346 809.254V337.851c0-15.626-12.707-28.363-28.363-28.363-15.626 0-28.333 12.738-28.333 28.363v471.403c0 15.626 12.707 28.333 28.333 28.333 15.656 0 28.363-12.708 28.363-28.333" fill="#8a8a8a" p-id="8821"></path><path d="M869.997 348.295v-2.219c0.061-8.345-2.797-15.87-8.026-21.13-4.743-4.803-10.942-7.356-18.088-7.385-15.93 0-29.488 12.919-29.61 28.21l0.029 2.402c0 2.082 0.031 5-0.03 15.808-0.029 8.968 0 13.94 0.03 19.212 0.031 6.567 0.062 13.605-0.03 29.413-0.12 25.87-0.06 52.046 0 78.068 0.062 24.943 0.122 49.75 0 74.1-0.029 7.48-0.029 11.22 0 14.988 0.03 3.77 0.062 7.51 0 15.047-0.639 126.376-1.003 212.468-1.003 239.918 0 20.036-4.892 33.684-13.376 37.425-4.59 1.946-13.162 2.19-20.761 1.792l-3.467 0.03-8.27-0.09-10.274 0.06c-3.467 0.062-7.023 0.095-10.64 0.095l-30.278-0.154-11.37 0.06c-27.543 0.367-55.543 0.215-83.45 0.03-27.055-0.151-54.02-0.303-80.378-0.03l-8.936 0.062-16.721-0.12-9.06 0.06c-103.36 1.215-181.886 0.577-226.969 0.214-13.863-0.12-24.563-0.215-31.89-0.215-26.661 0-34.2-9.363-37.85-17.51-2.552-5.776-3.768-12.89-3.312-19.67 0.03-0.607 0-3.13 0-3.16V347.383c0.06-16.432-10.853-28.38-26.114-28.485-16.174 0-30.462 13.208-30.462 28.272l0.061 482.788-0.122 2.431 0.06 0.76c-1.032 15.99 1.764 31.982 8.088 46.27 14.683 33.074 46.512 51.252 89.65 51.252 7.387 0 18.15 0.095 32.012 0.186 25.597 0.212 61.742 0.455 106.978 0.455 34.96 0 75.363-0.153 120.507-0.64l8.97-0.061 16.718 0.123 9.029-0.064c26.357-0.273 53.2-0.151 80.136 0 27.968 0.154 56.09 0.367 83.784 0l12.495-0.06 30.156 0.091 20.187-0.122 6.323 0.06v0.031c2.645 0.124 5.289 0.215 7.905 0.215 13.77 0 25.017-2.28 36.42-7.297 30.946-13.376 50.95-48.276 50.95-88.86l-0.091-28.15c-0.122-41.07-0.395-114.396 0.091-211.526 0.03-7.933 0.03-11.734 0-15.716v-14.318c0.12-24.382 0.061-49.218 0-74.146-0.091-26.038-0.151-52.122 0-77.993 0.061-15.398 0.03-22.527 0-28.91-0.03-5.487-0.061-10.427 0-19.684 0.06-10.945 0.03-13.908-0.001-15.99" fill="#8a8a8a" p-id="8822"></path><path d="M655.492 782.653c0 15.626 12.737 28.334 28.363 28.334 15.655 0 28.364-12.708 28.364-28.334V590.281c0-15.627-12.709-28.365-28.364-28.365-15.626 0-28.363 12.737-28.363 28.365v192.372zM683.855 505.873c15.626 0 28.333-12.737 28.333-28.364v-84.39c0-15.627-12.708-28.334-28.333-28.334-15.626 0-28.363 12.708-28.363 28.333v84.391c0 15.627 12.738 28.364 28.363 28.364M866.896 159.158l-243.75 0.122c-0.426-0.152-1.673-1.247-5.442-6.415l-1.552-2.113c-2.612-3.678-7.63-10.808-9.878-13.695-23.652-30.659-53.444-44.369-96.642-44.369-43.442 0.03-74.785 14.075-98.74 44.11-3.343 4.18-9.788 13.453-13.62 18.94-1.276 1.824-2.187 3.207-2.613 3.753l-243.507 0.381c-7.57 0-14.713 2.934-20.004 8.315-5.41 5.35-8.299 12.51-8.299 20.078 0 15.595 12.739 28.257 28.303 28.257h11.31l237.578-0.334c15.625 0 27.816-6.825 39.55-22.01 0.822-1.155 3.101-4.423 5.747-8.193 3.618-5.259 7.994-11.507 9.88-13.878 10.883-13.635 24.32-22.693 54.416-22.724 29.06 0 40.584 7.813 51.68 22.253 1.765 2.25 6.476 8.939 9.028 12.48l1.673 2.34c15.385 20.977 30.189 29.549 51.134 29.549l243.781-0.197c15.594 0 28.302-12.723 28.302-28.395-0.032-15.562-12.74-28.255-28.335-28.255" fill="#8a8a8a" p-id="8823"></path></svg> 删除</a>':'') +
                        '<a href="javascript:;" class="add"><i class="fas fa-check"></i> 保存</a> ' +
                        '<a href="javascript:;" class="exit" style="margin-left: 10px;color: #ff0000!important;"><i class="fas fa-times"></i> 取消</a>' +
                        '</div></td>'
                }
            }
            let html='';
            table.each(function (item){

                html +=setVal($(this),item);
            });
            return html;
        }
        let replaceHtml=function (tmp,val){
            let reg = /@\{(.+?)\}/g;
            let res = tmp.match(reg);
            if(res){
                $.each(res,function (k,v){
                    let id=v.replace(/\@{(.+?)\}/,"$1");
                    tmp=tmp.replace(v,val[ id ]);
                })
            }
            return tmp;


        }
        let _setHtml=function (table,data){
            let html='';
            table.each(function (item){
                let name=$(this).attr('data-name');
                if(name&&data[name]){
                    if($(this).find('span').length>0){
                        html +='<td data-name="'+name+'">'+replaceHtml($(this).find('span').html(),data)+'</td>';
                    }else{
                        html +='<td data-name="'+name+'">'+data[name]+'</td>';
                    }

                }else{
                    if($(this).find('span').length>0){
                        html +='<td>'+replaceHtml($(this).find('span').html(),data)+'</td>';
                    }else{
                        html +='<td></td>';
                    }

                }
            });
            return html;
        },_save=function (table,object,callback){
            ajax({
                data:{
                    action:table,
                    data:object
                },
                success:function (res){
                    callback(res);
                }
            })
        }
        let _product=function (){

            $('.add-product').off('click').on('click',function (){
                $('.activity-product-table>tbody').append('<tr data-add>'+(_setTable($('.activity-product-table>thead>tr>th')))+'</tr>');

                let g_html='';
                $.each(ProductGroups,function (k,v){
                    g_html +='<option value="'+v.id+'">'+v.name+'</option>'
                })
                $('.activity-product-table>tbody>tr[data-add]:last').find('select[name="gid"]').html(g_html)

                $('.activity-product-table>tbody>tr[data-add] .exit').off("click").on("click",function (){
                    $(this).closest('tr').remove();
                })
                $('.activity-product-table>tbody>tr[data-add] .add').off("click").on("click",function (){
                    let self=$(this),object={ };
                    $(this).closest('tr').children('td[data-item]').each(function (){
                        let s=$(this).children('[name]')
                        object[ s.attr('name')  ]=s.val();
                    })
                    _save('product',object,function (res){
                        self.closest('tr').remove();
                        $('.activity-product-table>tbody').append('<tr data-data=\''+JSON.stringify(res)+'\'>'+(_setHtml($('.activity-product-table>thead>tr>th'),res))+'</tr>');
                        $('.activity-product-table>tbody>tr:last>td[data-name="gid"]').html(res.group_name);
                        _product();
                    });
                })




            })
            $('.activity-product-table>tbody .edit').off('click').on('click',function (){
                let tr=$(this).closest('tr'),object=JSON.parse(tr.attr("data-data"));
                console.log(object);
                tr.hide().after( '<tr data-edit>'+(_setTable($('.activity-product-table>thead>tr>th'),object))+'</tr>' );
                let g_html='';
                $.each(ProductGroups,function (k,v){
                    g_html +='<option value="'+v.id+'" '+(object&&object.gid&&object.gid==v.id?'selected':'')+'>'+v.name+'</option>'
                })
                $(this).closest('tr').next().find('select[name="gid"]').html(g_html)

                $('.activity-product-table>tbody>tr[data-edit] .exit').off("click").on("click",function (){
                    $(this).closest('tr').prev('tr').show();
                    $(this).closest('tr').remove();

                })
                $('.activity-product-table>tbody>tr[data-edit] .delete').off("click").on("click",function (){
                    let self=$(this),tr=$(this).closest('tr').prev(),object=JSON.parse(tr.attr("data-data"));
                    ajax({
                        data:{
                            action:'delete',
                            type:'product',
                            data:object
                        },success:function (res){
                            self.closest('tr').prev('tr').remove();
                            self.closest('tr').remove();
                        }
                    })
                })
                $('.activity-product-table>tbody>tr[data-edit] .add').off("click").on("click",function (){
                    let self=$(this),object={ };
                    $(this).closest('tr').children('td[data-item]').each(function (){
                        let s=$(this).children('[name]')
                        object[ s.attr('name')  ]=s.val();
                    })
                    _save('product',object,function (res){
                        self.closest('tr').prev('tr').attr('data-data',JSON.stringify(res)).show().html(_setHtml($('.activity-product-table>thead>tr>th'),res));;
                        self.closest('tr').prev('tr').children('td[data-name="gid"]').html(res.group_name);
                        self.closest('tr').remove();
                        _product();
                    });
                })
            })
        }
        _product();
        let _prizesetting=function (){



        $('.add-activity').off('click').on('click',function (){
            $('.activity-table>tbody').append('<tr data-add>'+(_setTable($('.activity-table>thead>tr>th')))+'</tr>');

            $('.activity-table>tbody>tr[data-add] .exit').off("click").on("click",function (){
                  $(this).closest('tr').remove();
            })
            $('.activity-table>tbody>tr[data-add] .add').off("click").on("click",function (){
                let self=$(this),object={ };
                $(this).closest('tr').children('td[data-item]').each(function (){
                    let s=$(this).children('[name]')
                    object[ s.attr('name')  ]=s.val();
                })
                _save('prizesetting',object,function (res){
                    self.closest('tr').remove();
                    $('.activity-table>tbody').append('<tr data-data=\''+JSON.stringify(res)+'\'>'+(_setHtml($('.activity-table>thead>tr>th'),res))+'</tr>');
                    _prizesetting();
                });
            })




        })
            $('.activity-table>tbody .edit').off('click').on('click',function (){

                let tr=$(this).closest('tr'),object=JSON.parse(tr.attr("data-data"));

                tr.hide().after( '<tr data-edit>'+(_setTable($('.activity-table>thead>tr>th'),object))+'</tr>' );
                $('.activity-table>tbody>tr[data-edit] .exit').off("click").on("click",function (){
                    $(this).closest('tr').prev('tr').show();
                    $(this).closest('tr').remove();

                })
                $('.activity-table>tbody>tr[data-edit] .delete').off("click").on("click",function (){
                    let self=$(this),tr=$(this).closest('tr').prev(),object=JSON.parse(tr.attr("data-data"));
                    ajax({
                        data:{
                            action:'delete',
                            type:'ac',
                            data:object
                        },success:function (res){
                            self.closest('tr').prev('tr').remove();
                            self.closest('tr').remove();
                        }
                    })
                })
                $('.activity-table>tbody>tr[data-edit] .add').off("click").on("click",function (){
                    let self=$(this),object={ };
                    $(this).closest('tr').children('td[data-item]').each(function (){
                        let s=$(this).children('[name]')
                        object[ s.attr('name')  ]=s.val();
                    })
                    _save('prizesetting',object,function (res){
                        self.closest('tr').prev('tr').attr('data-data',JSON.stringify(res)).show().html(_setHtml($('.activity-table>thead>tr>th'),res));;
                        self.closest('tr').remove();
                        _prizesetting();
                    });
                })
            })
    }
        _prizesetting();
    })
</script>