<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
    <!--validate css-->
    <link rel="stylesheet" href="plugins/jquery-validation/css/validate.css">
    <!--fileinput css-->
    <link rel="stylesheet" href="plugins/bootstrap-fileinput/fileinput.min.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>

    <![endif]-->
</head>
<style>
    .kv-file-upload{display:none !important;}
    .borederStyleOut{
        border: 1px solid #aaa;
        border-radius: 4px;
        height: 100%;
        vertical-align: middle;
        padding: 5px;
    }
    .borederStyle{
        border: 1px dashed #aaa;
        border-radius: 4px;
        height: 100%;
        vertical-align: middle;
        margin: 12px 15px 12px 12px;
        padding: 5px;
    }
    .borederStyle img{
        width:auto;
        height: 100px;
        margin: 5px;
    }
    .hide {
        display:none;
    }
</style>
<!-- END HEAD -->
<body class="hold-transition skin-blue sidebar-mini">

<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">

        <section class="content-header">
            <h1>
                素材上传管理
                <small>素材上传</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a class="active">素材上传</a></li>
            </ol>
        </section>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <!-- general form elements disabled -->
                    <div class="box box-primary">
                        <form role="form" id="myForm" enctype="multipart/form-data" method="post" action="#springUrl('/a/materialUpload/formData')">
                            <input name="id" value="$!{data.id}" hidden="true"/>
                            <div class="box-body">
                                <div class="form-group form-md-line-input">
                                    <label>标题</label>
                                    <input type="text"
                                           class="form-control" name="title"
                                           id="title"
                                           maxlength="100" placeholder="标题"
                                           value="$!{data.title}"
                                           required="required"
                                    >

                                </div>
                                <div class="form-group form-md-line-input">
                                    <label>素材存储位置</label>
                                    <select name="cosconfigId" class="form-control">
                                        <option value="0">本地</option>
                                        #foreach($cosconfig in $cosconfigList)
                                            #if($cosconfig.cosType == "qcloud")
                                                <option value="$cosconfig.id">腾讯</option>
                                            #end
                                            #if($cosconfig.cosType == "ali")
                                            <option value="$cosconfig.id">阿里</option>
                                            #end
                                            #if($cosconfig.cosType == "qiniu")
                                            <option value="$cosconfig.id">七牛</option>
                                            #end
                                        #end
                                    </select>
                                </div>
                                <div class="form-group form-md-line-input borederStyleOut">
                                    <div id="resultview" class="borederStyle">
                                        <img src="/cfwechat/static/img/timg.jpg" onclick = "uploadImg('file')"/>
                                    </div>
                                    <p>
                                        <input class="hide" name="imgArr" type="file" id="file1" onchange = "readAsDataURL('file1')"/>
                                        <input class="hide" name="imgArr" type="file" id="file2" onchange = "readAsDataURL('file2')"/>
                                        <input class="hide" name="imgArr" type="file" id="file3" onchange = "readAsDataURL('file3')"/>

                                    </p>
                                </div>
                                <div class="box-footer">
                                    #if($shiro.hasPermission("material:edit"))
                                    <input class="btn btn-primary" id="save" value="保存" type="submit">
                                    #end
                                    <a href="#springUrl('/a/materialUpload/list')" class="btn btn-default">取消</a>
                                    <!--<button class="btn btn-primary" id="uploadBtn">上传</button>-->
                                </div>
                            </div>
                        </form>
                        <!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->


    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<script src="plugins\bootstrap-fileinput\fileinput.js"></script>
<script src="plugins/bootstrap-fileinput/zh.js"></script>
<!--fileinput js-->
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script src="plugins/bootstrap-maxlength/bootstrap-maxlength.min.js" type="text/javascript"></script>
<script src="plugins/security/sha256.js" type="text/javascript"></script>
<script src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script src="plugins/jquery-validation/js/localization/messages_zh.js"></script>
<script src="js/jquery.form.min.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.all.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/lang/zh-cn/zh-cn.js"></script>
<script src="plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<script src="common/js/cfapp.js"></script>
<script src="js/jquery.serializejson.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script>
    $().ready(function () {
        Cfapp.init();
//        recdTypeAdd.init();
        $('.datepicker').datepicker({
            show: true,
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: 'zh-CN',
            todayBtn: 'linked',
            clearBtn: 'linked'

        });

        Cfapp.initFileInput({
            uploadUrl: '#springUrl("/a/fileinput/upload")',
            deleteUrl: '#springUrl("/a/fileinput/delete")',
            imgs: "",
            ctrName: '#uploadBtn',
            maxFileCount: 5,
            valName: '#imgs',
            uploadExtraData: {
                className: 'meterial',
            }

        });
    });
    $("#myForm").validate({
        rules: {},
        messages: {},
        submitHandler: function (form) {
            $("#save").attr("disabled", true);
            var options = {
                url : "#springUrl('/a/materialUpload/formData')",
                type : "POST",
                dataType: "json",
                data: {},
                success : function(data) {
                    $("#save").attr("disabled",false);
                    if (data.errorNo == 0) {  //保存成功
                        Cfapp.confirm({
                            message: "创建成功",
                            btnoktext: "继续创建",
                            btncanceltext: "关闭",
                            success: function () {
                                location.href = "#springUrl('/a/materialUpload/form')";
                            },
                            cancel: function () {
                                location.href = "#springUrl('/a/materialUpload/list')";
                            }
                        });
                    } else {
                        Cfapp.alert({
                            message: "创建失败",
                            btntext: "确定",
                            success: function () {
                                location.href = "#springUrl('/a/materialUpload/list')";
                            }
                        });
                    }
                },error:function(data){
                    $("#save").attr("disabled",false);
                }
            };
            $("#myForm").ajaxSubmit(options);
        }
    })
    var result = $("#resultview");
    if(typeof FileReader == 'undefined') {
        result.innerHTML = "抱歉，你的浏览器不支持FileReader";
    }
    // 将文件以Data URL形式进行读入页面
    function readAsDataURL(id){
        // 检查是否为图像类型
        var idTemp = "#"+id;
        var simpleFile = $(idTemp)[0].files[0];
        if(!/image\/\w+/.test(simpleFile.type)) {
            alert("请确保文件类型为图像类型");
            return false;
        }
        var reader = new FileReader();
        // 将文件以Data URL形式进行读入页面
        reader.readAsDataURL(simpleFile);
        reader.onload = function(e){
            console.log(this.result);
            var imgTemp = document.createElement("img");
            imgTemp.setAttribute('src', this.result);
            result.children(":first").before(imgTemp);
//            result.append(imgTemp);
        }
    }
    //触发选择文件
    function uploadImg(id){
        var imgLength = $("#resultview").children('img').length
        if(imgLength>3){
            alert("最多上传3张图片")
            return false;
        }
        id+=imgLength;
        document.getElementById(id).click();
    }
</script>
</body>
<!-- END BODY -->
</html>