<link href="<?php echo asset_url('css/fileinput.min.css')?>" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo asset_url('js/fileinput.min.js') ?>"></script>
<script type="text/javascript" src="<?php echo sasset_url('js/fileinput_locale_zh.js') ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo asset_url('css/wendor/style.css');?>">
<style type="text/css">
.file-footer-buttons button{display: none}

    .modal{position: fixed!important;top:20%!important;width: 450px!important;height: 235px!important;background-color: transparent!important;}
     .modal-dialog{margin: 0!important;padding: 0!important;border-radius: 6px!important;}
     .row-fluid input{margin: 10px 0px;}
     .equitycof{margin-right: 0px;}

</style>
<div class="col-md-10" role="main">
    <nav class="breadcrumb"></i> 积分商城模块<span class="c-gray en">&gt;</span>商品管理</nav>
    <div class="xgsj_wbox">

            <div class="title-box" style="border:0;padding: 20px 0px;">
                    <p class="t-p" style="width: 500px;display: inline-block;">商品修改</p>
                    <button type="button" onclick="window.history.back()" class="sjxg_fh" data-dismiss="modal" style="margin-top:8px;">返回</button>
            </div>

            <div class="bs-example">
                <form action="<?php echo site_url('score/goods/save/'.$id)?>" method="post" class="registerForm regiswidth" enctype="multipart/form-data">
                    <input type="hidden" value="<?php echo $cate;?>" name="cate">
                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">*商品名称：</span>
                        <div class="examp">
                            <input type="text" maxlength="16" class="form-control" name="name" value="<?php echo $name;?>">
                           
                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请输入不超过16个汉字含符号。</span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">*类型：</span>
                        <div class="examp">
                            <input type="hidden" name="type" value="<?php echo $type;?>">
                            <div class="biu_rdo rew_rdo" style="float:none;margin-top:9px;">                         
                                <img src="<?php echo asset_url('images/unchoice.png');?>" alt="" class="biu_rdo_img " isc="0" tyid="1"> <span>兑换</span>
                                <img src="<?php echo asset_url('images/unchoice.png');?>" alt="" class="biu_rdo_img " isc="0" tyid="2"> <span>抽奖</span>
                            </div>

                            
                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请选择类型。</span>
                            </div>
                        </div>
                    </div>                
                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">*消耗积分：</span>
                        <div class="examp">
                            <input type="text" class="form-control" name="score" maxlength="7" value="<?php echo $score;?>">
                            

                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请填写单次兑换/抽奖消耗积分，最多为9999999积分。</span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">*数量：</span>
                        <div class="examp">
                            <input type="number" name="num" maxlength="7" class="form-control" value="<?php echo $num;?>">

                             <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：最大数量为9999999个。</span>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">兑换次数：</span>
                        <div class="examp">
                            <input type="text" name="exchange_num" maxlength="4" class="form-control" value="<?php echo $exchange_num;?>">                

                             <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：每个用户最多兑换几次。最大数量为9999次。此处留空则可无限次兑换。</span>
                            </div>
                        </div>
                    </div>                 
                    <div class="form-group" id="chance" style="display:none;">
                        <span class="dianm sjxg_tle_span">*中奖概率：</span>
                        <div class="examp">
                            <select name="chance1" id="chance1" class="selectmr"  style="width:110px;" >
                                <?php foreach ($chances as $v) {
                                ?>
                                <option value="<?php echo $v;?>" <?php if(!empty($chance1) && $v == $chance1) echo 'selected=selected';?>><?php echo $v;?>分</option>
                                <?php
                                }
                                ?>
                            </select>之<input type="text" name="chance2"   style="border:1px solid #ccc;  margin-left: 12px;height: 26px;margin-bottom: 3px;width: 110px;text-indent: 20px;" value="<?php echo !empty($chance2)?$chance2:0;?>">
                            
                             <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请选择中奖概率的分母并填写分子，例如：100000分之5。</span>
                            </div>
                        </div>
                    </div>                
                    <div class="form-group">
                        <span class="dianm sjxg_tle_span">*获奖方式：</span>
                        <div class="examp">
                            <?php
                            if($cate == 1)
                            {
                            ?>
                                <input type="hidden" name="get_type" value="1">
                                <div class="biu_rdo" style="float:none;margin-top:10px;">   
                                    <img src="<?php echo asset_url('images/choice.png');?>" alt="" class="biu_rdo_img " isc="1"> <span>自取</span>
                                </div>
                            <?php
                            }else{
                            ?>  
                                <input type="hidden" name="get_type" value="2">
                                <div class="biu_rdo" style="float:none;margin-top:10px;">   
                                    <img src="<?php echo asset_url('images/choice.png');?>" alt="" class="biu_rdo_img " isc="1"> <span>代充</span>
                                </div>                       
                            <?php
                            }
                            ?>

                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请选择兑换方式。</span>
                            </div>
                        </div>
                    </div>
                    <?php 
                        if($cate == 1)
                        {
                    ?>
                    <div class="form-group" id="address">
                        <span class="dianm sjxg_tle_span">取货地址：</span>
                        <div class="examp">
                            <input type="text" name="address" maxlength="20" class="form-control" value="<?php echo $address;?>">       

                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请填写取货地址，最多20个汉字含符号，不填写默认为联系地址。</span>
                            </div>
                        </div>
                    </div>                
                    <?php
                        }
                    ?>
                    <div class="form-group form-group2" style="width:60%" id="messagelist_container">
                        <span class="cover jianjie sjxg_tle_span">*商品列表：</span>
                        <div class="examp">
                            <p class="shaopupload shaopupload1" style="overflow: hidden;">
                                <input type="file" name="avatar" id="message_piclist" multiple="" class="brefile">
                            </p>
                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                         <span style="font-size:12px;">提示：请上传宽高870*579像素的图片，以保证显示效果。</span>
                            </div>
                        </div>
                    </div>   
                   

                    <div class="form-group form-group2" style="width:60%" id="message_container">
                        <span class="cover jianjie sjxg_tle_span">*商品图：</span>
                        <div class="examp">
                            <p class="shaopupload shaopupload1" style="overflow: hidden;">
                                <input type="file" name="avatar" id="message_pic" multiple="" class="brefile">    
                            </p>
                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                         <span style="font-size:12px;">提示：请上传宽高1280*716像素的图片，以保证显示效果。</span>
                            </div>
                        </div>
                    </div>
  

                    <div class="form-group">
                        <input name="message" type="hidden">
                        <span class="jianjie sjxg_tle_span">*商品简介：</span>
                        <div class="examp">
                            <!-- 此处应该是编辑框 -->
                            <div id="summernote"><?php echo $message;?></div>
                            
                            <div class="lr_ts" style="float:none;margin: 10px 0px;">
                                     <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
                                     <span style="font-size:12px;">提示：请填写商品详情，以及使用规则。</span>
                            </div>

                        </div>
                    </div>
                    <button class="edit_btn  equitycof h5a-btn btn  btn-primary" id="save" type="submit">保存修改</button>
                </form>    
            </div>
        </div>
    </div>

