<!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">
    <!-- Select2 -->
    <link rel="stylesheet" href="plugins/select2/select2.min.css">
    <!--fileinput css-->
    <link rel="stylesheet" href="plugins/bootstrap-fileinput/bootstrap-fileinput.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]-->
    <link rel="stylesheet" href="common/css/uePicPicker.css">
</head>
<!-- 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">
                            <input name="id" value="$!{data.id}" hidden="true"/>
                            <div class="box-body">
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>所属分类</label>
                                <select class="form-control pull-right select2 role" multiple="multiple" data-placeholder="请选择分类">
                                    #foreach( $classify in $list )
                                    <option value="${classify.id}" data-attr="${role.roleSign}">${classify.classifyName}</option>
                                    #end
                                </select>

                            </div>

                            <div class="form-group form-md-line-input col-md-12" id="img">
                            </div>

                            <div class="form-group form-md-line-input col-md-12" id="imgDetail">
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>产品名称</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.productName}" maxlength="500" required
                                       name="productName" placeholder="产品名称"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>通用名</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.commonName}" maxlength="500" required
                                       name="commonName" placeholder="通用名"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>剂型</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.dosagaFrom}" maxlength="500" required
                                       name="dosagaFrom" placeholder="剂型"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>规格</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.format}" maxlength="500" required
                                       name="format" placeholder="规格"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>批准文号</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.approveNumber}" maxlength="500" required
                                       name="approveNumber" placeholder="批准文号"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>生产企业</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.manufacturer}" maxlength="500" required
                                       name="manufacturer" placeholder="生产企业"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>推荐计量</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.recommendMeasure}" maxlength="500" required
                                       name="recommendMeasure" placeholder="推荐计量"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>药品简介</label>
                                <textarea type="text" required class="form-control pull-right"
                                       value="" maxlength="500" required rows="5"
                                       name="description" placeholder="药品简介"
                                >$!{data.description}</textarea>
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>温馨提示</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.tips}" maxlength="500" required
                                       name="tips" placeholder="温馨提示"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>服用类型</label>
                                <select name="takeType" class="form-control pull-right">
                                    <option label="天" value="天">天</option>
                                    <option label="周" value="周" #if($data.takeType == '周') selected #end>周</option>
                                    <option label="月" value="月" #if($data.takeType == '月') selected #end>月</option>
                                </select>
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>服用频率<span style="color:red;">(格式“ 1/10 ” ：1表示每天服用数量，10表示单盒药品总数)</span></label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.takeFrequency}" maxlength="500" required
                                       name="takeFrequency" placeholder="1/100"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>服用数量</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.takeAmount}" maxlength="200" required
                                       name="takeAmount" placeholder="服用数量"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>库存</label>
                                <input type="text" class="form-control pull-right"
                                       value="$!{data.stock}" maxlength="200" readonly
                                       name="stock" placeholder="库存"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>价格</label>
                                <input type="text" required class="form-control pull-right"
                                       value="$!{data.price}" maxlength="200" required
                                       name="price" placeholder="价格"
                                >
                            </div>

                            <div class="form-group form-md-line-input col-md-12">
                                <label>单位</label>
                                <select name="unit" class="form-control pull-right">
                                    <option label="盒" value="盒">盒</option>
                                    <option label="瓶" value="瓶" #if($data.unit == '瓶') selected #end>瓶</option>
                                    <option label="包" value="包" #if($data.unit == '包') selected #end>包</option>
                                </select>
                            </div>

                            <input type="hidden" id="redingType" name="redingType" value="$!{data.isreading}"/>
                            <input type="hidden" id="sizeType" name="sizeType" value="$!{data.size}"/>
                            <div class="form-group form-md-line-input col-md-12">
                                <label>是否展示销量</label>
                                <select id="isreading" name="isreading" class="form-control pull-right">
                                    <option id="redingYes" name="redingYes" value="1" >是</option>
                                    <option id="redingNo" name="redingNo" value="0" >否</option>
                                </select>
                            </div>
                            <div class="form-group form-md-line-input col-md-12">
                                <label>是否好药推荐</label>
                                <select id="size" name="size" class="form-control pull-right">
                                    <option id="sizeYes" name="sizeYes" value="1" >是</option>
                                    <option id="sizeNo" name="sizeNo" value="0" >否</option>
                                </select>
                            </div>
                            <div class="form-group form-md-line-input col-md-12">
                                <label>是否为处方药</label>
                                <select name="isRs" class="form-control pull-right">
                                    <option label="是" value="是"  #if($data.isRs == '是') selected #end>是</option>
                                    <option label="否" value="否"  #if($data.isRs == '否') selected #end>否</option>
                                </select>
                            </div>

                            <div  class="form-group form-md-line-input col-md-12">
                                <label>销量</label>
                                <input type="text"  class="form-control pull-right"
                                       value="$!{data.reading}" maxlength="200" required
                                       name="reading" placeholder="销量"
                                >
                            </div>



                            <input type="text" style="display: none" name="_csrf" value="${_csrf.token}"/>
                            <input  type="text" style="display: none" name="_csrf_header" value="${_csrf.headerName}"/>

                            <div class="box-footer">
                                #if($shiro.hasPermission("qy:product:edit"))
                                <input class="btn btn-primary" id="save" value="保存" type="submit">
                                #end
                                <a href="#springUrl('/a/product/list')" class="btn btn-default">取消</a>
                            </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>
