<style>
    .dbedit{ color: red;font-size: 10px;}
</style>
<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;">
                                <button style="margin-right:5px;" type="button" class="btn btn-warning goback" >返回</button>
                                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addcoupon">添加普通礼品券</button>
                                <button style="margin-left:5px;" type="button" class="btn btn-success" data-toggle="modal" data-target="#addphonecard">添加电话卡券</button>
                            </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>名称<span class="dbedit">(双击可以编辑)</span></th>
                                                <th>过期天数<span class="dbedit">(双击可以编辑)</span></th>
                                                <th>添加时间</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)){
                                                foreach ($list as $key => $val) {
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $val['cid'];?></td>
                                                        <td class="ajaxedit" tdtype="text" field="cname" cid="<?php echo $val['cid'];?>">
                                                            <textarea class="form-control" rows="5" readonly><?php
                                                                foreach ($val['cnameArr'] as $lang => $v){
                                                                    echo $lang.'='.$v."\r\n";
                                                                }
                                                                ?>
                                                            </textarea>
                                                        </td>
                                                        <td class="ajaxedit" tdtype="input" field="cexpire" cid="<?php echo $val['cid'];?>"><?php echo $val['cexpire'];?></td>
                                                        <td><?php echo $val['ctime'];?></td>
                                                        <td>
                                                            <button type="button" class="del btn btn-danger" cid="<?php echo $val['cid'];?>">删除</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>


<!-- 添加礼品券 Modal Start -->
<div class="modal fade" id="addcoupon" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加礼品券</h4>
            </div>
            <form class="form-horizontal" data-validate="verify" method="post" action="<?php echo base_url('mall/actionAddCoupon')?>">
            <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="phone_card">名称:</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" placeholder="zh=中文名称(一行一种语言)" rows="5" name="cname"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="phone_card">过期天数:</label>
                        <div class="col-sm-10">
                            <input type="text" name="cexpire" placeholder="过期天数填数字 0为永不过期" class="form-control">
                        </div>
                    </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary J_ajaxSubmitBtn">添加</button>
            </div>
            </form>
        </div>
    </div>
</div>
<!-- 添加礼品券 Modal End -->

<!-- 添加电话卡券 Start -->
<div class="modal fade" id="addphonecard" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加电话卡券</h4>
            </div>
            <form class="form-horizontal" method="post" action="<?php echo base_url('mall/actionAddPhonecardCoupon')?>">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="phone_card">名称:</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" placeholder="zh=中文名称(一行一种语言)" rows="5" name="cname"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="phone_card">过期天数:</label>
                        <div class="col-sm-10">
                            <input type="text" name="cexpire" placeholder="过期天数填数字 0为永不过期" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">运营商:</label>
                        <div class="col-sm-10">
                            <input type="text" name="operation" placeholder="运营商名称" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">面额:</label>
                        <div class="col-sm-10">
                            <input type="text" name="amount" placeholder="面额" class="form-control">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary J_ajaxSubmitBtn">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- 添加电话卡券 End -->

<script>

    $('.goback').click(function () {
        window.history.go(-1);
    });

    //验证 名称是否填写
    function verify(){
        var cname = $("textarea[name='cname']");
        if ( $.trim(cname.val()) == '' ){
            cname.focus();
            return '名称为必填参数';
        }
        return true;
    }

    //双击开始编辑
    $('.ajaxedit').dblclick(function () {
        var td = $(this);
        var tdtype = td.attr('tdtype');//类型
        var tdval = td.text();//内容
        var cid = td.attr('cid');
        var field = td.attr('field');
        //设置输入框内容
        if (tdtype == 'text'){
            var input = td.children();
            input.removeAttr('readonly');
        }else{
            td.empty();
            var input = $("<input type='text' class='form-control'>").val(tdval);
            td.append(input);
        }
        //失去焦点, 提交数据
        input.blur(function(){
            var value = $(this).val();
            $.post("<?php echo base_url('mall/actionEditCoupon')?>", {cid: cid, field:field, val: value});
            if (tdtype == 'text'){
                $(this).attr('readonly','readonly');
            }else{
                input.remove();
                td.text(value);
            }
        });
    });

    //删除商品
    $('.del').click(function(){
        if( confirm('确认删除吗?') ){
            var cid = $(this).attr('cid');
            $.ajax({
                type: "POST",
                url : 'mall/actionDelCoupon',
                dataType: "json",
                data: "cid="+cid,
                success:function(e){
                    alert('删除成功');
                    location.reload();
                }
            });
        }
    });
</script>