<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/bootstrap/bootstrap.min.css" >

</head>
<body>
<div id="msg-tip">

</div>
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>添加商品</header>
                    <hr/>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <input type="hidden" value="<?php echo $url;?>" id="uploadUrl"/>
                                    <input type="hidden" value="<?php echo $baseUrl;?>" id="baseUrl"/>
                                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="uploadForm" action="#">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label"></label>
                                            <div class="col-sm-3" >
                                                <img src="" width="90px;" height="90px;" id="imgDisplay" class="hide"/>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">商品图片</label>
                                            <div class="col-sm-3">
                                                    <input type="file" name="giftfile" id="giftfile" />
                                            </div>
                                        </div>
<!--                                        <button type="submit" class="btn btn-default">Submit</button>-->
                                    </form>

                                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" id="addForm">
                                        <input type="hidden" value="" id="uploadedFile" name="uploadedFile"/>
                                        <div class="modal-body">
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">商品名称*: </label>
                                                <div class="col-sm-3">
                                                    <textarea class="form-control" name="mname" rows="5" placeholder="zh=中文名称(一行一种语言)"></textarea>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">礼券类型*:</label>
                                                <div class="col-sm-3">
                                                    <?php foreach ($coupon as $v => $n) {?>
                                                        <label class="radio-inline">
                                                            <input type="radio" name="mcoupon" <?php if($v == 1) echo 'checked'; ?> 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-3">
                                                    <select class="form-control" name="mcouponname">
                                                        <?php foreach ($couponSelect as $v => $n) {?>
                                                            <option value="<?php echo $v;?>"><?php echo $n;?></option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">数量*:</label>
                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" placeholder="数量" name="mnum" value="">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">状态*:</label>
                                                <div class="col-sm-3">
                                                    <label class="radio-inline">
                                                        <input type="radio" name="mstatus"  checked value="1"  > 上架
                                                    </label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="mstatus" value="2"  > 下架
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">排序*:</label>
                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" placeholder="排序" name="msort" value="">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-sm-offset-3 col-sm-3">
                                                <button type="button" class="btn btn-info goback">返回</button>
                                                <button type="submit" class="btn btn-primary">提交</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</body>
<!--<script src="--><?php //echo base_url();?><!--public/plugins/validate/jquery.validate-1.15.0.min.js"></script>-->
<!--<script src="--><?php //echo base_url();?><!--public/plugins/validate/jaquery.validate.messages_zh.js"></script>-->
<script src="<?php echo base_url();?>public/js/common.js"></script>
</html>
<script type="text/javascript">
    $('.goback').click(function(){
       history.go(-1);
    });

    var imgDisplay = $('#imgDisplay'),
        msgTip = $('#msg-tip');
    //上传图片
    $('#giftfile').change(function () {
        //置空保存已上传的图片路径
        $('#uploadedFile').val('');
        console.log('change');
        var dataUpload = new FormData(document.getElementById("uploadForm")),
            url = $('#uploadUrl').val(),
            baseUrl = $('#baseUrl').val();
        dataUpload.append("imgDir", "mall/"); //相对链接imgupload.php的文件的路径
        $.ajax({
            url: url,
            type: 'POST',
            data: dataUpload,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                if(data.ret != 0) {//上传失败
                    alert(data.msg);
                }else{
                    // msgTip.messager().success(data.msg);
                    $('#uploadedFile').val(data.data.imgPath);
                    imgDisplay.removeClass('hide');
                    $('#imgDisplay').attr('src', baseUrl + '/mall/' + data.data.imgPath);
                }
            },
            error: function (msg) {
                alert('网络错误，请刷新后重试');
            }
        }) ;
    });

    //ajax表单 提交
    $("#addForm").submit(function(){
        var uploadedFile = $("#uploadedFile").val();//获取图片
        var mname = $("textarea[name='mname']").val();//名称
        var mcoupon = $("input[name='mcoupon']:checked").val();//礼券类型
        var mcouponname = $("select[name='mcouponname']").val();//礼券ID
        var mstatus = $("input[name='mstatus']:checked").val();//状态
        var mnum = $("input[name='mnum']").val();//数量
        var msort = $("input[name='msort']").val();//排序

        if ($.trim(uploadedFile) == '' || $.trim(mname) == '' || $.trim(mcoupon) == '' || $.trim(mstatus) == '' || $.trim('mnum') == '' || $.trim(msort) == ''){
            alert('全部为必填');
            return false;
        }

        $.ajax({
            url : "<?php echo base_url('mall/actionAdd');?>",
            dataType : 'json',
            type : 'POST',
            data:{uploadedFile:uploadedFile, mname:mname, mcoupon:mcoupon, mcouponname:mcouponname, mstatus:mstatus, mnum:mnum, msort:msort},
            success: function(data){
                if(data.ret !== 0) {
                    msgTip.messager().error(data.msg);
                }else {
                    location.href = rootPath + '/#mall/getList';
                }
            },
            error:function () {
                msgTip.messager().error('网络错误，请重试');
            }
        });

        return false;
    });

    //单选切换
    $("input[name='mcoupon']").click(function(){
        var v = $(this).val();
        if (v == 2 || v == 3){
            $("select[name='mcouponname']").attr({'disabled':'disabled', 'readonly':'readonly'});
        }else{
            $("select[name='mcouponname']").removeAttr('disabled');
            $("select[name='mcouponname']").removeAttr('readonly');
        }
    });


</script>