<!--fileinput js-->
<script src="plugins/bootstrap-fileinput/bootstrap-fileinput.js"></script>
<!-- AdminLTE App -->
<!-- Select2 -->
<script src="plugins/select2/select2.full.min.js"></script>
<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="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="common/js/cfapp.js"></script>
<script src="js/jquery.serializejson.js"></script>
<script src="plugins/jquery-validation/js/localization/messages_zh.js"></script>
<script src="plugins/Sortable/Sortable.min.js"></script>
<script src="common/component/uePicPicker.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script>

    let description = `$!{data.description}`;
    if (description != null && description != '') {
        $("textarea[name='description']").text(description.replace(/<br\/>/g, "\r\n"));
    }

    $().ready(function () {


        let redingType = $("#redingType").val();
        let redingYes = $("#redingYes").val();
        if (redingType==redingYes){
            $("#redingYes").attr("selected","selected")
        }else{
            $("#redingNo").attr("selected","selected")
        }
        let sizeType = $("#sizeType").val();
        let sizeYes = $("#sizeYes").val();
        if (sizeType==sizeYes){
            $("#sizeYes").attr("selected","selected")
        }else{
            $("#sizeNo").attr("selected","selected")
        }
        Cfapp.init();
        recdTypeAdd.init();
        initPicsSel();
        initPicsSelDetail();
        $(".select2").select2();

        var classifyId ="$!{data.classifyId}";
        $(".select2").val(classifyId.split(",")).trigger("change");
    });

    var coverPP = null;
    var coverPPDetail = null;
    var initPicsSel = function () {

        var smallimg = "$!{data.productImg}";
        coverPP = new uePicPicker({
            tarId: 'img',
            title: '产品封面图',
            sizeDes: '205*202',
            max: 1,
            datas: smallimg=='' ? null : smallimg.split(","),
            success: null
        });
        coverPP.init();
    };

    var initPicsSelDetail = function () {

        var smallimg = "$!{data.productImgDetail}";
        coverPPDetail = new uePicPicker({
            tarId: 'imgDetail',
            title: '产品详情图',
            sizeDes: '205*202',
            max: 4,
            datas: smallimg=='' ? null : smallimg.split(","),
            success: null
        });
        coverPPDetail.init();
    };

    var classify = "#springUrl('/a/productclassify/listData')";

    $("#columnSelect").click(function () {
        Cfapp.f7({
            dataUrl: classify,
            checkType: 'multi', // 多选为multi
            title: "选择产品分类",
            btnoktext: "确定",
            btncanceltext: "取消",
            columns: ['选择', '产品分类名称', '产品分类图片'],
            isSearch: '0',
            searchColumns: ["classifyName"],//搜索的字段名称
            searchColumnsRemarks: ['产品分类名称'],//搜索的字段备注
            aoColumns: [{
                "mData": "id"
            }, {
                "mData": "classifyName"
            }, {
                "mData": "classifyImg"
            }],
            aoColumnDefs: [{
                "aTargets": [0],
                "mData": "id",
                "mRender": function (a, b, c, d) {
                    return '<input type="radio" id="' + a + '" name="sel" data-value="' + a + '" data-name="' + c.classifyName + '">';
                }
            }, {
                "aTargets": [2],
                "mData": "classifyImg",
                "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                    return "<img src='" + a + "' style='width: 80px;height:80px;' />";
                }
            }],
            success: function () {

            },
            cancel: function () {

            },
            storeFields: 'classifyId', // 储存的字段
            displayFields: 'classifyName'
        })
    });

    var recdTypeAdd = function () {
        var initForm = function () {
            var initFormCtrl = function () {
                bindEvent();
            };
            var bindEvent = function () {

                $("#myForm").validate({
                    rules: {},
                    messages: {},
                    submitHandler: function (form) {
                        $("#save").attr("disabled", true);

                        console.log($("textarea[name='description']").val());
                        var data =  $("#myForm").serializeJSON();
                        var productImg = coverPP.getStringValues().split(',')[0];
                        var productImgDetail = coverPPDetail.getStringValues();
                        var classify = $('.select2').select2('val').toString();
                        data.classifyId = classify;
                        data.productImg = productImg;
                        data.productImgDetail = productImgDetail;

                        $.post("#springUrl('/a/product/formData')", data, function (returnobj) {
                            $("#save").attr("disabled", false);
                            if (returnobj.errorNo == 2) {  //保存成功
                                Cfapp.confirm({
                                    message: "添加成功",
                                    btnoktext: "继续添加",
                                    btncanceltext: "关闭",
                                    success: function () {
                                        location.href = "#springUrl('/a/product/form')";
                                    },
                                    cancel: function () {
                                        location.href = "#springUrl('/a/product/list')";
                                    }
                                });
                            } else if (returnobj.errorNo == 0) {  //修改成功
                                Cfapp.alert({
                                    message: "更新成功",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/product/list')";
                                    }
                                });
                            } else {
                                Cfapp.alert({
                                    message: "创建失败",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/product/list')";
                                    }
                                });
                            }
                        });
                    }
                })
            }
            initFormCtrl();
        }
        return {

            //main function to initiate the module
            init: function () {
                initForm();
            }

        };
    }();
</script>
</body>
<!-- END BODY -->
</html>