<div class="content_wrapper">

    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>礼物管理</header>
                    <section>
                        <div class="container-fluid">
                            <div class="btn-group" style="margin-bottom:20px;">
                                <a href="<?php echo base_url('/#gift/add/')?>" class="btn btn-success" role="button"><i class="fa fa-plus-square"></i>添加礼物</a>
                                <a style="margin-left: 10px;" href="#" class="btn btn-success createconfigfile" role="button">生成配置</a>
                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="table-responsive">
                                        <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                            <thead>
                                            <tr>
                                                <th>礼物ID</th>
                                                <th>中文名称</th>
                                                <th>泰文</th>
                                                <th>类型</th>
                                                <th>价格</th>
                                                <th>图片</th>
                                                <th>属性</th>
                                                <th>过期天数</th>
                                                <th>排序</th>
                                                <th>是否显示</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)){
                                                foreach ($list as $key => $val) {
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $val['id'];?></td>
                                                        <td class="cname_<?php echo $val['id'];?>"><?php echo $val['cname'];?></td>
                                                        <td class="ename_<?php echo $val['id'];?>"><?php echo $val['ename'];?></td>
                                                        <td class="type_<?php echo $val['id'];?>"><?php echo $val['type'];?></td>
                                                        <td class="price_<?php echo $val['id'];?>"><?php echo $val['price'];?></td>
                                                        <td><?php echo $val['img'];?></td>
                                                        <td class="property_<?php echo $val['id'];?>"><?php echo $val['property'];?></td>
                                                        <td class="expire_<?php echo $val['id'];?>"><?php echo $val['expire'];?></td>
                                                        <td class="gsort_<?php echo $val['id'];?>"><?php echo $val['gsort'];?></td>
                                                        <td class="isShow_<?php echo $val['id'];?>"><?php if($val['isShow'] == 1) {echo '<span class="label label-success">是</span>';}else {echo '<span class="label label-default">否</span>';};?></td>
                                                        <td>
                                                            <!-- <button type="button" class="btn btn-primary giftdetail" data-toggle="modal" data-target="#myModal" gid="<?php echo $val['id']; ?>" >编辑礼物</button> -->
                                                            <a href="<?php echo base_url('/#gift/edit/'.$val['id']);?>" class="btn btn-primary active" role="button">编辑礼物</a>
                                                            <button type="button" class="delgift btn btn-danger" gid="<?php echo $val['id'];?>">删除</button>
                                                        </td>
                                                    </tr>
                                                <?php } } else{?>
                                                <tr>
                                                    <td colspan="10">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <nav>
                                        <ul class="pagination">
                                            <li class="disabled"><a href="#">共<span class="pagetotal"><?php echo $total;?></span>条</a></li>
                                            <?php echo $pageurl;?>
                                        </ul>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<!-- 礼物详情 START-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <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" role="form" method="post" action="gift/edit" enctype="multipart/form-data">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">当前图片:</label>
                        <div class="col-sm-9">
                            <img class="giftimg" src="" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">中文名称:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="中文名称" name="cname">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">泰文:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="泰文" name="ename">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">礼物类型:</label>
                        <div class="col-sm-9">
                            <?php foreach ($giftType as $v => $n) {?>
                                <label class="radio-inline">
                                    <input type="radio" name="type" cname="<?php echo $n;?>" value="<?php echo $v;?>"> <?php echo $n;?>
                                </label>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">价格:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="价格" name="price">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">礼物图片</label>
                        <div class="col-sm-9">
                            <input type="file" name="giftfile">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">属性:</label>
                        <div class="col-sm-9">
                            <?php foreach ($giftProperty as $v => $n) {?>
                                <label class="radio-inline">
                                    <input type="radio" name="prop" cname="<?php echo $n;?>" value="<?php echo $v;?>"> <?php echo $n;?>
                                </label>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">有效天数:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="有效天数" name="expire">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- 礼物详情 END-->
<script>
    //查看详情按钮
    $(".giftdetail").click(function () {
        var gid = $(this).attr('gid');
        var cname = $('.cname_'+gid).text();
        var ename = $('.ename_'+gid).text();
        var type = $('.type_'+gid).text();
        var price = $('.price_'+gid).text();
        var img = $('.img_'+gid).attr('src');
        var property = $('.property_'+gid).text();
        var expire = $('.expire_'+gid).text();

        //插入到表单
        $("input[name='cname']").val(cname);
        $("input[name='ename']").val(ename);
        $("input[name='price']").val(price);
        $("input[name='expire']").val(expire);

        $('.giftimg').attr('src', img);

        $.each($("input[name='type']"), function(i, e){
            if( e.getAttribute('cname') == type ){
                e.setAttribute('checked', true);
            }
        });

        $.each($("input[name='prop']"), function(i, e){
            if( e.getAttribute('cname') == property ){
                e.setAttribute('checked', true);
            }
        });

    });

    //删除礼物
    $('.delgift').click(function(){
        if( confirm('确认删除吗?') ){
            var gid = $(this).attr('gid');
        }
        $.ajax({
            type: "POST",
            url : 'gift/actionDelgift',
            dataType: "json",
            data: "gid="+gid,
            success:function(e){
                if(e.num >= 1){
                    alert('删除成功');
                    location.reload();
                }else{
                    alert('删除失败');
                }
            }
        });
    });

    //生成配置文件
    $('.createconfigfile').click(function(){
        if ( confirm('确定生成礼物配置文件吗?')){
            $.ajax({
                type: "POST",
                url : 'gift/createConfigFile',
                success:function(e){
                    alert('生成成功');
                }
            });
        }
    });

</script>