<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ include file="/WEB-INF/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link rel="stylesheet" href="/static/weixin/activity/css/emperor.css">
    <link rel="stylesheet" href="/static/weixin/activity/css/public.css">
    <link rel="stylesheet" href="/static/weixin/activity/css/SG_area_select.css?v=1.0.6">
    <link rel="stylesheet" href="/static/weixin/activity/css/mobiscroll/mobiscroll.css">
    <link rel="stylesheet" href="/static/weixin/activity/css/mobiscroll/mobiscroll_002.css">
    <link rel="stylesheet" href="/static/weixin/activity/css/mobiscroll/mobiscroll_003.css">
    <link rel="stylesheet" href="/static/weixin/css/all.base.css">
    <script src="/static/weixin/activity/js/flexible.js"></script>
    <style>
        .specific-address .select-sdf button.cancel {
            background: lightgray;
        }
    </style>
    <title>新增展会活动</title>
</head>
<body id="contain">
<div class="contain">
    <div class="bai-head">
        <span>新增展会活动</span>
        <a id="back" class="left">
            <img src="/static/weixin/activity/images/head-left.png"/>
        </a>
    </div>
    <div class="zeng-exhibition">
        <h2>基础信息</h2>
        <form id="activityForm" method="post">


        <ul class="exhibition-cont data">
            <li>
                <span>展会活动名称</span>
                <input id="activityName" placeholder="请填写展会活动的名称"  name="activityName" class="input-text wed valid_activityName myValid">
            </li>
            <li>
                <span>开始时间</span>
                <input value="" class="input-text input-tiao"   name="startTime"  readonly="readonly"  type="text" id="start-time" onchange="wert()"/>
            </li>
            <li>
                <span>结束时间</span>
                <input value="" class="input-text input-tiao"  name="endTime" readonly="readonly"  type="text" id="end-time" />
            </li>
            <li>
                <span>所在城市</span>
                <p id="city"></p>
<%--
                <input value="" class="input-text input-tiao"  name="" rea donly="readonly"  type="text" id="city" />
--%>
                <input type="hidden" name="province" value="" id="s-province">
                <input type="hidden" name="city" value="" id="s-city">
                <input type="hidden" name="district" value="" id="s-area">
            </li>
            <li class="city-address">
                <span>具体地址</span>
                <p id="specific"></p>
                <input type="text" name="address" id="ju-address" style="display: none">
            </li>
            <li>
                <%-- 跳转到选择展厅展位页面 已设置展位0个 --%>
                <span>展厅展位</span>
                <input id="exhibitionPosCnt" readonly type="text"  placeholder=""  value="未设置" class="input-text input-tiao">
            </li>
        </ul>
        </form>
    </div>
</div>

<div class="pro-steps assigned-steps">
    <div class="steps">
        <button class="xia" type="button" onclick="tijiao()">确认</button>
        <button>返回</button>
    </div>
</div>

<!--具体地址填写弹窗-->
<div class="specific-address">
    <textarea></textarea>
    <div class="select-sdf">
        <button style=" background-color: lightgray !important;" class="cancel">取消</button>
        <button class="bao">保存</button>
    </div>
</div>

<!--黑色蒙版-->
<div class="block-mask"></div>

<script src="/static/weixin/activity/js/jquery-1.8.3.min.js"></script>

<script src="/static/weixin/activity/js/iscroll.js"></script>
<script src="${ctxwxsta}/js/SG_area_select.js"></script>
<script src="/static/weixin/activity/js/emperor.js"></script>
<script src="/static/weixin/activity/js/mobiscroll/mobiscroll_002.js"></script>
<script src="/static/weixin/activity/js/mobiscroll/mobiscroll_004.js"></script>
<script src="/static/weixin/activity/js/mobiscroll/mobiscroll.js"></script>
<script src="/static/weixin/activity/js/mobiscroll/mobiscroll_003.js"></script>
<script src="/static/weixin/activity/js/mobiscroll/mobiscroll_005.js"></script>
<script src="/static/weixin/common/layer_mobile/layer.js"></script>
<script src="/static/weixin/js/jquery.formHelper.js"></script>
<script src="/static/weixin/js/moment.js"></script>
<script src="/static/weixin/js/g.js"></script>
<link rel="stylesheet" href="/static/weixin/common/layer_mobile/need/right.css">
<script>

