using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DSWeb.Areas.Account.Models.MsOpBill;
using DSWeb.MvcShipping.DAL.Chfee_AuditDAL;
using System.Text;
using DSWeb.Areas.Mobile.Models.Common;
using DSWeb.Areas.Mobile.Models.Approval;
using DSWeb.Areas.Mobile.DAL;
using DSWeb.Areas.Mobile.Models.Login;

namespace DSWeb.Areas.Mobile.Views.Approval
{
    public partial class YSYF : System.Web.Mvc.ViewPage
    {
        List<MsOpBill> dataList = new List<MsOpBill>();
        public string billListHTML;
        public PageContentModel model = new PageContentModel();
        public User _User;
        public string _Keyword;
        protected void Page_Load ( object sender, EventArgs e )
        {
            //GetBillData();
            //SetupBillListHTML();
            var openid = Request.Cookies["openid"] == null ? "" : Request.Cookies["openid"].Value;
            _Keyword = Request["search"] == null ? "" : Request["search"];
            if (openid == "")
            {
                DBLog.Log("cookies-openid", "null");
                //授权并跳转
                //SetupHTML(openid.ToString());
                SetupHTML("123");
            }
            else
            {
                DBLog.Log("cookies-openid", Request.Cookies["openid"].Value);
                SetupHTML(openid.ToString());

            }
            hdcomid.Value = _User.comid;
            hduid.Value = _User.uid;
            hdusername.Value = _User.codename;
            hdkeyword.Value = _Keyword;
        }

