using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Text;
using DSWeb.DataAccess;
using System.Data.SqlClient;
using DSWeb.Models;
using DSWeb.EntityDA;
using System.Web.Script.Serialization;

namespace DSWeb.Shipping
{
    public partial class SeaExportInfoBusinessChange : PageBase
    {
        public string sSQL = String.Empty;
        public string gvid = String.Empty;
        T_ALL_DA T_ALL_DA = new T_ALL_DA();
        MoneyConvert MoneyConvert = new MoneyConvert();
        TopSeaeEntity TopSeaeEntity = new TopSeaeEntity();
        SeaExportInfoDA SeaExportInfoDA = new SeaExportInfoDA();
        TopSeaeBillmanageEntity TopSeaeBillmanageEntity = new TopSeaeBillmanageEntity();
        TopSeaeBillmanageDA TopSeaeBillmanageDA = new TopSeaeBillmanageDA();
        RangeDA RangeDA = new RangeDA();
        public string strUserID;//用户GID

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] != null)
            {
                strUserID = Session["USERID"].ToString().Trim();
            }
            else
            {
                Server.Transfer("~/Error/FriendError.aspx");
                return;
                //strUserID = ""; 
            }
            //
            if (!IsPostBack)
            {
                if (Request.QueryString["handle"] != null)
                {
                    Session["handle"] = Request.QueryString["handle"].ToString();
                }
                if (Request.QueryString["leid"] != null)
                {
                    Session["leid"] = Request.QueryString["leid"].ToString();
                    this.hdGid.Value = Request.QueryString["leid"].ToString();
                }
                if (Request.QueryString["id"] != null)
                {
                    ViewState["GID"] = Request.QueryString["id"].ToString();
                }
                //
                if (Session["handle"].ToString().Trim() == "edit")
                {
                    if (Session["leid"].ToString().Trim() != null)
                    {
                        DataSet ds = T_ALL_DA.GetTopLetter(Session["leid"].ToString().Trim());
                        if (ds != null)
                        {
                            //TO                    
                            tbTO.Text = ds.Tables[0].Rows[0]["HEAD_TO"].ToString().Trim();
                            //ATTN 
                            tbATTN.Text = ds.Tables[0].Rows[0]["HEAD_ATTN"].ToString().Trim();
                            //FM 
                            tbFM.Text = ds.Tables[0].Rows[0]["INPUTBY"].ToString().Trim();
                            //
                            ds = T_ALL_DA.GetTopLetterChange(Request.QueryString["leid"].ToString().Trim());
                            if (ds != null)
                            {
                                //主提单号
                                tbMBLNO.Text = ds.Tables[0].Rows[0]["MBLNO"].ToString().Trim();
                                //卸货港
                                tbPORTDISCHARGE.Text = ds.Tables[0].Rows[0]["PORTDISCHARGE"].ToString().Trim();
                                //船名/航次
                                tbVESSELVOYNO.Text = ds.Tables[0].Rows[0]["VESSELVOYNO"].ToString().Trim();
                                //开船日期
                                if (ds.Tables[0].Rows[0]["ETD"].ToString().Trim() == "")
                                {
                                    tbETD.Text = "";
                                }
                                else
                                {
                                    tbETD.Text = DateTime.Parse(ds.Tables[0].Rows[0]["ETD"].ToString().Trim()).ToString("yyyy-MM-dd").Trim();
                                }
                                //件重尺
                                tbPKC.Text = ds.Tables[0].Rows[0]["PKC"].ToString().Trim();
                                //箱型箱量
                                tbCNTR.Text = ds.Tables[0].Rows[0]["CNTR"].ToString().Trim();
                                //箱型箱量
                                tbProName.Text = ds.Tables[0].Rows[0]["ProName"].ToString().Trim();
                                //箱型箱量
                                tbProOld.Text = ds.Tables[0].Rows[0]["ProOld"].ToString().Trim();
                                //箱型箱量
                                tbProNew.Text = ds.Tables[0].Rows[0]["ProNew"].ToString().Trim();
                                //备注
                                tbREMARK.Text = ds.Tables[0].Rows[0]["REMARK"].ToString().Trim();
                            }
                        }
                    }

                    //权限可视范围
                    string strV = RangeDA.GetVISIBLERANGE(strUserID.Trim(), "modOrderManagement");
                    string strO = RangeDA.GetOPERATERANGE(strUserID.Trim(), "modOrderManagement");
                    //页面是否只读
                    if (Request.QueryString["hdenabled"] != null)
                    {
                        if (Request.QueryString["hdenabled"].ToString() == "false")
                        {
                            strO = "4";
                        }
                    }
                    //
                    string alt = "";
                    if (strV.Trim() != strO.Trim())
                    {
                        if (strO.Trim().Equals("0"))//全部
                        {
                        }
                        else if (strO.Trim().Equals("1"))//分公司
                        {
                            alt = RangeDA.GetOrderManagementCompany(strUserID.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                        else if (strO.Trim().Equals("2"))//部门
                        {
                            alt = RangeDA.GetOrderManagementDEPT(strUserID.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                        else if (strO.Trim().Equals("3"))//个人
                        {
                            alt = RangeDA.GetOrderManagementPerson(strUserID.Trim(), ViewState["GID"].ToString().Trim());
                            if (alt == "")
                            {
                                getEnabled();
                            }
                        }
                        else if (strO.Trim().Equals("4"))//无
                        {
                            getEnabled();
                        }
                    }
                    //
                }
                else
                {
                    Session["handle"] = Request.QueryString["handle"].ToString();
                    if (ViewState["GID"].ToString().Trim() != null)
                    {
                        TopSeaeEntity = SeaExportInfoDA.GetID(ViewState["GID"].ToString().Trim());
                        if (TopSeaeEntity.BSNO != null)
                        {
                            tbFM.Text = T_ALL_DA.GetSHOWNAME(strUserID.Trim());
                            //主提单号                    
                            tbMBLNO.Text = TopSeaeEntity.MBLNO.ToString().Trim();
                            //卸货港                    
                            tbPORTDISCHARGE.Text = TopSeaeEntity.PORTDISCHARGE.ToString().Trim();
                            //船名/航次                    
                            tbVESSELVOYNO.Text = TopSeaeEntity.VESSEL.ToString().Trim() + " " + TopSeaeEntity.VOYNO.ToString().Trim();
                            //开船日期                
                            if (TopSeaeEntity.ETD.ToString().Trim() == "" || TopSeaeEntity.ETD.ToString().Trim().IndexOf("0001")>-1)
                            {
                                tbETD.Text = "";
                            }
                            else
                            {
                                tbETD.Text = TopSeaeEntity.ETD.ToString("yyyy-MM-dd").Trim();
                            }
                            //件重尺                    
                            tbPKC.Text = TopSeaeEntity.PKGS.ToString().Trim() + TopSeaeEntity.KINDPKGS.ToString().Trim() + "\t |\t " + TopSeaeEntity.KGS.ToString().Trim() + "KGS\t |\t " + TopSeaeEntity.CBM.ToString().Trim() + "CBM";
                            //备注                    
                            tbREMARK.Text = TopSeaeEntity.REMARK.ToString().Trim();
                            //箱型箱量   
                            DataSet dss = T_ALL_DA.GetTopCtn(ViewState["GID"].ToString().Trim());
                            if (dss != null)
                            {
                                foreach (DataTable myTable in dss.Tables)
                                {
                                    foreach (DataRow myRow in myTable.Rows)//遍历表
                                    {
                                        tbCNTR.Text += myRow["CTNALL"].ToString().Trim() + "*" + myRow["CTNNUM"].ToString().Trim() + " \t|\t ";
                                    }
                                }
                                tbCNTR.Text = tbCNTR.Text.Trim().Substring(0, tbCNTR.Text.Trim().Length - 1);
                            }
                        }
                    }
                    else
                    {
                    }
                    this.imgPrint.Disabled = true;
                }
            }
        }

        protected void getEnabled()
        {
            ibEnter.Enabled = false;
            this.imgPrint.Disabled = true;
            //
            tbTO.Enabled = false;
            tbATTN.Enabled = false;
            tbFM.Enabled = false;
            tbVESSELVOYNO.Enabled = false;
            tbMBLNO.Enabled = false;
            tbPORTDISCHARGE.Enabled = false;
            tbETD.Enabled = false;
            tbPKC.Enabled = false;
            tbCNTR.Enabled = false;
            tbProName.Enabled = false;
            tbProOld.Enabled = false;
            tbProNew.Enabled = false;
            tbREMARK.Enabled = false;
        }

        protected void btnEnter_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["handle"].ToString().Trim() != null && Session["handle"].ToString().Trim() == "edit")
            {
                if (Session["leid"].ToString().Trim() != null && Session["leid"].ToString().Trim() != "")
                {
                    string alt = T_ALL_DA.GetTopLetterChangeUpdate(Session["leid"].ToString().Trim(), tbTO.Text.Trim(), tbATTN.Text.Trim(), tbFM.Text.Trim(), tbMBLNO.Text.Trim(), tbPORTDISCHARGE.Text.Trim(), tbVESSELVOYNO.Text.Trim(), tbETD.Text.Trim(), tbPKC.Text.Trim(), tbCNTR.Text.Trim(), tbProName.Text.Trim(), tbProOld.Text.Trim(), tbProNew.Text.Trim(), tbREMARK.Text.Trim(), strUserID.Trim());
                    if (alt == "")
                    {
                        Response.Write("<script>alert('更改成功!');window.opener.location.reload();history.back();</script>");
                        return;
                    }
                    else
                    {
                        Response.Write("<script>alert('" + alt + "');history.back();</script>");
                        return;
                    }
                }
            }
            else
            {
                Session["leid"] = T_ALL_DA.getSQLID("TopLetterChange");
                string alt = T_ALL_DA.GetTopLetterChangeInsert(Session["leid"].ToString().Trim(), ViewState["GID"].ToString().Trim(), tbTO.Text.Trim(), tbATTN.Text.Trim(), tbFM.Text.Trim(), tbMBLNO.Text.Trim(), tbPORTDISCHARGE.Text.Trim(), tbVESSELVOYNO.Text.Trim(), tbETD.Text.Trim(), tbPKC.Text.Trim(), tbCNTR.Text.Trim(), tbProName.Text.Trim(), tbProOld.Text.Trim(), tbProNew.Text.Trim(), tbREMARK.Text.Trim(), strUserID.Trim());
                if (alt == "")
                {
                    Session["handle"] = "edit";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>reloadParent('" + ViewState["GID"].ToString().Trim() + "','" + Session["leid"].ToString().Trim() + "');</script>");
                    //Response.Write("<script>alert('添加成功!');window.opener.location.reload();history.back();</script>");
                    return;
                }
                else
                {
                    Response.Write("<script>alert('" + alt + "');history.back();</script>");
                    return;
                }
            }
        }
    }
}