using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Product;
using Base;
using Corporation.Product;

public partial class member_Trade_SupplyEdit : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckCookies())
        {
            Response.Redirect("../ErrorPage.aspx?Err=0");
            return;
        }
        else
        {
            if (!IsPostBack)
            {
                ProTypeBind();
                UnitBind();
                Bind();
            }
        }
    }
    private void ProTypeBind()
    {
        IProType ProType = new RProType();
        DataTable dt = ProType.GetDataTable();
        if (dt!=null)
        {
        dropProType.DataSource = dt.DefaultView;
        dropProType.DataValueField = "ID";
        dropProType.DataTextField = "Name";
        dropProType.DataBind();
        }
       
    }
    private void UnitBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(@Server.MapPath("~/xml/UnitPrice.xml"));
        dropUnit.DataSource = ds.Tables[0];
        dropUnit.DataTextField = ds.Tables[0].Columns[1].ToString();
        dropUnit.DataValueField = ds.Tables[0].Columns[0].ToString();
        dropUnit.DataBind();
    }
    private void Bind()
    {
        int id = int.Parse(Request.QueryString["ID"].ToString());
        ISupply RSupp = new RSupply();
        TSupply TSupp = RSupp.GateItem(id);
        if (TSupp!=null)
        {
            this.txtName.Text = TSupp.Name;
            this.txtKey.Text = TSupp.Key;
            if (TSupp.Picture_1 == "")
            {
                this.ImgUrl1.Value = "../../Images/System/wutu.gif";
                this.ImgUrl.Src = "../../Images/System/wutu.gif";
            }
            else
            {
                this.ImgUrl1.Value = TSupp.Picture_1;
                this.ImgUrl.Src = TSupp.Picture_1;
            }
            this.dropProType.SelectedValue = TSupp.ProType.ToString();
            this.txtPlace.Text = TSupp.Place;
            this.txtBrand.Text = TSupp.Brand;
            this.txtModel.Text = TSupp.Model;
            this.txtPrice.Text = TSupp.Price.ToString();
            this.txtQuantity.Text = TSupp.Quantity.ToString();
            this.dropUnit.SelectedValue = TSupp.Units;
            this.txtMinimum.Text = TSupp.Minimum.ToString();
            this.radlDeadline.SelectedValue = TSupp.Deadline.ToString();
            this.txtContent.Value = TSupp.Info;
            this.txtStartDate.Text = TSupp.StartDate.ToShortDateString();
            this.txtEndDate.Text = TSupp.EndDate.ToShortDateString();
            this.txtRemark.Text = TSupp.Remark;
            this.txtDocument.Text = TSupp.Document;
        }


        if (TSupp.IndType != null)
        {
            string[] str = TSupp.IndType.Split(new char[] { ',' });
            for (int i = 0; i < str.Length; i++)
            {
                if (i == 0)
                    this.txtSortB.Value = str[i];
                else if (i == 1)
                    this.txtSortM.Value = str[i];
                else
                    this.txtSortS.Value = str[i];
            }
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string Restr = "";
            string myValue = "";
             if (Request.Form["ctl00$ContInfo$SortB"] != null && Request.Form["ctl00$ContInfo$SortB"] != "")
                myValue += Request.Form["ctl00$ContInfo$SortB"];
            if (Request.Form["ctl00$ContInfo$SortM"] != null && Request.Form["ctl00$ContInfo$SortM"] != "")
                myValue += "," + Request.Form["ctl00$ContInfo$SortM"] + ",";
            if (Request.Form["ctl00$ContInfo$SortS"] != null && Request.Form["ctl00$ContInfo$SortS"] != "")
                myValue += "," + Request.Form["ctl00$ContInfo$SortS"];
            int id = int.Parse(Request.QueryString["ID"].ToString());
            TSupply TSup = new TSupply();
            TSup.ID = id;
            if (txtName.Text.Trim().Equals(string.Empty))
                Restr += "产品名称，";
            else
                TSup.Name = this.txtName.Text;
            if (txtKey.Text.Trim().Equals(string.Empty))
                Restr += "关键字，";
            else
                TSup.Key = this.txtKey.Text;
            TSup.IndType = myValue;
            TSup.Picture_1 = this.ImgUrl1.Value;

            if (dropProType.SelectedValue!="")
            {
               TSup.ProType = int.Parse(this.dropProType.SelectedValue);
            }

            TSup.Place = this.txtPlace.Text;
            TSup.Brand = this.txtBrand.Text;
            TSup.Model = this.txtModel.Text;
            if (txtPrice.Text != string.Empty)
                TSup.Price = int.Parse(this.txtPrice.Text);
            else
                TSup.Price = 0;
            if (txtQuantity.Text != string.Empty)
                TSup.Quantity = int.Parse(this.txtQuantity.Text);
            else
                TSup.Quantity = 0;

            TSup.Units = this.dropUnit.SelectedValue;
            if (txtMinimum.Text != string.Empty)
                TSup.Minimum = int.Parse(this.txtMinimum.Text);
            else
                TSup.Minimum = 0;
            TSup.Deadline = int.Parse(this.radlDeadline.SelectedValue);
            TSup.Info = this.txtContent.Value;
            if (txtStartDate.Text != string.Empty)
                TSup.StartDate = Convert.ToDateTime(this.txtStartDate.Text);
            else
                TSup.StartDate = DateTime.Now;
            if (txtEndDate.Text != string.Empty)
                TSup.EndDate = Convert.ToDateTime(this.txtEndDate.Text);
            else
                TSup.EndDate = DateTime.Now;

            TSup.Remark = this.txtRemark.Text;
            TSup.Document = this.txtDocument.Text;


            ISupply RSup = new RSupply();
            if (!Restr.Trim().Equals(string.Empty))
                Restr = Restr.Substring(0, Restr.Length - 1);
            if (Restr.Trim().Equals(string.Empty))
            {
                RSup.Update(TSup);
                ShowMessage("恭喜您，商机修改成功！", "Trade/SupplyManage.aspx");
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = Restr + "填写有误！";
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);

        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("SupplyManage.aspx");
    }
}