        private void SetupHTML (string openid) {

            _User = LoginDAL.GetUserWithOpenId(openid);
            StringBuilder sbMain = new StringBuilder();
           
            string strCondition = @"EXISTS (select 1 from ch_fee left join workflow_using wu on wu.bsno=ch_fee.GID left join workflow_step st  on (st.WORKFLOWID=wu.WORKFLOWID and st.GROUPID=WU.CurrentID)   where v_op_bill.BSNO=ch_fee.BSNO  and isnull((power(2,(ST.STEPNO)) & wu.stepno),0)>0 and st.AUDITOR='" +_User.uid + "' and (ch_fee.FEESTATUS=2 or ch_fee.FEESTATUS=3 or ch_fee.FEESTATUS=4) ) ";
            if (_Keyword!="")
            {
                strCondition += " AND MBLNO like '%"+_Keyword+"%'";
               
            }
            string sort = "";
            dataList = Chfee_AuditDAL.GetBillDataList(0, 50, strCondition, _User.uid, _User.codename, _User.comid, sort);

            //html
            if (dataList.Count > 0)
            {
                foreach (var bill in dataList)
                {
                    string type = bill.OPTYPE=="更改单"?"1":"0";
                    //计算利润
                    YSYFAmountModel amout = ApprovalDAL.GetAmountAndLirun(bill.MBLNO);
                    //更改单利润
                    GGDAmountModel ggd = new GGDAmountModel();
                    ggd.ggdsrmb = "0";
                    ggd.ggdsusd = "0";
                    ggd.ggdfrmb = "0";
                    ggd.ggdfusd = "0";
                    //更改单累加利润
                    GGDAmountModel ggd2 = new GGDAmountModel();
                    ggd2.ggdsrmb = "0";
                    ggd2.ggdsusd = "0";
                    ggd2.ggdfrmb = "0";
                    ggd2.ggdfusd = "0";

                    string lirunRmb = (float.Parse(amout.yingshourmb)-float.Parse(amout.yingfurmb)).ToString("f2");
                    string lirunUsd = (float.Parse(amout.yingshouusd)-float.Parse(amout.yingfuusd)).ToString("f2");
                    string ggdlirunRmb = "0";
                    string ggdlirunUsd = "0";
                    string ggd2lirunRmb = "0";
                    string ggd2lirunUsd = "0";
                    //更改单原因文本
                    string ggdyy = "";
                    if (type=="1")
                    {
                        ggdyy += "<br>更改原因:" + bill.REASON+"<br>创建时间:"+bill.CREATETIME;
                    }
                    //主内容
                    string listStr = "";
                    if (type=="1")
                    {
                        ggd = ApprovalDAL.GetGGDLirunWithMBLNO(bill.MBLNO,bill.BSNO,true);
                        ggd2 = ApprovalDAL.GetGGDLirunWithMBLNO(bill.MBLNO, bill.BSNO, false);
                        ggdlirunRmb = (float.Parse(ggd.ggdsrmb) - float.Parse(ggd.ggdfrmb)).ToString("f2");
                        ggdlirunUsd = (float.Parse(ggd.ggdsusd) - float.Parse(ggd.ggdfusd)).ToString("f2");
                        ggd2lirunRmb = (float.Parse(ggd2.ggdsrmb) - float.Parse(ggd2.ggdfrmb)).ToString("f2");
                        ggd2lirunUsd = (float.Parse(ggd2.ggdsusd) - float.Parse(ggd2.ggdfusd)).ToString("f2");
                        listStr = bill.MBLNO + "<br>[" + bill.CUSTOMERNAME + "][" + "" + bill.OPTYPE + "]" + ggdyy + "<br>应收 RMB:" + ggd.ggdsrmb + "  USD:" + ggd.ggdsusd + "<br>" + "应付 RMB:" + ggd.ggdfrmb + "  USD:" + ggd.ggdfusd + "<br>利润 RMB:" + ggdlirunRmb + "  USD:" + ggdlirunUsd;
                    }
                    else
                    {
                        listStr = bill.MBLNO + "<br>[" + bill.CUSTOMERNAME + "][" + "" + bill.OPTYPE + "]" + ggdyy + "<br>应收 RMB:" + amout.yingshourmb + "  USD:" + amout.yingshouusd + "<br>" + "应付 RMB:" + amout.yingfurmb + "  USD:" + amout.yingfuusd + "<br>利润 RMB:" + lirunRmb + "  USD:" + lirunUsd;
                    }
                   
                    //更改后利润
                    string amoutStr = "";
                    if (type=="1")
                    {
                        string rmbstr = (float.Parse(lirunRmb) + float.Parse(ggd2lirunRmb)).ToString("f2");
                        string usdstr = (float.Parse(lirunUsd) + float.Parse(ggd2lirunUsd)).ToString("f2");
                        
                        amoutStr = "<br>更改后的总利润 <br>RMB:" + rmbstr + "    USD:" + usdstr;

                    }
                    listStr += amoutStr;
                    //机密费用
                    string jimiStr = "";
                    JiMiFeeModel jm = ApprovalDAL.GetJiMiFeeInfo(bill.BSNO, _User.uid);
                    if (jm!=null)
                    {
                        jimiStr += "<br/>"+jm.JimiName + " USD:" + jm.JimiUsd;
                    }
                    listStr += jimiStr;

                    sbMain.Append("<li  style=\"padding-top:0px;padding-bottom:0px;padding-right:20px;padding-left:0px;\">");
                    sbMain.Append("<input class=\"ckbBill\" value=\"" + bill.BSNO + "$" + type + "$"+bill.OPLB+ "\" type=\"checkbox\" name=\"checkbox-0\" style=\"width:20px;height:20px;margin-top:20px\">");
                   // sbMain.Append("<div data-role=\"collapsible\" style=\"margin-left:50px;padding:0px\">");
                    sbMain.Append("<div data-role=\"listview\" style=\"margin-left:50px;padding:0px\">");
                    sbMain.Append("<br/><h3>" + listStr + "</h3><br/>");
                    
                    //详细内容
                   // sbMain.Append("<p>");
                    //sbMain.Append("<ul data-role=\"listview\" data-inset=\"true\">");
                    //StringBuilder sbDet = new StringBuilder();
                    //string condition = "BILLNO = '" + cp.BILLNO + "'";
                    //List<Chfee_do_detail> list = new List<Chfee_do_detail>();
                    //list = ChpayapplicationDAL.GetBodyAuditList(condition);
                    //foreach (var fee in list)
                    //{
                    //    //费用名称
                    //    sbDet.Append("<li  data-role=\"list-divider\" style=\"font-size:18px\">");
                    //    sbDet.Append(fee.FEENAME);
                    //    sbDet.Append("</li>");
                    //    //主提单号
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("主提单号:" + fee.MBLNO);
                    //    sbDet.Append("</li>");
                    //    //委托单位
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("委托单位:" + fee.CUSTOMERNAME);
                    //    sbDet.Append("</li>");
                    //    //核销金额
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("核销金额:" + fee.AMOUNT);
                    //    sbDet.Append("</li>");
                    //    //原始金额
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("原始金额:" + fee.ORIGAMOUNT);
                    //    sbDet.Append("</li>");
                    //    //原始币别
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("原始币别:" + fee.ORIGCURRENCY);
                    //    sbDet.Append("</li>");
                    //    //原始汇率
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("原始汇率:" + fee.ORIGEXCHANGERATE);
                    //    sbDet.Append("</li>");
                    //    //开船日期
                    //    sbDet.Append("<li>");
                    //    sbDet.Append("开船日期:" + fee.ETD);
                    //    sbDet.Append("</li>");
                    //}

                    //sbMain.Append(sbDet.ToString());
                    //sbMain.Append("</ul>");
                    //sbMain.Append("</p>");
                    
                    sbMain.Append("</div>");
                    sbMain.Append("</li>");
                }
                model.content = sbMain.ToString();

                //页脚按钮
                StringBuilder sbFooter = new StringBuilder();
                if (_User.uid == null || _User.uid == string.Empty)
                {
                    sbFooter.Append("<li><a href=\"#\" data-icon=\"minus\" id=\"btnBack\">返回</a></li>");
                }
                else
                {
                    sbFooter.Append("<li><a href=\"#\" data-icon=\"minus\" id=\"btnBack\">返回</a></li>");
                    sbFooter.Append("<li><a href=\"#\" data-icon=\"check\" id=\"btnOKALL\">全选</a></li>");
                    sbFooter.Append("<li><a  href=\"#\" data-icon=\"check\" id=\"btnOK\">所选通过</a></li>");
                    sbFooter.Append("<li><a href=\"#\" data-icon=\"delete\" id=\"btnNO\">驳回</a></li>");
                }

                model.footer = sbFooter.ToString();
            }
            else
            {
                model.content = "没有待审核数据!";
                model.footer = "<li><a href=\"#\" data-icon=\"minus\" id=\"btnBack\">返回</a></li>";
            }

        }

       
    }
}