<script type="text/javascript">
   $(function(){
    var type = $('input[name=type]').val();
    if(type == 2)
    {
        $('#chance').show();
    }
    $('.biu_rdo img').each(function(){
     if($(this).attr('tyid') == type)
     {
        $(this).attr('src','<?php echo asset_url("images/choice.png");?>');
        $(this).attr('isc',1);
     }
    });    
     // 单选按钮切换
    $('.biu_rdo img').click(function(){
        var isc=$(this).attr('isc');
        $('input[name=type]').val($(this).attr('tyid'));
        if(isc==0){
            $(this).attr('src','<?php echo asset_url("images/choice.png");?>');
            $(this).attr('isc','1');
            $(this).siblings().attr('src','<?php echo asset_url("images/unchoice.png");?>');
            $(this).siblings().attr('isc','0');
           if($(this).index()==2){
                $('#chance').show();
           }else{
                $('#chance').hide();
           }
        }
    })
})    
 


</script>



<script type="text/javascript">

jQuery(document).ready(function($) {

    $("#back").on('click', function(event) {
        event.preventDefault();
        /* Act on the event */
        location='javascript:history.go(-1)';
    });
    //分子判断
    $("input[name=chance2]").keyup(function(event) {
        /* Act on the event */
        var chance1 = $('#chance1').find("option:selected").val();
        chance1 = parseInt(chance1);
        var chance2 = $(this).val();
        console.log(chance2);
         console.log(chance1);
        var rege = /^[1-9]\d*$/;
        if(chance2 == '')
        {
            $("#save").attr('disabled',true);
            return true;
        }
        if(parseInt(chance2)>chance1 || rege.test(chance2) == false)
        {
            alert("概率不正确");
            $("#save").attr('disabled',true);
            return false;
        }
        $("#save").attr('disabled',false);
        
    });
    //分母判断
    $("select[name=chance1]").change(function(event) {
        /* Act on the event */
        var chance1 = parseInt($(this).val());
        var chance2 = $('input[name=chance2]').val();
        if(chance2 == '')
        {
            $("#save").attr('disabled',true);
            return true;
        }
        if(parseInt(chance2)>chance1)
        {
            alert("概率不正确");
            $("#save").attr('disabled',true);
            return false;
        }
        $("#save").attr('disabled',false);
    }); 
    $('#summernote').summernote({
        height: 300,
        onImageUpload: function(files, editor, welEditable) {
            sendFile(files[0],editor,welEditable);
        }
     
    });    
});

