using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using Sybase.DataWindow;
using TXSoft.Common;
using TXSoft.ExtPB;
using TXSoft.DataStore;
using A8ERP;
using System.Text.RegularExpressions;
using A8ERP.Common;
using Newtonsoft.Json;

namespace A8ERP.CaiGou.Base
{
    public partial class W_Main
    {

        protected string dw_yw_cg_list = string.Empty;
        protected string dw_yw_cg_top = string.Empty;
        protected string dw_yw_cg_mx = string.Empty;
        protected string tableNameHz = string.Empty;
        protected string tableNameMx = string.Empty;
        protected string flag = "CG";
        protected string cg_fs_dwChild = string.Empty;
        protected string dw_yw_cg_top_title = string.Empty; // 入（退）标题
        protected string dw_yw_cg_top_title_init_djlx = string.Empty;// 入（退）默认值 
        protected bool isHaveKc = true;
        protected string curSFQY_CGYW_JJZE_SHURU = "0";
        protected string curFormPzh ="";//外边凭证反调

        public W_Main()
        {

        }

        public override void OnLoad()
        {
           
            base.OnLoad();

            #region 外来凭证打开
            if (Request["curFormPzh"]!=null)
            {
             curFormPzh=Request["curFormPzh"].ToString();
            }
            this.SetParm("curFormPzh", curFormPzh);
            //来源于对账单
            string curDzdPzh = "";
            if (Request["curDzdPzh"] != null)
            {
                curDzdPzh = Request["curDzdPzh"].ToString();
            }

            this.SetParm("curDzdPzh", curDzdPzh);
          
            #endregion

            this.SetParm("tableNameHz", tableNameHz);
            this.SetParm("tableNameMx", tableNameMx);

            if (string.IsNullOrEmpty(dw_yw_cg_list))
            {
                dw_yw_cg_list = "dw_yw_cg_ddhz_list_cx";
            }
            if (string.IsNullOrEmpty(dw_yw_cg_top))
            {
                dw_yw_cg_top = "dw_yw_cg_ddmx_top";
            }
            if (string.IsNullOrEmpty(dw_yw_cg_mx))
            {
                dw_yw_cg_mx = "dw_yw_cg_ddmx_list";
            }
            if (string.IsNullOrEmpty(cg_fs_dwChild))
            {
                cg_fs_dwChild = "CG_DDFS";
            }
            this.SetParm("curSfqytmlsh", AppService.GetXiTongCanShu("yw_cg_tmlsh","xtcs","qybz"));
            this.SetParm("curDate", DBHelp.GetServerCurrentDateFormatYYYYMMDDHHMMSS());
            dw_hz.DataStore.DataWindowObject = dw_yw_cg_list;
            dw_mx_top.DataStore.DataWindowObject = dw_yw_cg_top;
            dw_mx.DataStore.DataWindowObject = dw_yw_cg_mx;
            base.Dw_UserPerMission = dw_mx;

            #region 每页显示条数&查询天数
            string curDayAndPageSize=DBHelp.GetDayAndPageSize(this.FunctionID,"CG");

            string curPageSize = curDayAndPageSize.Split('#')[1];
            //this.dw_hz.PageSize =Convert.ToInt32( curPageSize);
            sle_Count.Text = curPageSize;
            // 查询天数
            string seldays = curDayAndPageSize.Split('#')[0];
            this.SetParm("seldays", seldays.ToString());
           
            #endregion

            if (!string.IsNullOrEmpty(dw_yw_cg_top_title))
            {
                this.SetParm("dw_yw_cg_top_title_value", dw_yw_cg_top_title);
            }
            if (string.IsNullOrEmpty(dw_yw_cg_top_title_init_djlx))
            {
                dw_yw_cg_top_title_init_djlx = "1";
            }
            this.SetParm("dw_yw_cg_top_title_init_djlx_value", dw_yw_cg_top_title_init_djlx);
            this.SetParm("companyidLength", AppService.GetXiTongCanShu("userid", "companyid", "bzxx"));
            this.SetParm("companyid", AppService.GetXiTongCanShu("userid", "companyid", "csmc") + AppService.GetXiTongCanShu("userid", "companyid", "qybz"));

            string yhbh = A8ERP.AppService.GetUserID();
            this.SetParm("curYhbh", yhbh);
            this.SetParm("curQxlx", "cg");
            //dw_hz.Retrieve(dw_yw_cg_top_title_init_djlx, yhbh);

            DataWindowChild dwc = dw_mx.GetChild("spkh");
         
        //    dwc.Retrieve("%");

           // 
            DataWindowChild idwc_ys_child = dw_mx.GetChild("ysbh");
            dwc.SetTransaction(this.AdoTransaction);

            dwc = dw_mx.GetChild("bxbh");
            dwc.SetTransaction(this.AdoTransaction);

            dwc = dw_mx_top.GetChild("gysbh");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("%", yhbh);

            dwc = dw_mx_top.GetChild("xxly");
            //dwc.SetTransaction(this.AdoTransaction);
            //dwc.Retrieve("XXLY");
            string Json = AppService.GetXiTongCanShuFroCsfl("XXLY");
            DataTable dt = JsonConvert.DeserializeObject<DataTable>(Json);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dwc.InsertRow(i + 1);
                dwc.SetItemString(i + 1, "csfl", "XXLY");
                dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
            }
 

