<%@ Page Language="C#" ValidateRequest="false" MasterPageFile="~/Admin/MasterPage.master" AutoEventWireup="true" Inherits="Kesion.NET.WebSite.Admin_YunMarket" Title="应用市场" Codebehind="KS.YunMarket.aspx.cs" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="System.Xml" %>
<asp:Content ID="Content1" ContentPlaceHolderID="KSContent" runat="Server">
<div id="manage_top" class="menu_top_fixed">
    <ul class="titleTab">
        <!--<li><strong>管理操作：</strong></li>-->
        <li<%=!KSCMS.S("action").Equals("myapp")?"  class='curr'":"" %>><a href="KS.YunMarket.aspx">应用市场</a></li>
        <li<%=KSCMS.S("action").Equals("myapp")?"  class='curr'":"" %>><a href="KS.YunMarket.aspx?action=myapp">我的应用</a></li>
    </ul>
</div>  
<div class="menu_top_fixed_height"></div> 
<script>
    function InstallApp(appId) {
		    location.href='<%=KSCMS.GetInstallDir()+MyCache.GetCacheConfig(4)%>/Yun/KS.YunMarket.aspx?action=InstallApp&appid='+appId;
    }
    function ToMyApp() {
		    location.href='<%=KSCMS.GetInstallDir()+MyCache.GetCacheConfig(4)%>/Yun/KS.YunMarket.aspx?action=myapp';
    }
    function AppDownLoad(appId) {
        var price = $("#price" + appId).html();
        var downUrl="<%=KSCMS.GetInstallDir()+MyCache.GetCacheConfig(4)%>/Yun/KS.YunMarket.aspx?action=downapp&appId=" + appId;
        if (price!=undefined&&parseFloat(price) > 0) {
             top.$.dialog.confirm('收费插件，确认付费购买下载吗？',function(){location.href = downUrl;},function(){});
        } else {
            location.href = downUrl;
        }
     }
    function ViewPics(id){
            top.initialPrettyPhotoPicStr($("#p"+id).html());
    }
     function SetAppStatus(appId, v) {
		    location.href='<%=KSCMS.GetInstallDir()+MyCache.GetCacheConfig(4)%>/Yun/KS.YunMarket.aspx?action=AppStatus&v='+v+'&appid='+appId;
          }
          
     function DeleteApp(appId) {
             top.$.dialog.confirm('是否卸载应用插件?',function(){location.href='<%=KSCMS.GetInstallDir()+MyCache.GetCacheConfig(4)%>/Yun/KS.YunMarket.aspx?action=DeleteApp&appid='+appId;},function(){});
    }
</script>