function sendFile(file,editor,welEditable) {
    data = new FormData();
    data.append("file", file);
    $.ajax({
        data: data,
        type: "POST",
        url: "<?php echo site_url('score/goods/summer');?>",
        cache: false,
        contentType: false,
        processData: false,
        success: function(url) {
                editor.insertImage(welEditable, url);
        }
    });
}

function init_upload(target, min, max, name,initialPreview ,initialPreviewConfig){
    var height="";
    var width="";
    if(name == "pic")
    {
        height = 716;
        width = 1280;
    }
    else
    {
        height = 579;
        width = 870;       
    } 
    var ele = $(target);
    ele.fileinput({
        minFileCount : min,
        dropZoneEnabled : false,
        maxFileCount : max,
        minImageHeight : height,
        minImageWidth : width,
        maxImageWidth : width,
        maxImageHeight : height,        
        validateInitialCount: true,
        uploadUrl : '<?php echo site_url('score/goods/upload');?>',
        language: "zh",
        showUpload : false,
        previewSettings : {
            image: {width: "130px", height: "auto"},
            html: {width: "213px", height: "160px"},
            text: {width: "160px", height: "136px"},
            video: {width: "213px", height: "160px"},
            audio: {width: "213px", height: "80px"},
            flash: {width: "213px", height: "160px"},
            object: {width: "213px", height: "160px"},
            other: {width: "160px", height: "160px"}
        },
        initialPreview: initialPreview,
        initialPreviewConfig: initialPreviewConfig,
    });

    ele.on('fileuploaded', function(event, data, previewId, index){ 
        console.log(previewId);
        console.log(index)
        var input = '<input type="hidden" value="'+data.response.url+'" name="'+name+'"/>'
        $(input).appendTo('form');
    });

    ele.on('filebatchselected', function(event, files, extra) {
            $("#save").attr('disabled', false);
    });

    ele.on('fileclear', function(event) {
        $("input[name='"+name+"']").remove();
    });

    $('#input-id').on('filesuccessremove', function(event, id) {
        console.log(id)
    });
    $('#input-id').on('filedeleted', function(event, key) {
        console.log('Key = ' + key);
    });
}

$(function(){
    var message_initialPreview = [
            "<img src='<?php echo $pic;?>' class='file-preview-image' style='width:130px;height:auto'>",
        ];
    var message_initialPreviewConfig = [
            {caption: "", width: "130px", url: "", key:""},
    ];   
    var messagelist_initialPreview = [
            "<img src='<?php echo $piclist;?>' class='file-preview-image' style='width:130px;height:auto'>",
        ];
    var messagelist_initialPreviewConfig = [
            {caption: "", width: "130px", url: "", key:""},
    ];       
    init_upload('#message_pic', 1, 1, 'pic',message_initialPreview,message_initialPreviewConfig)
    init_upload('#message_piclist', 1, 1, 'piclist',messagelist_initialPreview,messagelist_initialPreviewConfig)    
})

