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 Solely;
using Product;
using Base;



public partial class member_Trade_Purchase : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                UnitBind();
                int userid = Convert.ToInt32(GetCookie("UserID").ToString());
                int gradeid = Convert.ToInt32(GetCookie("GradeID").ToString());
                if (!Other.CheckUserBussinessCount(gradeid, "purchase", userid))
                {
                    Response.Redirect("../ErrorPage.aspx");
                    return;
                }
            }
        }
        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();
    }
    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(Base.BasePage.GetCookie("UserID"));
            TPurchase TPur = new TPurchase();
            TPur.UserID = id;
            TPur.IsUser = "1";
            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;
            TPur.StartDate = Convert.ToDateTime(this.txtStartDate.Text);
            TPur.EndDate = Convert.ToDateTime(this.txtEndDate.Text);
            TPur.Brand = this.txtBrand.Text;
            TPur.Model = this.txtModel.Text;
            TPur.Unit = dropUnit.SelectedValue;
            TPur.Count = int.Parse(txtCount.Text);
            TPur.Price = txtPrice.Text;
            IPurchase Rpur = new RPurchase();
            if (!Restr.Trim().Equals(string.Empty))
                Restr = Restr.Substring(0, Restr.Length - 1);
            int isEmail = 0;

            //读取指定节点下的value值
            string strFlag = Base.XMLProcess.Read("xml/EmailFlag.xml", "/Item/flag/mode[@type='purchase']", "value");
            if (strFlag == "")
                strFlag = "us_purchase";
            if (Restr.Trim().Equals(string.Empty)) //判断输入是否正确
            {
                Rpur.Insert(TPur, out isEmail);
                if (isEmail > 0)
                {
                    nspBase.Core.clsEmail.SendMail(strFlag, id);
                }
                ShowMessage("恭喜您，商机发布成功！", "Trade/PurchaseManage.aspx");
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = Restr + "填写有误！";
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {

        Response.Redirect("PurchaseManage.aspx");
    }
}