            dwc = dw_mx_top.GetChild("bmbh");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("%", yhbh, "cg");

            dwc = dw_mx_top.GetChild("cgry");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("%", yhbh,"cg");


            dwc = dw_mx_top.GetChild("cgfs");
            //dwc.SetTransaction(this.AdoTransaction);
            //dwc.Retrieve(cg_fs_dwChild);       // 订单方式的下拉框。
            Json = AppService.GetXiTongCanShuFroCsfl(cg_fs_dwChild);
             dt = JsonConvert.DeserializeObject<DataTable>(Json);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dwc.InsertRow(i + 1);
                dwc.SetItemString(i + 1, "csfl", cg_fs_dwChild);
                dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
            }

            if (flag == "CR" || flag == "CT")
            {
                //dwc.SetTransaction(this.AdoTransaction);
                //dwc.Retrieve("rklx");

                dwc = dw_mx.GetChild("rkfs");

                Json = AppService.GetXiTongCanShuFroCsfl("rklx");
                dt = JsonConvert.DeserializeObject<DataTable>(Json);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dwc.InsertRow(i + 1);
                    dwc.SetItemString(i + 1, "csfl", "rklx");
                    dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                    dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
                }

                dwc = dw_mx_top.GetChild("rkfs");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dwc.InsertRow(i + 1);
                    dwc.SetItemString(i + 1, "csfl", "rklx");
                    dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                    dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
                }
                //dwc.SetTransaction(this.AdoTransaction);
                //dwc.Retrieve("rklx");
                //Json = AppService.GetXiTongCanShuFroCsfl("rklx");
                //dt = JsonConvert.DeserializeObject<DataTable>(Json);
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    dwc.InsertRow(i + 1);
                //    dwc.SetItemString(i + 1, "csfl", "rklx");
                //    dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                //    dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
                //}
            }
            dw_tmjqfs.Retrieve();//条码扫描截取方式。。 

            ddlb_2.Items.Add("全部单据");
            ddlb_2.Items.Add("新增单据");
            ddlb_2.Items.Add("转接单据");
            ddlb_2.Text = "全部单据";
            //数据窗口关联服务
            //LinkageService service = (LinkageService)dw_mx_top.Services.Add(ServiceName.Linkage);
            //service.SetMaster(dw_hz.DataStore);
            //service.Add("pzh", "pzh");
            //service.LinkageStyle = LinkageStyle.Retrieve;

            //service = (LinkageService)dw_mx.Services.Add(ServiceName.Linkage);
            //service.SetMaster(dw_mx_top.DataStore);
            //service.Add("pzh", "pzh");
            //service.LinkageStyle = LinkageStyle.Retrieve;

            this.SetParm("flag", flag); ;
            this.SetParm("curUserId", AppService.GetUserID().ToString());
            this.SetParm("curBmbhAndBmmc", new DBHelp().GetCurrBusinessDefaultBmbhAndBmmc(this.FunctionID));
            this.SetParm("curFunctionId", this.FunctionID);
            this.SetParm("cur35ColString", A8ERP.Common.DWServiceHelp.GetCMByString1());
            this.SetParm("RowAndCol", AppService.Get35HangAndLie());// 35列的表头的行和列
            this.SetParm("curJqm", AppService.GetJqm());
            this.SetParm("curUserGroupId", AppService.GetUser().GroupId);//权限组

            this.SetParm("curSFQY_CGYW_JJZE_SHURU", curSFQY_CGYW_JJZE_SHURU);//权限组
            #region 订单的反审核操作
            if ("CG".Equals(flag))
            {
                tb_1.GetItem("btn_FAudit").Visible = true;
            }
            #endregion

            #region  处理权限
            dw_mkfhjb.Retrieve(this.FunctionID);  // 审核级别 
            #endregion

            for (int i = 1; i <= dw_mkfhjb.RowCount; i++)
            {
                ddlb_1.Items.Add( dw_mkfhjb.GetItemString(i, "fhmc"));
            }
            string curShzt = "";
            curShzt = dw_mkfhjb.GetItemString(dw_mkfhjb.RowCount-1, "fhmc");
            
            #region 整单折扣处理
            if ("CR".Equals(flag) || "CT".Equals(flag) || "CG".Equals(flag) || "CZ".Equals(flag))
            {
                em_1.Visible = true;
                st_1.Visible = true;
                this.st_1.X = 796;
                this.st_1.Y = 113;
                this.em_1.X = 869;
                this.em_1.Y = 110;
            }
            #endregion

            #region 整单折扣处理
            if (!"CR".Equals(flag) && !"CT".Equals(flag))
            {
                sc_1.SplitterDistance = 170;
            }
            #endregion



            #region 通知单没有记账操作

            ddlb_1.Items.Add("未记账");
            ddlb_1.Items.Add("已记账");
            ddlb_1.Text = "未记账";
            if ("CG".Equals(flag))
            {
                ddlb_1.Items.Add("已转单");
                ddlb_1.Items.Add("未转单");
            }
          
               
           // }
            #endregion
            ddlb_1.Items.Add("全部");
           
            #region 出入库单没有转单操作
            if ("CT".Equals(flag) || "CR".Equals(flag))
            {
                tb_1.GetItem("btn_Trans").Visible = false;
                dwc = dw_mx_top.GetChild("fklx");
                //dwc.SetTransaction(this.AdoTransaction);
                //dwc.Retrieve("FKLX");
                Json = AppService.GetXiTongCanShuFroCsfl("FKLX");
                dt = JsonConvert.DeserializeObject<DataTable>(Json);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dwc.InsertRow(i + 1);
                    dwc.SetItemString(i + 1, "csfl", "FKLX");
                    dwc.SetItemString(i + 1, "csbh", dt.Rows[i]["csbh"].ToString());
                    dwc.SetItemString(i + 1, "csmc", dt.Rows[i]["value"].ToString());
                }
            }
            #endregion

            #region  处理标题35列



            #region  处理标题35列

            if (this.FunctionID == "SJ050")
            {
                if ("1".Equals(AppService.GetCGDDGSYKCFLAG()))
                {
                    Common.DWServiceHelp.Add35CmColShowDbRfkc(dw_mx, isHaveKc);

                }
                else
                {
                    Common.DWServiceHelp.Add35CmCol(dw_mx, isHaveKc);
                    Common.DWServiceHelp.Add35CmCol(dw_import_PL, isHaveKc);
                }
            }
            else
            {
                Common.DWServiceHelp.Add35CmCol(dw_mx, isHaveKc);
                Common.DWServiceHelp.Add35CmCol(dw_import_PL, isHaveKc);
            }
            dw_mx.Modify("datawindow.detail.color='16777215\tif(currentrow()=getrow(), 12639424,if(mod(getrow(),2)=0,536870912, 16777215) )' ");
            #endregion

           // Common.DWServiceHelp.Add35CmCol(dw_mx, isHaveKc);
           // Common.DWServiceHelp.Add35CmCol(dw_import_PL, isHaveKc);
           string curRowAndCol= Common.DWServiceHelp.GetCmBtRowAndCol();
           this.SetParm("curRowAndCol", curRowAndCol);
            #endregion

           //来源于首页
           string curBmbh = Request["curParam"];
           string curGysbh = Request["curParam1"];
           this.SetParm("curBmbh", curBmbh);
           this.SetParm("curGysbh", curGysbh);

            if (this.Page != null)
            {

                this.Page.ClientScript.RegisterClientScriptInclude("W_Main", "/CaiGou/Base/W_Main.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_Main_Error_List", "/CaiGou/Base/W_Main_Error_List.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("OpenWindowQueryBase", "/DangAn/Base/OpenWindowQueryBase.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("OpenWindowSimpleChoseBase", "/DangAn/Base/OpenWindowSimpleChoseBase.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_CaiGou_ChaXun", "/CaiGou/Base/W_CaiGou_ChaXun.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_HYHKDR", "/import/W_HYHKDR.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_TXTDR", "/import/W_TXTDR.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_PH_FenJian_List", "/PiFa/PiFaBase/W_PH_FenJian_List.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_YW_ZNSM_CGYW", "/YeWuZongHeFenXi/W_YW_ZNSM_CGYW.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_CaiGou_Rkjl", "/CaiGou/Base/W_CaiGou_Rkjl.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_KC_Dj_PIC", "/KuCun/TongJiFenXi/W_KC_Dj_PIC.win.js?JSV=" + AppService.GetJSVesion());

            }

            if ("CT".Equals(flag) || "CR".Equals(flag))
            {
                this.rkljts.Visible = true;
            }
            //string FormatPzhAndValue = AppService.GetYwFormatBH(AppService.GetUserID(), this.FunctionID);
            //if (FormatPzhAndValue != null && FormatPzhAndValue != "")
            //{
            //    this.SetParm("format_pzh", FormatPzhAndValue.Split('&')[0].ToString());
            //   // AppService.GetYwFormat(dw_hz, FormatPzhAndValue.Split('&')[1].ToString());
            //} 

          //  DataTable dt = null;
         //   DBHelp db = new DBHelp();

            #region 业务精确度
            double jqd = 0;
            string csmc = "";
            string cs = AppService.GetXiTongCanShu("CG", "YW_JQD","csmc")+"!"+AppService.GetXiTongCanShu("CG", "YW_JQD", "bzxx");
            if (cs != "-1") {
                csmc = cs.Split('!')[0];
                if (csmc == "0")
                {
                    jqd = Convert.ToDouble(cs.Split('!')[1]) * 0.1;
                }
                else if (csmc == "1")
                {
                    jqd = Convert.ToDouble(cs.Split('!')[1]) * 0.01;
                }
                else if (csmc == "2")
                {
                    jqd = Convert.ToDouble(cs.Split('!')[1]) * 0.001;
                }
                else if (csmc == "3")
                {
                    jqd = Convert.ToDouble(cs.Split('!')[1]) * 0.0001;
                }
            }
            //dt = db.QueryRDataTable(" select csmc,bzxx from da_qj_csb where csfl='YW_JQD' AND csbh='CG' ");
            //int count = dt.Rows.Count;
            //if (count > 0)
            //{
            //    csmc = dt.Rows[0]["csmc"].ToString();
            //    if (csmc == "0")
            //    {
            //        jqd = Convert.ToDouble(dt.Rows[0]["bzxx"]) * 0.1;
            //    }
            //    else if (csmc == "1")
            //    {
            //        jqd = Convert.ToDouble(dt.Rows[0]["bzxx"]) * 0.01;
            //    }
            //    else if (csmc == "2")
            //    {
            //        jqd = Convert.ToDouble(dt.Rows[0]["bzxx"]) * 0.001;
            //    }
            //    else if (csmc == "3")
            //    {
            //        jqd = Convert.ToDouble(dt.Rows[0]["bzxx"]) * 0.0001;
            //    }
            //}
            #endregion

            #region 是否显示版型
            string bxshow = "";
            cs = AppService.GetXiTongCanShu("sf", "xsbx","qybz");
            if (cs != "-1") {
                bxshow = cs;
            }
            //dt = db.QueryRDataTable(" select top 1 qybz from da_sp_bxbt ");
            //int bxcount = dt.Rows.Count;
            //if (bxcount > 0)
            //{
            //    bxshow = dt.Rows[0]["qybz"].ToString();
            //}
            #endregion

       //     db.Close();
      //      dt.Dispose();
            this.SetParm("curJqd", jqd.ToString());
            this.SetParm("curJqw", csmc.ToString());
            this.SetParm("bxshow", bxshow.ToString());
            //--------------------打印----------------------------
            dw_print.AllowCreate = true;
            dw_print.AllowRetrieve = true;
            dw_print.AllowSetFullState = true;
            dw_proxy.AllowCreate = true;
            dw_proxy.AllowRetrieve = true;
            dw_proxy.AllowSetFullState = true;
            //--------------------打印结束-------------------------

            //设置业务基础价格取价方式
            if (!"ERROR".Equals(AppService.GetQjfs()))
                this.SetParm("QJFS", AppService.GetQjfs());
            ////设置条码录入模式
            //if (!"ERROR".Equals(AppService.GetTmlrms()))
            //    this.SetParm("TMLRMS", AppService.GetTmlrms());

            //if (Request["curParm1"] != null)
            //{
            //    string curpzh = Request["curParm1"];
            //    this.SetParm("curpzh", curpzh);
            //} 
           this.SetParm("GnqxStr", this.GnqxStr);// 权限传到前台给右键使用。
           if (this.GnqxStr != null)
           {
               //修改权限
               if ((this.GnqxStr).ToString().IndexOf("btn_Edit") < 0)
               {
                   this.btn_Save.Visible = false;
               }
               //单据复制权限
               if ((this.GnqxStr).ToString().IndexOf("rbn_djfz") < 0)
               {
                   this.rbn_djfz.Enabled = false;
               }
               //单据粘贴权限
               if ((this.GnqxStr).ToString().IndexOf("rbn_djzt") < 0)
               {
                   this.rbn_djzt.Enabled = false;
               }
               //单据导出权限
               if ((this.GnqxStr).ToString().IndexOf("rbn_dcel") < 0)
               {
                   this.btn_Export.Enabled = false;
               }
               //excel导入权限
               if ((this.GnqxStr).ToString().IndexOf("rbn_drcel") < 0)
               {
                   this.rbn_drcel.Enabled = false;
               }
           }

           if ("1".Equals(curSFQY_CGYW_JJZE_SHURU))
           {
               dw_mx.Modify("jjze.visible='0' hjjze.visible='1'");
           }
           else
           {
               dw_mx.Modify("jjze.visible='1' hjjze.visible='0'");
           }
           string curjgqx = base.ColumnQxStr;         
           int isExist_jj = curjgqx.IndexOf("jj");
           if (isExist_jj != -1)
           {
               dw_hz.Modify("zjje.visible='0'");
               dw_mx.Modify("jj.visible='0' jjze.visible='0' zkl.visible='0' hjjze.visible='0' ");
           }
           int isExist_lsj = curjgqx.IndexOf("lsj");
           if (isExist_lsj != -1)
           {
               dw_hz.Modify("lsjze.visible='0'");
               dw_mx.Modify("lsj.visible='0' lsjze.visible='0'");
           }

           int isExist_dpj = curjgqx.IndexOf("dpj");
           if (isExist_dpj != -1)
           {
               dw_hz.Modify("dpjze.visible='0'");
               dw_mx.Modify("dpj.visible='0' dpjze.visible='0'");
           }
           this.SetParm("curjgqx", curjgqx);

           if ("CG".Equals(flag))
           {
               sc_1.Panel1.Title = "采购订单";
           }
           else if ("CZ".Equals(flag))
           {
               sc_1.Panel1.Title = "采购通知单";
               btn_Audit.Visible = false;
               btn_UnAudit.Visible = false;
               btn_Tally.Visible = false;

           }
           else if ("CR".Equals(flag))
           {
               sc_1.Panel1.Title = "采购入库单";
               ddlb_2.Visible = true;
           }
           else if ("CS".Equals(flag))
           {
               sc_1.Panel1.Title = "采购退货申请单";
           }
           else if ("CD".Equals(flag))
           {
               sc_1.Panel1.Title = "采购退货通知单";
           }
           else if ("CT".Equals(flag))
           {
               sc_1.Panel1.Title = "采购退货单";
               ddlb_2.Visible = true;
           }
   
           dw_mx.ExportProcessSparse = false;

           this.IsRegisterScriptFile = false;
           
        }
    }
}