<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>终端版本管理</title>
    <link rel="stylesheet" href="${ctx}/css/plugin/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="${ctx}/js/plugin/layui/css/layui.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/fontawesome/font-awesome.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/datatables.bootstrap.css">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <style>

        .form-control {
            height: 22px;
            padding: 1px 5px;
        }
        .control-label{
            height: 22px;
            line-height: 22px;
        }
        .btn {
            padding: 1px 6px;
            font-size:12px;
        }
        .col-sm-3,.col-xs-3{
            padding-right: 0px;
            text-align: right;
        }
    </style>
</head>
<body class="container-fluid">
<form class="form-horizontal" role="form">
    <div class="form-group mt15">
        <label for="title" class="col-sm-3 col-xs-3 control-label">版本号:</label>
        <div class="col-sm-9 col-xs-9">
            <input type="text" id="title"   placeholder="请输入程序版本号"
                   autocomplete="off" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label for="edProtocol" class="col-sm-3 col-xs-3 control-label">协议版本:</label>
        <div class="col-sm-9 col-xs-9">
            <input type="text" id="edProtocol" name="edProtocol"
                   placeholder="请输入协议版本" autocomplete="off" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label for="edDate" class="col-sm-3 col-xs-3 control-label">版本日期:</label>
        <div class="col-sm-9 col-xs-9">
            <input  type="text" class="form_datetime form-control"
                   id="edDate" autocomplete="off" placeholder="请选择日期">
        </div>
    </div>
    <div class="form-group">
        <label for="devType" class="col-sm-3 col-xs-3 control-label">装置系列:</label>
        <div class="col-sm-9 col-xs-9">
            <select  id="devType" class="form-control"></select>
        </div>
    </div>
    <div class="form-group">
        <label for="edtype" class="col-sm-3 col-xs-3 control-label">版本类型:</label>
        <div class="col-sm-9 col-xs-9">
            <input type="text" id="edtype"   placeholder="填写特殊类型(不填默认通用类型)"
                   autocomplete="off" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label for="describe" class="col-sm-3 col-xs-3 control-label">描述:</label>
        <div class="col-sm-9 col-xs-9">
            <textarea id="describe"  placeholder="请输入内容" class="form-control" rows="3" style="resize: none;"></textarea>
        </div>
    </div>

    <div class="layui-upload" style="height: 50px;margin-top: 15px">
        <button type="button" class="btn btn-primary" id="upload">上传升级文件</button>
        <span style="color: red;margin-left: 10px">后缀为.bin的文件/无后缀文件</span>
        <div class="layui-upload-list" style="margin: 0px 0;padding: 2px 5px">
            <p id="demoText"></p>
        </div>
    </div>
    <div class="form-group">
        <label for="crc" class="col-sm-3 col-xs-3 control-label">CRC校验:</label>
        <div class="col-sm-9 col-xs-9">
            <input  type="text" class="form-control"
                    id="crc" autocomplete="off" placeholder="请输入内容">
        </div>
    </div>

    <footer class="navbar-fixed-bottom">

        <div class="col-xs-12 col-sm-12 mt10" style="text-align: center">
            <div class="form-group">
                <button type="button" class="btn btn-primary" id="save">保存</button>
            </div>
        </div>

    </footer>
