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;

public partial class member_Trade_PurchaseEdit : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                UnitBind();
                Bind();
            }
        }
        else
        {
            Response.Redirect("../login/login.aspx");
        }

    }

    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"]);
        IPurchase RPur = new RPurchase();
        TPurchase TPur = RPur.GateItem(id);
        if (TPur!=null)
        {
            this.txtName.Text = TPur.Name;
            this.txtKey.Text = TPur.Key;

            this.txtSortB.Value = TPur.IndType;
            this.txtContent.Value = TPur.Info;
            if (TPur.Picture_1 == "")
            {
                ImgUrl1.Value = "../../Images/System/wutu.gif";
                ImgUrl.Src = "../../Images/System/wutu.gif";
            }
            else
            {
                this.ImgUrl1.Value = TPur.Picture_1;
                this.ImgUrl.Src = TPur.Picture_1;
            }
            this.txtBrand.Text = TPur.Brand;
            this.txtModel.Text = TPur.Model;
            this.txtStartDate.Text = TPur.StartDate.ToShortDateString();
            this.txtEndDate.Text = TPur.EndDate.ToShortDateString();

            this.dropUnit.SelectedValue = TPur.Unit;
            this.txtCount.Text = TPur.Count.ToString();
            this.txtPrice.Text = TPur.Price;
        }
        if (TPur.IndType != null)
        {
            string[] str = TPur.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$province"] != null && Request.Form["ctl00$ContInfo$province"] != "")
                myValue += Request.Form["ctl00$ContInfo$province"] + ",";
            if (Request.Form["ctl00$ContInfo$city"] != null && Request.Form["ctl00$ContInfo$city"] != "")
                myValue += Request.Form["ctl00$ContInfo$city"] + ",";
            if (Request.Form["ctl00$ContInfo$county"] != null && Request.Form["ctl00$ContInfo$county"] != "")
                myValue += Request.Form["ctl00$ContInfo$county"];
            int id = int.Parse(Request.QueryString["ID"].ToString());
            TPurchase TPur = new TPurchase();
            TPur.ID = id;
            TPur.IndType = myValue;
            if (txtName.Text.Trim().Equals(string.Empty))
                Restr += "产品名称，";
            else
                TPur.Name = this.txtName.Text;
            if (txtKey.Text.Trim().Equals(string.Empty))
                Restr += "关键字，";
            else
                TPur.Key = this.txtKey.Text;
            TPur.Info = this.txtContent.Value;
            TPur.Picture_1 = this.ImgUrl1.Value;
            if (txtStartDate.Text != string.Empty)
                TPur.StartDate = Convert.ToDateTime(this.txtStartDate.Text);
            else
                TPur.StartDate = DateTime.Now;
            if (txtEndDate.Text != string.Empty)
                TPur.EndDate = Convert.ToDateTime(this.txtEndDate.Text);
            else
                TPur.EndDate = DateTime.Now;
            TPur.Brand = this.txtBrand.Text;
            TPur.Model = this.txtModel.Text;
            TPur.Unit = dropUnit.SelectedValue;
            if (txtCount.Text != string.Empty)
                TPur.Count = int.Parse(txtCount.Text);
            else
                TPur.Count = 0;
            if (txtPrice.Text != string.Empty)
                TPur.Price = txtPrice.Text;
            else
                TPur.Price = "0";
            IPurchase Rpur = new RPurchase();
            if (!Restr.Trim().Equals(string.Empty))
                Restr = Restr.Substring(0, Restr.Length - 1);
            if (Restr.Trim().Equals(string.Empty))
            {
                Rpur.Update(TPur);
                Messagess.ShowMessage("恭喜您，商机发布成功！", "PurchaseManage.aspx");
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = Restr + "填写有误！";
            }
        }
        catch (Exception ex)
        {
            Messagess.ShowMessage(ex.Message);
        }

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