<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>发文管理管理</title>
	<meta name="decorator" content="default"/>
    <style>
        label.error{
            left:0;
            top:40px;
        }
    </style>
    <script type="text/javascript">
        var validateForm;
        function doSubmit(i){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            if(validateForm.form()){
                if(i==2){
                    $("#inputForm").attr("action","${ctx}/worksendmessage/workSendMessage/tstore");
                }
                $("#inputForm").submit();
                return true;
            }

            return false;
        }
        $(document).ready(function() {
            validateForm = $("#inputForm").validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
            $("#attachment_btn").click(function () {
                $("#attachment_file").click();
            });
        });

        function insertTitle(tValue){
            var list = "${workSendMessage.workAttachments}";
            var size = (list.split('url')).length-1;
            var files = $("#attachment_file")[0].files;            for(var i = 0;i<files.length;i++) {                var file = files[i];
            var attachmentId = "";
            var attachmentFlag = "107";
            console.log(file);
            var timestamp=new Date().getTime();

            var storeAs = "attachment-file/workIncomingMessage/"+timestamp+"/"+file['name'];
            var uploadPath="http://gangwan-app.oss-cn-hangzhou.aliyuncs.com/"+storeAs;/*将这段字符串存到数据库即可*/
            var divId = "_attachment";
            $("#addFile"+divId).show();
            multipartUploadWithSts(storeAs, file,attachmentId,attachmentFlag,uploadPath,divId,size);}
        }

        function changeUser(ids,names,parents) {
            var split = ids.split(',');
            var split2 = names.split(',');
            $("#userTableList").html("");
            userIdx=0;
            for(var i=0;i<split.length;i++){
                var id = split[i];
                if(id==''||id==null){
                    continue;
                }
                var obj = {'id':id,'name':split2[i],'officeName':parents[i]};
                addRow('#userTableList',userIdx,userTpl,obj);
                userIdx+=1;
            }
        }

        function changeOffice(ids,names,parentIds) {
            $("#officeTableList").html("");
            officeIdx=0;
            for(var i=0;i<ids.length;i++){
                var obj = {'id':ids[i],'name':parentIds[i]};
                addRow('#officeTableList',officeIdx,officeTpl,obj);
                officeIdx+=1;
            }
        }
        function getSelectOfficeIds() {
            var selectedIds = "";
            var pidArr = $("#officeTableList tr .officeId");
            for(var i=0;i<pidArr.length;i++){
                selectedIds+=$(pidArr[i]).val();
                selectedIds+=",";
            }
            return selectedIds;
        }
        function getSelectUserIds() {
            var selectedIds = "";
            var pidArr = $("#userTableList tr .userId");
            for(var i=0;i<pidArr.length;i++){
                selectedIds+=$(pidArr[i]).val();
                selectedIds+=",";
            }
            return selectedIds;
        }
        
        function setValuee(obj) {
            if(obj==null){
                return;
            }
            $.ajax({
                type:'post',
                url:'${ctx}/workcommongroup/workCommonGroup/queryDetail',
                data:{
                    "id":obj.id
                },
                success:function (result) {
                    console.log(result);
                    if(result.success){
                        var idArr = $("#userTableList tr .userId");
                        var userList = result.body.userList;
                        for(var i=0;i<userList.length;i++){
                            var user = userList[i];
                            if(user.id==''||hasInArr(user.id,idArr)){
                                continue;
                            }
                            addRow('#userTableList',userIdx,userTpl,user);
                            userIdx+=1;
                        }
                    }
                }
            })
        }

        function hasInArr(id,idArr) {
            for(var i=0;i<idArr.length;i++){
                if(id==$(idArr[i]).val()){
                    return true;
                }
            }
            return false;
        }
    </script>
