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;
using Corporation.Product;


public partial class member_Supply_Supplyt : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                ProTypeBind();
                UnitBind();
                int userid = Convert.ToInt32(GetCookie("UserID").ToString());
                int gradeid = Convert.ToInt32(GetCookie("GradeID").ToString());
                if (!Other.CheckUserBussinessCount(gradeid, "supply", userid))
                {
                    Response.Redirect("../ErrorPage.aspx");
                }
            }
        }
        else
            Response.Redirect("../../login/login.aspx");
    }
    private void ProTypeBind()
    {
        dropProType.Items.Add(new ListItem("--+所有类别+--", ""));
        IProType ProType = new RProType();
        TProType TPro = new TProType();
        TPro.UserID = Convert.ToInt32(GetCookie("UserID").ToString());
        DataTable dt = ProType.GetDataTable(TPro);
        if (dt.Rows.Count != 0)
        {
            foreach (DataRow dataRow in dt.Rows)
            {

                dropProType.Items.Add(new ListItem(dataRow["Name"].ToString(), dataRow["ID"].ToString()));

            }
        }
    }
    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"));
            TSupply TSup = new TSupply();
            TSup.UserID = 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;
            TSup.Thumbnail = this.ImgUrl1.Value;
            if (dropProType.SelectedValue != "")
                TSup.ProType = int.Parse(dropProType.SelectedValue);
            TSup.Place = this.txtPlace.Text;
            TSup.Brand = this.txtBrand.Text;
            TSup.Model = this.txtModel.Text;
            if (txtPrice.Text != string.Empty)
                TSup.Price = Convert.ToDecimal(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);
            int isEmail = 0;
            //读取指定节点下的value值
            string strFlag = Base.XMLProcess.Read("xml/EmailFlag.xml", "/Item/flag/mode[@type='supply']", "value");
            if (strFlag == "")
                strFlag = "us_supply";
            if (Restr.Trim().Equals(string.Empty)) //判断输入是否正确
            {
                RSup.Insert(TSup, out isEmail);
                if (isEmail > 0)
                {
                    //nspBase.Core.clsEmail.SendMail(strFlag, id);
                }
                ShowMessage("恭喜您，商机发布成功！", "Trade/SupplyManage.aspx?Verify=3&class=MenuLiCheck");

            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = Restr + "填写有误！";
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);

        }

    }

}

