using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HySoft.BaseCallCenter.Web.formmanage
{
    public partial class attributeedit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    txtotype.Value = Request.QueryString["action"].Trim();
                }
                if (!string.IsNullOrEmpty(Request.QueryString["entityid"]))
                {
                    txtentityid.Value = Request.QueryString["entityid"].Trim();
                    GetEntityInfo(Request.QueryString["entityid"].Trim());
                }
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    txtid.Value = Request.QueryString["id"].Trim();
                    //GetEntityInfo(Request.QueryString["entityid"].Trim());
                }
                BindList();
            }
        }

        private void GetEntityInfo(string entityid)
        {
            Model.T_Form_EntityInfo model = new Model.T_Form_EntityInfo();
            try
            {
                model = new BLL.T_Form_EntityInfo().GetModel(Convert.ToInt32(entityid));
                if (model != null)
                {
                    txtF_DBTableName.Text = model.F_DBTableName.Trim();
                }
            }
            catch(Exception ex){
            }
        }

        private void BindList()
        {
            selectF_ColumnNumber.Items.Clear();
            DataTable dt = new DataTable();
            try
            {
                if (txtentityid.Value != "")
                {
                    dt = new BLL.T_Form_EntityAttribute().GetList("F_EntityId=" + txtentityid.Value.Trim() + " order by F_ColumnNumber ").Tables[0];
                    int max = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        selectF_ColumnNumber.Items.Add(new ListItem(dt.Rows[i]["F_ColumnNumber"].ToString() + "-" + dt.Rows[i]["F_Name"].ToString(), dt.Rows[i]["F_ColumnNumber"].ToString()));
                        if (i == (dt.Rows.Count - 1))
                        {
                            max = Convert.ToInt32(dt.Rows[i]["F_ColumnNumber"].ToString());
                        }
                    }
                    if (Request.QueryString["action"].Trim() == "add")
                    {
                        selectF_ColumnNumber.Items.Add(new ListItem((max + 1).ToString(), (max + 1).ToString()));
                        selectF_ColumnNumber.Value = (max + 1).ToString();
                    }
                }
                else
                {
                    selectF_ColumnNumber.Items.Add(new ListItem("0", "0"));
                }
            }
            catch
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }

            selecttype.Items.Clear();
            try
            {
                selecttype.Items.Add(new ListItem("选择数据类型", ""));
                dt = new BLL.T_Form_AttributeTypes().GetList("F_IsShow=1").Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    selecttype.Items.Add(new ListItem(dt.Rows[i]["F_DBTypeName"].ToString() + "[" + dt.Rows[i]["F_TypeName"].ToString() + "]", dt.Rows[i]["F_TypeId"].ToString()));
                }
            }
            catch
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string res = "";

            try
            {
                string name = txtF_Name.Text.Trim();
                string field = txtF_DBFieldlName.Value.Trim();
                string basetable = txtF_DBTableName.Text.Trim();
                string fieldtype = selecttype.Value.Trim();
                string attributeid = txtid.Value.Trim();
                string opttype = txtotype.Value.Trim();
                DateTime dat = DateTime.Now;
                if (string.IsNullOrEmpty(name.Trim()))
                {
                    res = "属性名称不能为空";
                }
                if (string.IsNullOrEmpty(field.Trim()))
                {
                    res = "属性数据字段名不能为空";
                }
                if (string.IsNullOrEmpty(basetable.Trim()))
                {
                    res = "未获取属性的数据表名";
                }
                if (string.IsNullOrEmpty(fieldtype.Trim()))
                {
                    res = "请选择属性的数据类型";
                }


                Model.T_Form_EntityAttribute attributemodel = new Model.T_Form_EntityAttribute();
                #region 属性
                if (string.IsNullOrEmpty(attributeid.Trim()))
                {
                }
                else
                {
                    attributemodel.F_AttributeId = Convert.ToInt32(attributeid);
                }
                if(!string.IsNullOrEmpty(fieldtype.Trim()))
                {
                attributemodel.F_TypeId = Convert.ToInt32(fieldtype);
                attributemodel.F_DBTypeName = new BLL.T_Form_AttributeTypes().GetModel(Convert.ToInt32(fieldtype)).F_DBTypeName;
                }
                attributemodel.F_Name = name;
                attributemodel.F_DBFieldlName = "F_"+field;

                if (string.IsNullOrEmpty(txtF_MaxLength.Text.Trim()))
            {
                attributemodel.F_Length = null;
                attributemodel.F_MaxLength = null;
            }
            else
            {
                try
                {
                    attributemodel.F_Length = Convert.ToInt32(txtF_MaxLength.Text.Trim());
                    attributemodel.F_MaxLength = Convert.ToInt32(txtF_MaxLength.Text.Trim());
                }
                catch
                {
                    res= "属性最大长度格式不正确";
                }
            }
                attributemodel.F_IsNullable = true;
                attributemodel.F_EntityId = Convert.ToInt32(txtentityid.Value.Trim());
                attributemodel.F_DBTableName = basetable;
                attributemodel.F_DefaultValue = txtF_DefaultValue.Text.Trim();
                attributemodel.F_ColumnNumber = Convert.ToInt32(selectF_ColumnNumber.Value.Trim());
                attributemodel.F_ValidForUpdateAPI = true;
                attributemodel.F_ValidForReadAPI = true;
                attributemodel.F_ValidForCreateAPI = true;
                if (chkF_VisibleToPlatform.Checked)
                {
                    attributemodel.F_VisibleToPlatform = true;
                }
                else
                {
                    attributemodel.F_VisibleToPlatform = false;
                }
                attributemodel.F_IsPKAttribute = chkF_IsPKAttribute.Checked;
                attributemodel.F_IsCustomField = chkF_IsCustomField.Checked;
                attributemodel.F_DisplayMask = 1;
                attributemodel.F_IsSortAttribute = true;
                attributemodel.F_PrecisionValue = 0;
                attributemodel.F_IsIdentity = false;
                attributemodel.F_AppDefaultValue = 0;
                attributemodel.F_Locked = false;
                attributemodel.F_MinValue = 0;
                attributemodel.F_MaxValue = 0;
                if (chkIsList.Checked)
                {
                    attributemodel.F_LookupKey = txtF_LookupKey.Text.Trim();
                    attributemodel.F_LookupStyle = "";
                    attributemodel.F_IsLookup = true;
                }
                else
                {
                    attributemodel.F_LookupKey = "";
                    attributemodel.F_LookupStyle = "";
                    attributemodel.F_IsLookup = false;
                }
                attributemodel.F_Des = txtF_Remark.Text.Trim();

                #endregion
                int r = -4;
                if (opttype == "add")
                {
                    r = new BLL.T_Form_EntityAttribute().AddTran(attributemodel);
                }
                else if (opttype == "modify")
                {
                    //r = new BLL.T_Form_EntityAttribute().UpdateAttribute(attributemodel);
                }

                if (r == 0)
                {
                    res = "success";
                }
                else
                {
                    if (r == -2)
                    {
                        res = "实体属性数据表名称已存在";
                    }
                    else if (r == -3)
                    {
                        res = "实体属性名称已存在";
                    }
                    else if (r == -1)
                    {
                        res = "-1";
                    }
                    else
                    {
                        res = "" + r.ToString();
                    }
                }
            }
            catch
            {
                res = "创建属性异常";
            }
        }
    }
}