using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace FineUIPro.Web.InterFace
{
    public partial class InterFaceEdit : PageBase
    {
        #region 定义项
        /// <summary>
        /// 接口配置主键
        /// </summary>
        public string InterFaceSetId
        {
            get
            {
                return (string)ViewState["InterFaceSetId"];
            }
            set
            {
                ViewState["InterFaceSetId"] = value;
            }
        }
        #endregion

        /// <summary>
        /// 接口配置编辑页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                ///权限
                this.GetButtonPower();
                this.InterFaceSetId = Request.Params["InterFaceSetId"];
                BLL.ConstValue.InitConstValueDropDownList(this.drpIsOpen, ConstValue.Group_0001, false);
                BLL.InterFaceSetService.InitReqMethodDropDownList(this.DrpUrlReqMethod,  false);
                BLL.InterFaceSetService.InitReqMethodDropDownList(this.DrpForUrlReqMethod,  false);
                BLL.InterFaceSetService.InitReqMethodDropDownList(this.DrpCallBackUrlReqMethod,  false);
                this.drpUnit.DataTextField = "UnitName";
                this.drpUnit.DataValueField = "UnitId";
                this.drpUnit.DataSource = BLL.UnitService.GetUnitDropDownList();
                drpUnit.DataBind();
                Funs.FineUIPleaseSelect(this.drpUnit);
                if (!string.IsNullOrEmpty(this.InterFaceSetId))
                {
                    this.drpUnit.SelectedValue = this.InterFaceSetId;
                }
                else
                {
                    var unit = BLL.CommonService.GetIsThisUnit();
                    if (unit != null)
                    {
                        this.drpUnit.SelectedValue = unit.UnitId;
                    }
                }

                var list = (from x in Funs.DB.Base_Unit
                            orderby x.UnitName
                            select new { x.UnitName, x.UnitId }).ToList();
                foreach (var item in list)
                {
                    this.rbAuthUnits.Items.Add(item.UnitName, item.UnitId);
                }

                if (!string.IsNullOrEmpty(this.InterFaceSetId))
                {
                    var FaceSet = BLL.InterFaceSetService.GetInterFaceSetById(this.InterFaceSetId);
                    if (FaceSet != null)
                    {
                        
                       
                        if (!string.IsNullOrEmpty(FaceSet.UnitId))
                        {
                            this.drpUnit.SelectedValue = FaceSet.UnitId;
                        }
                        this.txtFaceName.Text = FaceSet.InterFaceName;
                        this.txtFaceUrl.Text = FaceSet.InterFaceUrl;
                        this.txtFaceForUrl.Text = FaceSet.InterFaceForUrl;
                        this.txtCallBackUrl.Text=FaceSet.InterFaceCallBackUrl;
                        this.IsCallBack.Checked =(bool) FaceSet.IsCallBack;
                        if (!string.IsNullOrEmpty(FaceSet.AuthUnitIds))
                        {
                            this.drpAuthUnit.Values = FaceSet.AuthUnitIds.Split(',');
                        }
                       
                        this.txtRemarks.Text = FaceSet.TxtRemarks;
                        if (FaceSet.IsOpen.HasValue)
                        {
                            this.drpIsOpen.SelectedValue = Convert.ToString(FaceSet.IsOpen);
                        }
                        if (!string.IsNullOrEmpty(FaceSet.UrlReqMethod))
                        {
                            this.DrpUrlReqMethod.SelectedValue = FaceSet.UrlReqMethod;
                        }
                        if (!string.IsNullOrEmpty(FaceSet.ForUrlReqMethod))
                        {
                            this.DrpForUrlReqMethod.SelectedValue = FaceSet.ForUrlReqMethod;
                        }
                        if (!string.IsNullOrEmpty(FaceSet.CallBackUrlReqMethod))
                        {
                            this.DrpCallBackUrlReqMethod.SelectedValue = FaceSet.CallBackUrlReqMethod;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 加载页面
        /// </summary>
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //目标单位
            string AuthUnitId = string.Empty;
            string AuthUnitName = string.Empty;
            string UnitId = string.Empty;
            foreach (var item in this.drpAuthUnit.Values)
            {
                if (item != "")
                {
                    AuthUnitId += item + ",";
                }
            }
            foreach (var item in this.drpAuthUnit.Texts)
            {
                if (item != "")
                {
                    AuthUnitName += item + ",";
                   
                }
            }
            AuthUnitId = AuthUnitId.Substring(0, AuthUnitId.LastIndexOf(","));
            AuthUnitName = AuthUnitName.Substring(0, AuthUnitName.LastIndexOf(","));
            if (this.drpUnit.SelectedValue != Const._Null)
            {
                UnitId = this.drpUnit.SelectedValue;
            }
            Model.InterFaceSet newSet = new Model.InterFaceSet
            {
                InterFaceName = this.txtFaceName.Text.Trim(),
                InterFaceUrl = this.txtFaceUrl.Text.Trim(),
                InterFaceForUrl = this.txtFaceForUrl.Text.Trim(),
                IsOpen = Convert.ToBoolean(this.drpIsOpen.SelectedValue),
                TxtRemarks = this.txtRemarks.Text.Trim(),
                AuthUnitIds = AuthUnitId,
                AuthUnitName = AuthUnitName,
                UnitId = UnitId,
                IsCallBack = IsCallBack.Checked,
                InterFaceCallBackUrl = txtCallBackUrl.Text,
                UrlReqMethod=DrpUrlReqMethod.SelectedValue.Trim(),
                ForUrlReqMethod=DrpForUrlReqMethod.SelectedValue.Trim(),
                CallBackUrlReqMethod=DrpCallBackUrlReqMethod.SelectedValue.Trim()

            };
            if (string.IsNullOrEmpty(this.InterFaceSetId))
            {
                newSet.InterFaceSetId = SQLHelper.GetNewID(typeof(Model.InterFaceSet));
                BLL.InterFaceSetService.AddInterFaceSet(newSet);
                BLL.LogService.AddLog(this.CurrUser.UserId, "添加接口设置-" + this.txtFaceName.Text.Trim());
            }
            else
            {
                newSet.InterFaceSetId = this.InterFaceSetId;
                BLL.InterFaceSetService.UpdateInterFaceSet(newSet);
                BLL.LogService.AddLog(this.CurrUser.UserId, "修改接口设置" + this.txtFaceName.Text.Trim());
            }
            
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(null,this.CurrUser.UserId, BLL.Const.InterFaceSetMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion

        protected void IsCallBack_CheckedChanged(object sender, CheckedEventArgs e)
        {
            if (IsCallBack.Checked==true)
            {
                this.txtCallBackUrl.Hidden = false;
            }
            else
            {
                this.txtCallBackUrl.Hidden = true;
            }

        }
    }
}