$(function(){
    $('.note-editable').on('click',function(event) {
        $('#save').attr('disabled',false);
    });    
    $('.registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh',
            excluded : ':disabled'
        },
        fields: {
            name: {
                message: '商品名称',
                validators: {
                    notEmpty: {
                        message: '商品名称不能为空'
                    },
                    stringLength: {
                        max: 16,
                        message: '商品名称不超过16个字符'
                    }
                }
            },
            address: {
                message: '取货地址',
                validators: {
                    stringLength: {
                        max: 20,
                        message: '取货地址不能超过20个字符'
                    }
                }
            },                       
            score: {
                message: '消耗积分',
                validators: {
                    notEmpty: {
                        message: '消耗积分不能为空'
                    },
                    regexp: {
                        regexp: /^([1-9])\d{0,6}$/i,
                        message: '积分大于0整数',
                    }
                }
            },
            num: {
                message: '数量',
                validators: {
                    notEmpty: {
                        message: '数量不能为空'
                    },
                    regexp: {
                        regexp: /^([1-9])\d{0,6}$/i,
                        message: '数量大于0整数',
                    }
                }
            },
            exchange_num: {
                message: '兑换次数不正确',
                validators: {
                    notEmpty: {
                        message: '兑换次数不能为空'
                    },
                    regexp: {
                        regexp: /^[1-9]\d{0,3}$/i,
                        message: '兑换次数大于0',
                    }
                }                
            },
            message  : {
                message: '商品简介',
                validators: {
                    notEmpty: {
                        message: '商品简介不能为空'
                    }
                }
            }       
        }
    })
.on('success.form.bv',function(e){
            e.preventDefault();
            $("input[name=message]").val($('#summernote').code());

            var str = $('#summernote').code();
            var patt1 = new RegExp("^<p><br></p>$");

            var result = patt1.test(str); 
            if(result == true)
            {
                alert("请输入商品详细规则");
                return false;
            } 

            if($("input[name=type]:checked").val() == 2)
            {
               if($("input[name=chance2]").val() == "")
                {
                    alert("请填写正确的概率");
                    return false;
                }
            }            
            var message_num = $('#message_container').find('.file-preview-thumbnails .file-preview-frame').length;
            var messagelist_num = $('#messagelist_container').find('.file-preview-thumbnails .file-preview-frame').length;
            if(messagelist_num < 1){
                alert('请上传商品列表图');
                window.location.href = '#messagelist_container'
                return false;
            }            
            if(message_num < 1){
                alert('请上传商品图');
                window.location.href = '#message_container'
                return false;
            }
            var form = this;
            e.preventDefault();
            var picNum = $('#message_pic').fileinput('getFileStack').length;
            var piclistNum = $('#message_piclist').fileinput('getFileStack').length;
            if(picNum == 0 && piclistNum==0)
            {
                form.submit();
            }
            else if(picNum == 0 && piclistNum !=0)
            {
                $('#message_piclist').on('filebatchuploadcomplete', function(event, files, extra) {
                   form.submit(); 
                });
                $('#message_piclist').fileinput('upload');               
            }
            else if(picNum != 0 && piclistNum ==0)
            {

                $('#message_pic').on('filebatchuploadcomplete', function(event, files, extra) {
                    form.submit(); 
                });
                $('#message_pic').fileinput('upload');
            }
            else
            {
                $('#message_piclist').on('filebatchuploadcomplete', function(event, files, extra) {
                   
                });
                $('#message_pic').on('filebatchuploadcomplete', function(event, files, extra) {
                    form.submit(); 
                }); 
                $('#message_pic').fileinput('upload');
                $('#message_piclist').fileinput('upload');                
            }
    })
    
})

</script>