</head>
<body>
<div class="single-form">
    <div class="container">
		<form:form id="inputForm" modelAttribute="workSendMessage" action="${ctx}/worksendmessage/workSendMessage/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
            <div class="form-group layui-row first">
                <div class="form-group-label"><h2>基本信息</h2></div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label"><span class="require-item">*</span>发文标题：</label>
                    <div class="layui-input-block">
                        <form:input path="title" htmlEscape="false"    class="form-control  layui-input required"/>
                    </div>
                </div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label"><span class="require-item">*</span>发文文号：</label>
                    <div class="layui-input-block">
                        <div class="input-group">
                            <form:input path="num" htmlEscape="false"  readonly="true"  class="form-control  layui-input"/>
                            <span class="input-group-btn">
                                <label class="form-status"><c:choose><c:when test="${not empty workSendMessage.state}">${fns:getDictLabel(workSendMessage.state, 'audit_state', '')}</c:when><c:otherwise>新添</c:otherwise></c:choose></label>
                             </span>
                        </div>
                    </div>
                </div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label"><span class="require-item">*</span>发文类型：</label>
                    <div class="layui-input-block">
                        <form:select path="docType" class="form-control simple-select required">
                            <form:option value="" label=""/>
                            <form:options items="${fns:getMainDictList('doc_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                        </form:select>
                    </div>
                </div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label"><span class="require-item">*</span>发文日期：</label>
                    <div class="layui-input-block">
                        <input type="text" name="createDate" htmlEscape="false" value="<fmt:formatDate value="${workSendMessage.createDate}" pattern="yyyy-MM-dd"/>" readonly="readonly" class="form-control  layui-input required"/>
                    </div>
                </div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label"><span class="require-item">*</span>拟 稿 人：</label>
                    <div class="layui-input-block">
                        <form:input path="person" htmlEscape="false" class="form-control  layui-input required"/>
                    </div>
                </div>
                <div class="layui-item layui-col-sm6">
                    <label class="layui-form-label">所属部门：</label>
                    <div class="layui-input-block">
                        <input type="text" name="office.name" readonly="readonly" class="form-control layui-input"  value="${workSendMessage.office.topCompany}"/>
                        <input type="hidden" name="office.id" readonly="readonly" class="form-control layui-input"  value="${workSendMessage.office.id}"/>
                    </div>
                </div>
                <div class="layui-item layui-col-sm12 with-textarea">
                    <label class="layui-form-label">发文摘要：</label>
                    <div class="layui-input-block">
                        <form:textarea path="summary" htmlEscape="false" rows="3" maxlength="255" class="form-control"/>
                    </div>
                </div>
                <div class="layui-item layui-col-sm12 with-textarea">
                    <label class="layui-form-label">备    注：</label>
                    <div class="layui-input-block">
                        <form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="255" class="form-control"/>
                    </div>
                </div>
            </div>
            <div class="form-group layui-row">
                <div class="form-group-label"><h2>发文范围-部门</h2></div>
                <div class="layui-item nav-btns">
                <sys:treeselectoffices id="offices" name="" value="" labelName="memberNameStr" labelValue=""
                                       retnParent="true" checked="true" notAllowSelectParent="true" title="部门" url="/sys/office/treeData?type=2"  cssClass="form-control required" allowClear="true"/>
                </div>
                <table id="officeTable" class="table table-bordered table-condensed details">
                <thead>
                <tr>
                    <th class="hide"></th>
                    <th>部门</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="officeTableList">
                </tbody>
                </table>
                <script type="text/template" id="officeTpl">//<!--
                    <tr id="officeList{{idx}}">
                        <td class="hide">
                            <input id="officeList{{idx}}_id" name="officeList[{{idx}}].id" readonly="true" value="{{row.id}}" type="hidden" class="form-control officeId"/>
                        </td>
                        <td>
                            {{row.name}}
                        </td>
                        <td class="text-center op-td">
                                    <a href=javascript:void(0); onclick="delRow(this, '#officeList{{idx}}')"   class="op-btn op-btn-delete"><i class="fa fa-trash"></i> 取消</a>
                        </td>
                    </tr>//-->
                </script>
            </div>
            <div class="form-group layui-row">
                <div class="form-group-label"><h2>发文范围-成员</h2></div>
                <div class="layui-item nav-btns">
                    <sys:treeselectusers id="users" name="" value="" labelName="memberNameStr" labelValue=""
                                         retnParent="true" title="用户" url="/sys/office/treeData?type=3" checked="true" cssClass="form-control required" allowClear="true" notAllowSelectParent="true"/>
                    <sys:gridselectCommonGroup url="${ctx}/workcommongroup/workCommonGroup/selectList" id="baseData" title="选择常用组"
                                            cssClass="form-control" fieldLabels="" fieldKeys=""  searchLabel="常用组名称" searchKey="name"></sys:gridselectCommonGroup>
                </div>
                <table id="userTable" class="table table-bordered table-condensed details">
                    <thead>
                    <tr>
                        <th class="hide"></th>
                        <th>姓名</th>
                        <th>部门</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="userTableList">
                    </tbody>
                </table>
                <script type="text/template" id="userTpl">//<!--
                    <tr id="userList{{idx}}">
                        <td class="hide">
                            <input id="userList{{idx}}_id" name="userList[{{idx}}].id" readonly="true" value="{{row.id}}" type="hidden" class="form-control userId"/>
                        </td>
                        <td>
                        {{row.name}}
                        </td>
                        <td>
                        {{row.officeName}}
                        </td>
                        <td class="text-center op-td">
                                    <a href=javascript:void(0); onclick="delRow(this, '#userList{{idx}}')"   class="op-btn op-btn-delete"><i class="fa fa-trash"></i> 取消</a>
                        </td>
                    </tr>//-->
                </script>
                <script type="text/javascript">
                    var userIdx = 0, userTpl = $("#userTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                    var officeIdx = 0, officeTpl = $("#officeTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");

                    $(document).ready(function() {
                        var data = ${fns:toJson(workSendMessage.officeList)};
                        if (data!=null) {
                            for (var i = 0; i < data.length; i++) {
                                addRow('#officeTableList', officeIdx, officeTpl, data[i]);
                                officeIdx = officeIdx + 1;
                            }
                        }
                        var dataBank = ${fns:toJson(workSendMessage.userList)};
                        if (dataBank!=null) {
                            for (var i = 0; i < dataBank.length; i++) {
                                addRow('#userTableList', userIdx, userTpl, dataBank[i]);
                                userIdx = userIdx + 1;
                            }
                        }
                    });

                    function addRow(list, idx, tpl, row){
                        var idx1 = 0;
                        if('#userTableList'==list){
                            idx1 = $("#userTableList tr").length
                        }else if('#officeTableList'==list){
                            idx1 = $("#officeTableList tr").length
                        }
                        bornTemplete(list, idx, tpl, row, idx1);
                    }
                    function bornTemplete(list, idx, tpl, row, idx1){
                        $(list).append(Mustache.render(tpl, {
                            idx: idx, delBtn: true, row: row,
                            order:idx1 + 1, idx1:idx1
                        }));
                        $(list+idx).find("select").each(function(){
                            $(this).val($(this).attr("data-value"));
                        });
                        $(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
                            var ss = $(this).attr("data-value").split(',');
                            for (var i=0; i<ss.length; i++){
                                if($(this).val() == ss[i]){
                                    $(this).attr("checked","checked");
                                }
                            }
                        });
                    }
                    function delRow(obj, prefix,idx){
                        $(obj).parent().parent().remove();
                    }
                </script>
            </div>
            <div class="form-group layui-row">
                <div class="form-group-label"><h2>附件信息</h2></div>
                <div class="layui-item nav-btns">
                    <a id="attachment_btn" class="nav-btn nav-btn-add" title="添加附件"><i class="fa fa-plus"></i>&nbsp;添加附件</a>
                </div>
                <div id="addFile_attachment" style="display: none" class="upload-progress">
                    <span id="fileName_attachment" ></span>
                    <b><span id="baifenbi_attachment" ></span></b>
                    <div class="progress">
                        <div id="jindutiao_attachment" class="progress-bar" style="width: 0%" aria-valuenow="0">
                        </div>
                    </div>
                </div>
                <input id="attachment_file" type="file" name="attachment_file" multiple="multiple" style="display: none;" onChange="if(this.value)insertTitle(this.value);"/>
                <span id="attachment_title"></span>
                <div class="layui-item layui-col-xs12" style="padding:0 16px;">
                    <table id="upTable" class="table table-bordered table-condensed details">
                        <thead>
                        <tr>
                                <%-- <th>序号</th>--%>
                            <th>文件预览</th>
                            <th>上传人</th>
                            <th>上传时间</th>
                            <th width="150px">操作</th>
                        </tr>
                        </thead>
                        <tbody id="file_attachment">
                        <c:forEach items="${workSendMessage.workAttachments}" var = "workClientAttachment" varStatus="status">
                            <tr>
                                    <%-- <td>${status.index + 1}</td>--%>
                                <c:choose>
                                    <c:when test="${fn:containsIgnoreCase(workClientAttachment.attachmentName,'jpg')
                                                           or fn:containsIgnoreCase(workClientAttachment.attachmentName,'png')
                                                           or fn:containsIgnoreCase(workClientAttachment.attachmentName,'gif')
                                                           or fn:containsIgnoreCase(workClientAttachment.attachmentName,'bmp')
                                                           or fn:containsIgnoreCase(workClientAttachment.attachmentName,'jpeg')}">
                                        <td><img src="${workClientAttachment.url}" width="50" height="50" onclick="openDialogView('预览','${ctx}/sys/picturepreview/picturePreview?url=${workClientAttachment.url}','90%','90%')" alt="${workClientAttachment.attachmentName}"></td>
                                    </c:when>
                                    <c:otherwise>
                                        <c:choose>
                                            <c:when test="${fn:containsIgnoreCase(workClientAttachment.attachmentName,'pdf')}">
                                                <td><a class="attention-info" href="javascript:void(0)" onclick="preview('预览','${workClientAttachment.url}','90%','90%','1')">${workClientAttachment.attachmentName}</a></td>
                                            </c:when>
                                            <c:otherwise>
                                                <td><a class="attention-info" href="javascript:void(0)" onclick="preview('预览','${workClientAttachment.url}','90%','90%')">${workClientAttachment.attachmentName}</a></td>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:otherwise>
                                </c:choose>
                                <td>${workClientAttachment.createBy.name}</td>
                                <td><fmt:formatDate value="${workClientAttachment.createDate}" type="both"/></td>
                                <td class="op-td">
                                    <div class="op-btn-box" >
                                        <a href="javascript:location.href='${ctx}/workfullmanage/workFullManage/downLoadAttach?file='+encodeURIComponent('${workClientAttachment.url}');" class="op-btn op-btn-download"><i class="fa fa-download"></i>&nbsp;下载</a>

                                        <c:if test="${workClientAttachment.createBy.id eq fns:getUser().id}">
                                            <a href="javascript:void(0)" onclick="deleteFileFromAliyun(this,'${ctx}/sys/workattachment/deleteFileFromAliyun?url=${workClientAttachment.url}&id=${workClientAttachment.id}&type=2','addFile')" class="op-btn op-btn-delete" ><i class="fa fa-trash"></i>&nbsp;删除</a>
                                        </c:if>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
	</form:form>
    </div>
</div>
</body>
</html>