using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Text;
using System.Xml;

namespace DSWeb.Invoice
{
    public partial class InvoiceMakeOutEdit : System.Web.UI.Page
    {
        private string strInvoiceGID;//已开发票GID
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                strInvoiceGID = Request.QueryString["id"].ToString();
                h_invoiceid.Value = strInvoiceGID;
            }

            if (strInvoiceGID != null)
            {
                GetInvoiceInfo(strInvoiceGID);
                InvoiceDA invoiceDA = new InvoiceDA();

                //string strSql = " (SELECT BSNO,CASE WHEN BSSTATUS = 1 THEN '锁定' ELSE '未锁定' END as BSSTATUS,MBLNO,HBLNO,CUSTNO,INPUTBY,CREATETIME "
                //              + " FROM op_seae WHERE BSNO IN (SELECT DISTINCT B.BSNO FROM ch_fee_invoice as A INNER JOIN ch_fee_do as B ON A.BILLNO = B.BILLNO WHERE A.GID = '" + strInvoiceGID + "'))";
                //strSql += " UNION ALL ";
                //strSql += " (SELECT BSNO,CASE WHEN BSSTATUS = 1 THEN '锁定' ELSE '未锁定' END as BSSTATUS,MBLNO,HBLNO,CUSTNO,INPUTBY,CREATETIME "
                //              + " FROM op_seai WHERE BSNO IN (SELECT DISTINCT B.BSNO FROM ch_fee_invoice as A INNER JOIN ch_fee_do as B ON A.BILLNO = B.BILLNO WHERE A.GID = '" + strInvoiceGID + "'))";

                //2013-02-05 修改
                string strSql = " (SELECT BSNO,CASE WHEN BSSTATUS = 1 THEN '锁定' ELSE '未锁定' END as BSSTATUS,MBLNO,HBLNO,CUSTNO,INPUTBY,CREATETIME "
                              + " FROM VW_Settlement WHERE BSNO IN (SELECT DISTINCT B.BSNO FROM ch_fee_invoice as A INNER JOIN ch_fee_do as B ON A.BILLNO = B.BILLNO WHERE A.GID = '" + strInvoiceGID + "'))";

                DataTable sourceTable = new DataTable();

                sourceTable = invoiceDA.GetExcuteSql(strSql).Tables[0];

                StringBuilder sourceBuilder = new StringBuilder();
                sourceBuilder.Append("{");
                sourceBuilder.Append("opinfo:[");
                string strBuilder = "";
                for (int i = 0; i < sourceTable.Rows.Count; i++)
                {
                    sourceBuilder.Append("{bsno:\"" + sourceTable.Rows[i][0].ToString() + "\",");
                    sourceBuilder.Append("\"bsstatus\":\"" + sourceTable.Rows[i][1].ToString() + "\",");
                    sourceBuilder.Append("\"mblno\":\"" + sourceTable.Rows[i][2].ToString() + "\",");
                    //string strETD = "";
                    //if (!sourceTable.Rows[i][3].ToString().Trim().Equals(""))
                    //{
                    //    strETD = DateTime.Parse(sourceTable.Rows[i][3].ToString()).ToString("yyyy-MM-dd");
                    //}
                    //sourceBuilder.Append("\"etd\":\"" + strETD + "\",");
                    sourceBuilder.Append("\"hblno\":\"" + sourceTable.Rows[i][3].ToString() + "\",");
                    sourceBuilder.Append("\"custno\":\"" + sourceTable.Rows[i][4].ToString() + "\",");
                    sourceBuilder.Append("\"inputby\":\"" + sourceTable.Rows[i][5].ToString() + "\",");
                    sourceBuilder.Append("\"ctime\":\"" + sourceTable.Rows[i][6].ToString() + "\"},");
                }
                InvoiceEntity invoiceEntity = new InvoiceEntity();
                invoiceEntity = invoiceDA.GetInvoiceByGID(strInvoiceGID);

                IList<FeeDoEntity> feeDoEntities = new List<FeeDoEntity>();

                if (invoiceEntity != null)
                {
                    if (invoiceEntity.GID != null)
                    {
                        UserDA userDA = new UserDA();
                        UserEntity userEntity = new UserEntity();

                        userEntity = userDA.GetUserAllByID(invoiceEntity.Operator);

                        feeDoEntities = GetFeeDoXml(invoiceEntity.FeeItem);
                        if (invoiceEntity.FeeItem.Trim() != "")
                        {
                            foreach (FeeDoEntity feeDoEntity in feeDoEntities)
                            {
                                if (feeDoEntity.Orig == 2)
                                {
                                    sourceBuilder.Append("{bsno:\"handmakeout\",");
                                    sourceBuilder.Append("\"bsstatus\":\"直开票\",");
                                    sourceBuilder.Append("\"mblno\":\"\",");
                                    sourceBuilder.Append("\"hblno\":\"\",");
                                    sourceBuilder.Append("\"custno\":\"\",");
                                    sourceBuilder.Append("\"inputby\":\"" + userEntity.ShowName + "\",");
                                    sourceBuilder.Append("\"ctime\":\"" + invoiceEntity.InvoiceMakeTime + "\"}");
                                    sourceBuilder.Append(",");
                                    break;
                                }
                            }
                        }
                    }
                }
                if (sourceBuilder.ToString().Trim().EndsWith(","))
                {
                     strBuilder = sourceBuilder.ToString().Trim().Substring(0, sourceBuilder.ToString().Trim().Length - 1);
                }

                StringBuilder endBuilder = new StringBuilder();
                endBuilder.Append(strBuilder);
                endBuilder.Append("]");
                endBuilder.Append("}");

                this.h_opcache.Value = endBuilder.ToString();
            }
        }

        private void GetInvoiceInfo(string tempInvoiceGID)
        {
            InvoiceEntity invoiceEntity = new InvoiceDA().GetInvoiceByGID(tempInvoiceGID);

            if (invoiceEntity.GID != null)
            {
                this.txt_invoiceno.Value = invoiceEntity.InvoiceNO;
                this.txt_custno.Value = invoiceEntity.CustomerName;
                this.txt_vesselvoyage.Value = invoiceEntity.VesselVoyage;
                this.txt_makeouttime.Value = invoiceEntity.InvoiceMakeTime.ToString("yyyy-MM-dd hh:mm:ss");
                this.txt_custno.Value = invoiceEntity.BSNO;
                //this.txt_mblno.Value = invoiceEntity.
                this.txt_pod.Value = invoiceEntity.POD;
                this.txt_pol.Value = invoiceEntity.POL;

                this.h_account.Value = invoiceEntity.Account;
                this.h_bank.Value = invoiceEntity.Bank;
                this.h_currency.Value = invoiceEntity.Currency;
                this.h_invoicecus.Value = invoiceEntity.InvoiceCustName;

                this.ck_isfee.Checked = invoiceEntity.IsDelete;
                this.ck_isfee.Checked = invoiceEntity.IsNeedFee;
                this.ck_isprint.Checked = invoiceEntity.IsNeedPrint;

                this.h_billno.Value = invoiceEntity.BillNO;
            }
        }

        private IList<FeeDoEntity> GetFeeDoXml(string strXml)
        {
            XmlDocument xmlFeeDoDoc = new XmlDocument();
            IList<FeeDoEntity> feeDoEntities = new List<FeeDoEntity>();

            try
            {
                xmlFeeDoDoc.LoadXml(strXml);

                for (int i = 0; i < xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes.Count; i++)
                {
                    FeeDoEntity feeDoEntity = new FeeDoEntity();
                    feeDoEntity.FeeID = xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[0].InnerText.Trim();
                    feeDoEntity.FeeName = xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[1].InnerText.Trim();
                    feeDoEntity.DoAmount = decimal.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[2].InnerText.Trim());
                    feeDoEntity.Category = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[3].InnerText.Trim());
                    feeDoEntity.FeeType = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[4].InnerText.Trim());
                    feeDoEntity.Orig = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[5].InnerText.Trim());

                    feeDoEntities.Add(feeDoEntity);
                }
            }
            catch (Exception error)
            {
                throw (error);
            }

            return feeDoEntities;
        }
    }
}