</script>
<script type="text/javascript">
    /*var allright=true;
    function isWrong(el,obj, msg){
        if(el){
            $(obj).nextAll("b").remove();
            if(msg!="") $(obj).after("<b style='display:block'>"+msg+"</b>");
            allright=false;
            return false;
        }else{
            $(obj).nextAll("b").remove();
            return true;
        }
    }
    function clearErr(obj){
        $(obj).nextAll("b").remove();
    }

    $(".valid_activityName").bind("blur keyup",function(){ //投保人名称
        var r1 = isWrong($(this).val().trim()=="",this,"必填项不可为空"); if(!r1) return r1;
        var r2 = isWrong(!/^[\u4e00-\u9fa5a-zA-Z]{2,24}$/.test($(this).val()),this,"要求2-24个中英文字母");
    });*/

    function wert() {
        // $('#end-time').val('');
    }

    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset: 'date'};
    opt.datetime = {preset: 'datetime'};
    opt.time = {preset: 'time'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'bottom', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 10, //开始年份
        endYear: currYear + 10, //结束年份

        onSelect: function (valueText, inst) {
            var id = $(this)[0].id;
            var validity = valueText.split("-");
            if (id === "start-time") {
                if (opt.default.maxDate) {
                    opt.default.maxDate = null;
                }
                opt.default.minDate = new Date(validity[0], +validity[1] - 1, +validity[2] );
                $("#end-time").mobiscroll($.extend(opt['date'], opt['default']));
            }
        }

    };
    $(function () {
        $("#start-time").mobiscroll($.extend(opt['date'], opt['default']));
        var optDateTime = $.extend(opt['datetime'], opt['default']);
        var optTime = $.extend(opt['time'], opt['default']);
    });

    $('#city').on('click', function () {
        $.areaSelect();
        $('.title-min .query').on('click', function () {
            // $('#city').text('');
            $(".popups-box").show().animate({bottom: '-251px'}, function () {
                $('.sg-mask').css('display', 'none');
                $('.sg-mask').remove();
            });
        })

        /*移动端确定操作响应*/
        $('.title-min .submit').on('click', function () {
            var test = $('#hasCheck').val();
            if(test.split("-").length!=3){
                layer.open({content:"必须选择省市县三级",btn: '我知道了'});
                return;
            }
            $('#city').text($('#hasCheck').val());
            $(".popups-box").show().animate({bottom: '-251px'}, function () {
                $('.sg-mask').css('display', 'none');
                $('.sg-mask').remove();
            });
        })

    });

    function initExhibition(){
        //初始化表单
        if(sessionStorage.getItem("add_activity_info")!=null){
            var info = JSON.parse(sessionStorage.getItem("add_activity_info"))
            $("#activityForm").setForm(info);
            if(info.startTime){
                var validity = info.startTime.split("-");
                opt.default.minDate = new Date(validity[0], +validity[1] - 1, +validity[2] );
                $("#start-time").mobiscroll($.extend(opt['date'], opt['default']));
                $("#end-time").mobiscroll($.extend(opt['date'], opt['default']));
            }
            if(info.endTime && info.startTime){
                var validity = info.startTime.split("-");
                opt.default.minDate = new Date(validity[0], +validity[1] - 1, +validity[2]);
                $("#end-time").mobiscroll($.extend(opt['date'], opt['default']));
            }
            try{
                $("#city").html(g_getRegion($("#s-province").val()).province + " "+ g_getRegion($("#s-city").val()).citytype + " "+ g_getRegion($("#s-area").val()).city);
                $("#specific").html(info.address);
            }catch (e) {
                console.log(e);
            }
        }

        if(sessionStorage.getItem("add_exhibitions")!=null){
            var json  = JSON.parse(sessionStorage.getItem("add_exhibitions"));
            //获得展位数量
            var idx = 0;
            for(var i in json){
                idx += json[i].length;
            }
            $("#exhibitionPosCnt").val("已设置展位"+idx+"个");
        }

    }(initExhibition());

    $("#exhibitionPosCnt").on('click',function(){
        //缓存当前页面的值
        var p  = $("#activityForm").serializeJson();
        sessionStorage.setItem("add_activity_info",JSON.stringify(p));

        window.location.href = '/weixin/exhibition/exhibition-add-view';
    });


    $("#back").click(function(){
        layer.open({
            content: '新增的内容未保存，确定要退出吗？'
            ,btn: ['确定', '取消']
            ,yes: function(index){
                layer.close(index);
                window.location.href="/weixin/activity/activityListOnline";
            }
        });
    })

    function  tijiao(){
        var p  = $("#activityForm").serializeJson();
//        console.log(p);
        if($.trim(p["activityName"])=="") {layer.open({content:"展会活动名称不能为空",btn: '我知道了'}); return;}
        //if(!/^[\u4e00-\u9fa5A-Za-z0-9]{1,50}$/.test($.trim(p["activityName"]))) {layer.open({content:"展会活动名称只能是1-50中英文数字",btn: '我知道了'}); return;}
        if($.trim(p["startTime"])=="") {layer.open({content:"开始时间不能为空",btn: '我知道了'}); return;}
        if($.trim(p["endTime"])=="") {layer.open({content:"结束时间不能为空",btn: '我知道了'}); return;}
        if($.trim(p["district"])=="") {layer.open({content:"所在城市不能为空",btn: '我知道了'}); return;}
        if($.trim(p["address"])=="") {layer.open({content:"具体地址不能为空",btn: '我知道了'}); return;}
        // if($.trim(p["address"])=="" ||$.trim(p["address"]).length>50||$.trim(p["address"]).length<5 ) {
        //     layer.open({content:"具体地址不能为空,5-50个长度",btn: '我知道了'}); return;
        // }
        // if(!/^[\u4e00-\u9fa5A-Za-z0-9]{5,50}$/.test($.trim(p["address"]))) {layer.open({content:"具体地址只能是5-50中英文数字",btn: '我知道了'}); return;}

        //活动不能超过15天
        //活动开始时间不能比今天小
        var startTime = moment($.trim(p["startTime"]),"YYYY-MM-DD");
        var endTime = moment($.trim(p["endTime"]),"YYYY-MM-DD");
        console.log(startTime.diff(moment(new Date()))+">>>>>>>>>>>>")
        // if(startTime.diff(moment(new Date()),"days")<=-1){
        //     layer.open({content:"活动开始时间要从今天起",btn: '我知道了'});
        //     return;
        // }
        if(endTime.diff(startTime,'days')>15){
            layer.open({content:"活动最长等于15天",btn: '我知道了'});
            return;
        }

//        allright=true;
//        $(".myValid").each(function(){ $(this).trigger("blur"); $(this).trigger("change");    }); //校验表单
//        console.log(allright);
//        if(allright){
        //制造参数
        var json  = JSON.parse(sessionStorage.getItem("add_exhibitions"));
        var ex = [];
        for(var i in json){
            var str = "",obj={};
            for(var j in json[i]){
                str += json[i][j]+",";
            }
            str = str.substr(0,str.length-1);
            obj['exhibition_hall_name']=i;
            obj['exhibition_hall_pos_names']=str;
            ex.push(obj);
        }
        var params = $("#activityForm").serialize();
        params+="&exhibitions="+JSON.stringify(ex);

        if(ex.length==0) {layer.open({content:"展位不能为空",btn: '我知道了'}); return;}
       $('.block-mask').show();
        // layer.open({type:2})
          $.ajax({
              type: 'POST',
              url: "/weixin/activity/saveActivity",
              data:params,
              dataType:"json",
              async: true,
              success: function(data){
                  // console.log(data.msg);
                  if(data.status){
                      window.location.href="/weixin/activity/activityDetatil?id="+data.id;
                  }else{
                      // layer.closeAll();
                      $('.block-mask').hide();
                      layer.open({content:"操作失败",btn: '我知道了、'});
                  }

              }
          });


//        }



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