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 admin_Trade_PurchaseView : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            if (!CheckRights("system", "A1"))
            {
                HandleErr("您没有对该功能的操作权限", "index_Center.aspx", "1");
                return;
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
        }
        if (!IsPostBack)
        {
            UnitBind();
            Bind();
        }

    }
    private void Bind()
    {


        int id = int.Parse(Request.QueryString["ID"].ToString());
        if (Request.QueryString["UserID"] != null)
            this.HiddUserID.Value = Request.QueryString["UserID"].ToString();
        IPurchase RPur = new RPurchase();
        TPurchase TPur = RPur.GateItem(id);
        this.txtName.Text = TPur.Name;
        this.txtKey.Text = TPur.Key;

        this.txtSortB.Value = TPur.IndType;
        this.txtContent.Value = TPur.Info;
        if (TPur.Picture_1 == "")
        {
            this.ImgUrl1.Value = "../../Images/System/wutu.gif";
            this.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.ToString();
        this.txtEndDate.Text = TPur.EndDate.ToString();

        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];
            }
        }
    }
    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();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string myValue = "";
            myValue += Request.Form["SortB"];
            myValue += ',' + Request.Form["SortM"];
            myValue += ',' + Request.Form["SortS"];
            int id = int.Parse(Request.QueryString["ID"].ToString());
            TPurchase TPur = new TPurchase();
            TPur.ID = id;
            TPur.IndType = myValue;
            TPur.Name = this.txtName.Text;
            TPur.Key = this.txtKey.Text;
            TPur.Info = this.txtContent.Value;
            TPur.Picture_1 = this.ImgUrl1.Value;
            if (GetRequest.isDataTime(this.txtStartDate.Text))
                TPur.StartDate = Convert.ToDateTime(this.txtStartDate.Text);
            else
                TPur.StartDate = DateTime.Now;
            if (GetRequest.isDataTime(this.txtEndDate.Text))
                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 (GetRequest.isNum(txtCount.Text))
                TPur.Count = int.Parse(txtCount.Text);
            else
                TPur.Count = 0;
            if (GetRequest.isProce(txtPrice.Text))
                TPur.Price = txtPrice.Text;
            else
                TPur.Price = "0";
            IPurchase Rpur = new RPurchase();
            Rpur.Update(TPur);
            ShowMessage("恭喜您，商机发布成功！", "trade/PurchaseManage.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage("商机发布失败！", "trade/PurchaseManage.aspx");
        }


    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("PurchaseManage.aspx");
    }
    public string GetShortDate(string dateStr)
    {
        if (dateStr != string.Empty)
            return Convert.ToDateTime(dateStr).ToShortDateString();
        else
            return String.Empty;
    }
}