<asp:Panel ID="MarketListPanel" runat="server" Width="100%">
<style>
@media all and (max-width: 1255px) {
	.stencil ul li{ width:50%;}	
}
</style>
<div class="content-area">
            <asp:Repeater ID="YunTemplateList" runat="server">
                <HeaderTemplate>
                	<div class="myTitle noBorder-noP">精选</div>
                    <div class="stencil clearfix">
                        <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li title="应用产品：<%# Eval("classname") %> 开发商：<%# Eval("Author") %> 版本号：<%# Eval("version") %> 上传时间：<%# Eval("adddate") %>">
                        <div class="border">
                            <div class="s_img">
                                <img src="<%# GetYunTemplatePic(Convert.ToString(Eval("defaultPic"))) %>" onerror="this.src='/sysimg/nopic.gif';" /></div>
                            <div class="s_txt">
                                <div class="s_name" style="cursor:pointer" onclick="location.href='?action=viewapp&appid=<%# Eval("appid") %>'">
                                    <div class="s_titleBox"><%# Eval("appName") %>
                                    <!--<%# Eval("isvip").ToString()=="1"?"<i class='vip normal'><i class='iconfont guan'>&#xe604;</i></class>":string.Empty %>-->
                                    <%# Utils.StrToFloat(Eval("isvip"))>0?"<span class='vip_priceBox'><i>VIP专属</i></span>":"" %>
                                    </div>
                                    <div class="Tid break">
                                        <span class="systemColor"><%# Eval("APPEname") %> <%# isAppStatus(Utils.StrToInt(Eval("AppID").ToString()))==1?"<font class='redFont'>(已安装)</font>":"" %></span>
                                        <span>应用产品：<%# Eval("classname") %> </span>
                                        <span style="display:none;">上传时间：<%# Eval("adddate") %></span>
                                    </div>
                                </div>
                            </div>

                            <div class="s_rbox">
                                <span id="p<%#Eval("AppID") %>" style="display: none"><%# Eval("pics") %></span>
                                <a href='javascript:ViewPics("<%#Eval("AppID") %>")' title="预览" style="display:none;"><i class="iconfont icon">&#xe647;</i>预览</a>

                                <%# isAppStatus(Utils.StrToInt(Eval("AppID").ToString()))==1?"":@"<a href=""javascript:;"" onclick='AppDownLoad("+ Eval("AppId")+@")'>下载应用</a>" %>
                                 
                                <%# Utils.StrToFloat(Eval("price"))>0?"<span class='s_priceBox'>￥<font id='price"+Eval("AppId")+"'>"+Kesion.APPCode.Public.ReturnShopPrice(Eval("price"))+"</font></span>":"" %>
                            </div>
                        </div>
                    </li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>    
                </div>
              </table>
                </FooterTemplate>
            </asp:Repeater>
            <div class="empty" style="text-align: center;" id="showEmpty" runat="server" visible="false">
                云端没有发现可供下载的应用插件!
            </div>
            <asp:Label ID="LabPage" runat="server"></asp:Label>
</div>
</asp:Panel>

<asp:Panel ID="AppViewPanel" Visible="false" runat="server" Width="100%">
<div class="content-area">
    <div class="applicationBox clearfix">
        <div class="fl AppImgBox">
            <img src="<%= GetYunTemplatePic(dr["defaultPic"].ToString()) %>" />
        </div>  
        <div class="fl AppIntro">
            <h3><%=dr["AppName"].ToString() %><span class="appPrice redFont">
                <%=Utils.StrToFloat(dr["price"].ToString())>0?"￥"+Kesion.APPCode.Public.ReturnShopPrice(dr["price"].ToString()):"免费"%>
                <%=Utils.StrToFloat(dr["isvip"].ToString())>0?"<i>VIP专属</i>":""%>
                </span></h3>
            <div class="Tid break">
                <span class="systemColor greenFont"><%=dr["AppEName"].ToString() %></span>
                <span class="systemintro"><%=dr["Intro"].ToString() %></span>
            </div>
            <div class="AppBtnBox">
                <%=isAppStatus(Utils.StrToInt(dr["AppID"].ToString()))==1?@"<a href=""javascript:;"" class=""button button_on"">已安装</a>":@"<a href=""javascript:;"" class=""button button_on"" onclick='AppDownLoad("+ dr["AppId"].ToString()+@")'>下载应用</a>"  %>
                <a href="KS.YunMarket.aspx" class="button">返回</a>
            </div>
        </div>
    </div>
