<%--
  Created by IntelliJ IDEA.
  User: RDCenter
  Date: 4/23/12
  Time: 3:05 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript">
    $(document).ready(function() {

          $(".configFile").each(function(){
              var path=$(this).find("input[class='path']").val();
               //view textArea for file

              var gg=$(this);
                   $.ajax({
                     url: "readFile.action",
                     data:{
                         fileName:path
                     },
                     type:"post",
                     success:function(data) {
//                        $(this).find("textarea")
//                        $(this).find("textarea").val(null);
//                        $("#fileContent").height(500);
                         gg.parent().find("textarea").val(data);
                         //$("#fileContent").show();
                         //set new height to content
                         //  $("#content").css("height", $("#models").height() + $("#modelsParams").height()+150);
                     }
                 });

          });

         $(".configFile").click(function(){
             var path=$(this).find("input[name='path']").val();
              //view textArea for file

             var gg=$(this);
                  $.ajax({
                    url: "readFile.action",
                    data:{
                        fileName:path
                    },
                    type:"post",
                    success:function(data) {
//                        $(this).find("textarea")
//                        $(this).find("textarea").val(null);
//                        $("#fileContent").height(500);
                        gg.parent().find("textarea").val(data);
                        //$("#fileContent").show();
                        //set new height to content
                        //  $("#content").css("height", $("#models").height() + $("#modelsParams").height()+150);
                    }
                });
         });

        $("#save").click(function(){
            var configPathText="";
            var serialNumber=$("input[name='serialNumber']").val();
            var folderName=$("input[name='folderName']").val();
             $(".eachConfig").each(function(){
                    var path=$(this).find("input[name='path']").val();
                    var text=$(this).find("textarea").val();


                    if(configPathText==""){
                        configPathText=path+"@"+text;
                    }else{
                       configPathText=configPathText+"#"+path+"@"+text;
                    }

             });
                  $.ajax({
                    url: "saveConfigFiles.action",
                    data:{
                        configPathText:configPathText,
                        serialNumber:serialNumber,
                        folderName:folderName
                    },
                    type:"post",
                    success:function(data) {

                        location.href="viewConfigs.action?serialNumber="+serialNumber;
//                        $(this).find("textarea")
//                        $(this).find("textarea").val(null);
//                        $("#fileContent").height(500);
                       // gg.parent().find("textarea").val(data);
                        //$("#fileContent").show();
                        //set new height to content
                        //  $("#content").css("height", $("#models").height() + $("#modelsParams").height()+150);
                    }
                });

        });

        $("#back").click(function(){
             var serialNumber=$("input[name='serialNumber']").val();
             var page=$("input[name='page']").val();
             location.href="viewConfigs.action?serialNumber="+serialNumber+"&page="+page;
        });
        $("#upload").click(function(){

            var devI=$(this);
            if(devI.attr("disabled")!="disabled"){
                    devI.attr({disabled:true});
                    var serialNumber=$("input[name='serialNumber']").val();
                    var folderName=$("input[name='folderName']").val();
                    if(confirm("Do you really want to upload this configurations? ")){
                    $.ajax({
                        url: "uploadConfigFiles.action",
                        data:{
                            serialNumber:serialNumber,
                            folderName:folderName
                        },
                        type:"post",
                        success:function(result) {
                           if (result == "input") {
                                alert("Please wait !");
                           } else if (result == "error") {
                                alert("Sorry error occurred.Please try again!");
                           }
                             devI.attr({disabled:false});
                         }
                     });
                    }
            }
        });
       });
 </script>
<style type="text/css">
    .configFile{
        text-align:center;
        font-weight:lighter;
        height:30px;
        color:teal;
        width:240px;
        float: left;
    }
    #save,#back,#upload{
          background: url("images/button/button.png") no-repeat scroll left top transparent;
          height: 27px;
          width: 60px;
    }
</style>
    <s:if test="%{isConfigsNull()}">
        no config file
         <input type="button" name="back" value="Back" style="margin-left:300px;" id="backFromEmpty" />
    </s:if>
    <s:else>
            <div style="display:inline;width:910px;min-height:100px;">
               <s:iterator value="configs"   var="config" status="configStatus">
                    <div style="float:left;width:240px;min-height:100px;margin-left:20px;" class="eachConfig">
                        <%--<a href="#" class="configFile">--%>
                        <div class="configFile">
                            <s:property value="#config.configName" />
                            <input type="hidden" value="<s:property value='#config.configPath'/>" name="path" class="path"/>
                            <%--path=<s:property value='#config.configPath'/>--%>
                        <%--</a>--%>
                        </div>
                        <textarea name="fileContent<s:property value="#configStatus.index" />" rows="20" cols="31" value=""  style="resize:none;float: left;" ></textarea>
                    </div>
                    <%--<div style="float:right;width:300px;min-height:100px;" >--%>
                         <%----%>
                    <%--</div>--%>
               </s:iterator>
            </div>
            <div style="width:700px;float:left;margin-top:50px;">
                 <input type="button" name="back" value="Back" style="margin-left:300px;" id="back" />
                 <input type="button" name="save" value="Save" style="margin-left:50px;" id="save"/>
                 <input type="button" name="upload" value="Upload" style="margin-left:50px;<s:if test="downloadStatus==1">opacity: 0.5;filter: alpha(opacity=50);</s:if>" id="upload" <s:if test="downloadStatus==1"> disabled="disabled" </s:if>/>
            </div>
            <div style="float:left;width: 800px;color: red;font-size: 12px;font-style: italic;margin-top:30px;margin-left: 20px;" >
                If you change the files content with wrong values , save them and  upload then devices will start working not correctly!!!
            </div>

         <%--<div id="fileContent">--%>
            <%--<s:textarea name="fileContent" rows="20" cols="50"/>--%>
         <%--</div>--%>
    </s:else>
 <input type="hidden" value="<s:property value="serialNumber"/>" name="serialNumber" />
 <input type="hidden" value="<s:property value="folderName"/>" name="folderName" />
 <input type="hidden" value="<s:property value="page"/>" name="page"/>