</form>
<input type="hidden" id="username" value="${username}">
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/js/plugin/layui/layui.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap.min.js"></script>
<script src="${ctx}/jspJS/utils/loading.js"></script>
<script src="${ctx}/js/plugin/xss/xss.js"></script>
<script src="${ctx}/js/plugin/xss/aes.js"></script>
<script src="${ctx}/jspJS/sm2.js"></script>
<script src="${ctx}/jspJS/sm3.js"></script>
<script src="${ctx}/jspJS/confirmIndentity.js"></script>
<script src="${ctx}/jspJS/utils/common.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script>
    var myFile =null;
    var isUpFile = true;
    var edIndex; //版本id
    var indexi
    var type = getQueryString('type')
    var totype = ''; //新增修改接口
    $(function () {
        loadselect('装置系列','devType')

        $('.form_datetime').datetimepicker({
            minView: "month", //选择日期后，不会再跳转去选择时分秒
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            startView:2,
            bootcssVer:3,
            todayHighlight:true,
            maxView :2,
            showClear:true,
            autoclose:1,
            initialDate:new Date(),
        });





    })
    layui.use(['upload','layer'], function () {
        var upload = layui.upload,
            layer = layui.layer

        if (type == 1) {
            //新增
            isUpFile = true;
            totype = 'addVersion'
        } else {
            edIndex = getQueryString('edIndex')
            totype = 'updateVersion'
            if (edIndex == undefined || edIndex == '' || edIndex == null) {
                layer.msg('获取失败', {icon: 2})
                return
            }
            //修改
            $.ajax({
                type: 'get',
                dataType: 'json',
                url: '/pqs9900/version/getVersion',
                data: {edIndex: edIndex},
                beforeSend: function () {
                    indexi = ityzl_SHOW_LOAD_LAYER();
                },
                success: function (data) {
                    if (data.code == 200) {
                        let da= data.body
                        $('#title').val(da.edName)
                        $('#describe').val(da.edDescribe)
                        $('#edProtocol').val(da.edProtocol)
                        $('#edDate').val(da.edDate)
                        $('#devType').val(da.devType)
                        $('#edtype').val(da.type)
                        if (data.body.edFile != null && data.body.edFile != '') {
                            isUpFile = false
                            $('#demoText').html('<i class="glyphicon glyphicon-folder-open" style="font-size: 14px"></i>');
                        } else {
                            isUpFile = true
                            $('#demoText').empty()
                        }
                    } else {

                    }
                    ityzl_CLOSE_LOAD_LAYER(indexi);
                },
                error: function () {

                }
            })
        }
        var censz = /^[\w\u4E00-\u9FA5()（）/、_ ]+\.?[\w\u4E00-\u9FA5()（）/_]+$/;

        $('#save').click(function () {
            var t = $('#title').val()
            var d = $('#describe').val()
            var pp = $('#edProtocol').val()
            var edate = $('#edDate').val()
            var devType = $('#devType').val()
            var edtype = $('#edtype').val()
            var crc = $('#crc').val()
            if(t==null || t==''){
                layer.tips('版本号不可为空', '#title', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }else if(!censz.test(t)){
                layer.tips('非法字符串', '#title', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }else {
                let vv = valiteLength(t)
                if(vv!=''){
                    layer.tips(vv, '#title', {
                        tips: [1, 'red'] //还可配置颜色
                    });
                    return;
                }
            }


            if(pp==null || pp==''){
                layer.tips('协议版本不可为空', '#edProtocol', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }else if(!(/^[a-zA-Z\d\.]+$/.test(pp))){
                layer.tips('非法字符串', '#edProtocol', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }else {
                let vv = valiteLength(pp)
                if(vv!=''){
                    layer.tips(vv, '#edProtocol', {
                        tips: [1, 'red'] //还可配置颜色
                    });
                    return;
                }
            }

            if(edate==null || edate==''){
                layer.tips('版本日期不可为空', '#edDate', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }else if(!(/^([0-9-]+)$/.test(edate))){
                layer.tips('非法字符串', '#edDate', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }

            if(devType==null || devType==''){
                layer.tips('装置系列不可为空', '#devType', {
                    tips: [1, 'red'] //还可配置颜色
                });
                return;
            }

            t = filterXSS(t)
            d = filterXSS(d)
            pp =filterXSS(pp)
            edate = filterXSS(edate)
            edtype = filterXSS(edtype)
            let formdatas = new FormData();
            formdatas.append("edName", t);
            formdatas.append("edProtocol",pp);
            formdatas.append("edDate", edate);
            formdatas.append("edDescribe", d);
            formdatas.append("devType", devType);
            formdatas.append("type", edtype);
            let resetName = ''
            var username = $("#username").val();
            if (type == 2) {
                //修改
                resetName = '修改版本'
                if(isUpFile && myFile==null){
                    layer.msg('请上传升级文件',{icon:2})
                    return ;
                }
                if(myFile!=null){
                    if(crc==null || crc==''){
                        layer.tips('crc校验字符不可为空', '#crc', {
                            tips: [1, 'red'] //还可配置颜色
                        });
                        return;
                    }else {
                        let vv = valiteLength(crc)
                        if(vv!=''){
                            layer.tips(vv, '#crc', {
                                tips: [1, 'red'] //还可配置颜色
                            });
                            return;
                        }
                    }
                }
                if(myFile!=null){
                    formdatas.append("crc", crc);
                    formdatas.append("file", myFile);
                }
                formdatas.append("edIndex", edIndex);
            }else if(type == 1){
                resetName = '新增版本'
                if(myFile==null||myFile==''){
                    layer.msg('请上传文件',{icon:2})
                    return
                }
                if(crc==null || crc==''){
                    layer.tips('crc校验字符不可为空', '#crc', {
                        tips: [1, 'red'] //还可配置颜色
                    });
                    return;
                }else{
                    let vv = valiteLength(crc)
                    if(vv!=''){
                        layer.tips(vv, '#crc', {
                            tips: [1, 'red'] //还可配置颜色
                        });
                        return;
                    }
                }
                formdatas.append("file", myFile);
                formdatas.append("crc", crc);
            }
            passwordIndentityForEd(username, resetName, function () {
                layer.msg('CRC文件校验正确！',{icon:1,time:2000},function () {
                    $.ajax({
                        type: 'post',
                        url: '/pqs9900/version/' + totype + '/',
                        data: formdatas,
                        contentType: false,
                        processData: false,
                        beforeSend: function () {
                            indexi = ityzl_SHOW_LOAD_LAYER();
                        },
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.message, {icon: 1}, function () {
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                    window.parent.refreshs()
                                })
                            } else {
                                layer.msg(data.message, {icon: 2})
                            }
                            ityzl_CLOSE_LOAD_LAYER(indexi);
                        }
                    })
                })




            })
        })



        //执行实例

        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , accept: 'file' //允许上传的文件类型
            , auto: false
            , exts: ''
            , number: 1
            , choose: function (obj) {
                //读取本地文件
                obj.preview(function (index, file, result) {
                    myFile = file;
                    $('#demoText').html('<i class="glyphicon glyphicon-folder-open" style="font-size: 16px;color: #0c7cb5"></i>');
                })


            }
            , before: function (obj) {

            }
            , done: function (res) {
                //上传完毕回调
            }
            , error: function () {
                //请求异常回调
            }
        });


        /*form.verify({
            clength: function (value) {
                var i,sum;
                sum=0;
                for(i=0;i<value.length;i++){
                    if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=36))
                        sum=sum+1;
                    else
                        sum=sum+2;
                }
                if (sum > 36) {
                    return '最多只能36个字符或者18个中文字';
                }
            },
            totype:function (value) {
                if(!censz.test(value)){
                    return '格式错误'
                }
            }
        });*/


    });

    function loadselect(dictypeName,div){
        var part='#'+div;
        $(part).find("option").remove();
        $.ajax({
            method : "post",
            url : "/pqs9900/device/getselect",
            data : {
                dictypeName:dictypeName
            },
            async:false,
            success : function(data) {
                if(data == null){
                    var message = dictypeName + '信息加载失败';
                    layer.msg(message,{icon:1,time:1000});
                }else{
                    var option0 = $("<option selected value=''>"
                        + "全部" + "</option>");
                    $(part).append(option0);
                    $.each(data, function(i, item) {
                        var option = $("<option value='"
                            + item.dicIndex + "'>"
                            + item.dicName + "</option>");
                        $(part).append(option);
                    })
                }
            }
        })
    }


</script>
</body>
</html>