</div>
<div class="content-area mt20">
	<div class="myTitle">插件详情</div>
    <div class="Appdetails">
        <table class="yun-bindBox">
              <tr>
                <td class="yun-bindTit" width="120" align="left">版本号</td>
                <td class="grayFont">
                  V <%=dr["version"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">适用主程序版本</td>
                <td class="grayFont">
                 V  <%=dr["allowversion"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">适用产品</td>
                <td class="grayFont">
                 <%=dr["product"].ToString()=="common"?"通用":dr["product"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">更新日期</td>
                <td class="grayFont">
                    <%=dr["adddate"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">插件Id</td>
                <td class="grayFont">
                   <%=dr["appid"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">开发商</td>
                <td class="grayFont">
                   <%=dr["author"].ToString() %>
                </td>
            </tr>
            <tr>
                <td class="yun-bindTit" width="120" align="left">标签</td>
                <td class="grayFont">
                    Tag1 Tag2
                </td>
            </tr>
             <tr>
                <td class="yun-bindTit" width="120" align="left">插件介绍</td>
                <td class="grayFont">
                   <%=dr["content"].ToString() %>
                </td>
            </tr>
        </table>
    </div>
</div>
</asp:Panel> 


<asp:Panel ID="YunDownPanel" Visible="false" runat="server" Width="100%">
<div class="content-area">
        <div id="downstep1" style="min-height:150px;font-size: 20px; margin: 20px; text-align: center; padding-top: 60px; font-weight: bold; color: green;">
            <img src="../../sysimg/default/loading.gif" />
            <span id="processTips">正在尝试连接云端服务器并初始化下载数据，请稍候......</span>
        </div>
        <div id="downstep2" style="display: none; min-height:150px;margin: 40px; text-align: center;">
            <div style="font-size: 16px; height: 35px; line-height: 35px; font-weight: bold" id="per">下载进度：0%</div>
            <div style="margin: 0 auto; width: 600px; border: 1px solid #eac180; background: #fff; height: 20px">
                <div id="bar" style="width: 0px; height: 20px; background: #f7e4a2"></div>
            </div>
            <div style="height: 40px; font-size: 16px; line-height: 40px">
                插件安装包大小：<span id="totalSize" style="color: brown; font-weight: bold">0</span> 当前已下载：<span id="currSize" style="color: brown; font-weight: bold">0</span>
            </div>
        </div>

        <div class="pop-button" style="text-align: center; display: none" id="downstep3">
             download success!
        </div>
</div>
        <script>
           function Start() {
               $("#downstep2").show();
               $("#downstep1").hide();
            }
            function SetProcess(data) {
                $("#downstep2").show();
                $("#downstep1").hide();
                var arr = data.split('|');
                var total = "";
                var hasdown = "";
                if (arr[0] / 1024 > 1024) {
                    total = (arr[0] / 1024 / 1024).toFixed(2) + " M";
                } else {
                   total = (arr[0] / 1024).toFixed(0) + " KB";
                }
                if (arr[1] / 1024 > 1024) {
                    hasdown = (arr[1] / 1024 / 1024).toFixed(2) + " M";
                } else {
                    hasdown = (arr[1] / 1024).toFixed(0) + " KB";
                }
                $("#totalSize").html(total);
                $("#currSize").html(hasdown);
                var w = parseFloat(arr[1]) / parseFloat(arr[0]);
                if (w < 1) {
                    $("#per").html("下载进度：" + (w * 100).toFixed(2) + " %");
                } else {
                    $("#per").html("任务完成：100 %");
                }
                $("#bar").html('').width(w * 600);
            }
            function processTips(str) {
                $("#processTips").html(str);
            }
            function End(appId) {
                $('#downstep3').html('<input type="button" onclick="InstallApp(' + appId+'); " value=" 立即安装 " class="button" /> <input type="button" value=" 暂不安装 " class="button" onclick="ToMyApp();" />').show();
                $("#per").html("任务完成：100 %");

            }
        </script>
        <%
            if (KSCMS.S("action").Equals("downapp"))
            {
                InitialDownApp();
            }
            %>
 </asp:Panel>





<asp:Panel ID="MyAppPanel" Visible="false" runat="server" Width="100%">
<div class="content-area">
      <div class="myTitle">
          <span class="systemBox fr mr20"><!--<a href="" class="button">筛选</a>--><a href="KS.YunMarket.aspx" class="button downloadSystem">下载应用</a></span>
          我的应用</div>
         <table class="CTable" cellspacing="0" border="0"  style="color:Black;width:100%;border-collapse:collapse;">
				<tr class="CTitle">
					<th scope="col" style="width:60px">序号</th>
                    <th scope="col" style="text-align:center;width:100px;">应用图标</th>
                    <th scope="col" style="text-align:left;width:150px;">应用名称</th>
                    <th scope="col" style="text-align:left">应用介绍</th>
                    <th scope="col" style="width:180px;">开发商</th>
                    <th scope="col" style="width:160px;text-align:left;">版本信息</th>
                    <th scope="col" style="width:60px;text-align:center;">应用状态</th>
                    <th scope="col" style="width:160px;text-align:center;">管理操作</th>
                </tr>
              	<%
                    XmlDocument yunVersionXML = initialYunAPPVersionXML();

                    using(var rd=DataFactory.ExecuteReader("SELECT * From KS_YunApp Order by appid desc"))
                    {
                        StringBuilder sb = new StringBuilder();
                        if (!rd.HasRows)
                        {
                            sb.AppendLine(string.Format("<tr><td colspan='10' align='center' class=\"Ctd\">{0}</td></tr>", "无可用应用！"));
                        }
                        else
                        {
                            while (rd.Read())
                            {
                                bool isOpen = true;
                                bool isInstall = false;
                                if (rd["status"].ToString() == "0")
                                {
                                    isOpen = false;
                                }
                                if (rd["IsInstall"].ToString()=="1" && System.IO.Directory.Exists(Utils.GetMapPath("~/Plus/" + rd["AppEname"].ToString())))
                                {
                                    isInstall = true;
                                }

                                int appId = Utils.StrToInt(rd["appid"].ToString());
                                int yunAppId=Utils.StrToInt(rd["YunAppID"].ToString());
                                string yunVersion = "-";
                                if (yunVersionXML != null)
                                {
                                    XmlNode xmlNode = yunVersionXML.SelectSingleNode("root");
                                    if (xmlNode != null)
                                    {
                                        XmlElement xe = (XmlElement)xmlNode.SelectSingleNode("item[@appid=" + yunAppId.ToString() + "]");
                                        if (xe != null)
                                        {
                                            yunVersion = xe.SelectSingleNode("@version").InnerText;
                                        }
                                    }
                                }

                                string version = rd["version"].ToString();
                                sb.AppendLine(@"<tr onmouseover=""this.className=&#39;CtrMouseOver&#39;"" onmouseout=""this.className=&#39;CtrMouseOut&#39;"" onclick=""chk(this)"">");
                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""center"">{0}</td>", appId.ToString()));
                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""center""><img src='{0}' width='60' height='60'/></td>", rd["ico"].ToString()));
                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""left"">{0}<div class='tips'>{1}</a></td>", rd["AppName"].ToString(),rd["appEname"].ToString()));
                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""left"">{0}</td>", rd["Descript"].ToString()));
                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""center"">{0}</td>", rd["author"].ToString()));

                                bool hasNewVersion = false;
                                if (isInstall && yunVersion!="-" && Utils.StrToFloat(yunVersion) > Utils.StrToFloat(version))
                                {
                                    hasNewVersion = true;
                                }

                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""left""{2}>当前版本：{0}<br/><font class='greenFont'>云端版本：{1}</font></td>", version,yunVersion,hasNewVersion?" style='color:red'":""));

                                sb.AppendLine(string.Format(@"<td class=""Ctd"" align=""center"">{0}</td>", isInstall==false?"<span class='noinstall'>未安装</span>":(isOpen?@"<a class=""ok""  title=""正常"" href=""javascript:void(0)"" onclick=""SetAppStatus("+appId+@",0)""></a>":@"<a class=""no"" title=""关闭"" href=""javascript:void(0)"" onclick=""SetAppStatus("+appId+@",1)""></a>")));
                                sb.AppendLine(@"<td class=""Ctd link-operate""  align=""center"">");

                                if (isInstall)  //已安装
                                {
                                    if (hasNewVersion)
                                    {
                                        sb.Append(@"<a onclick=""AppDownLoad("+yunAppId+@");"" class='operat-a update greenFont'>升级应用</a> ");
                                    }
                                    sb.Append(@"<a onclick=""DeleteApp("+appId+@");"" class='operat-a uninstall greenFont'>卸载应用</a>");
                                }
                                else
                                {
                                    sb.Append(@"<a onclick=""InstallApp("+appId+@");"" class='operat-a install greenFont'>安装应用</a>");
                                }

                                sb.AppendLine(@"</td>
                         </tr>");


                            }
                        }
                        Response.Write(sb.ToString());
                    }


               %>
        </table>
</div>
</asp:Panel>            



</asp:Content>
