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 定义项
        /// 
        /// 接口配置主键
        /// 
        public string InterFaceSetId
        {
            get
            {
                return (string)ViewState["InterFaceSetId"];
            }
            set
            {
                ViewState["InterFaceSetId"] = value;
            }
        }
        #endregion
        /// 
        /// 接口配置编辑页面
        /// 
        /// 
        /// 
        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();
                this.drpAuthUnit.DataTextField = "UnitName";
                this.drpAuthUnit.DataValueField = "UnitId";
                this.drpAuthUnit.DataSource = BLL.UnitService.GetUnitDropDownList();
                drpAuthUnit.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;
                    }
                }
                 
                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.SelectedValueArray = FaceSet.AuthUnitIds.Split(',');
                        }
                       
                        this.txtRemarks.Text = FaceSet.TxtRemarks;
                        txtRequestJsonBody.Text = FaceSet.RequestJsonBody;
                        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;
                        }
                        if (FaceSet.IsSingleRequest != null) cbIsSingleRequest.Checked = (bool)FaceSet.IsSingleRequest;
                        cbIsSingleRequest_OnCheckedChanged(null, null);
                    }
                }
                else
                {
                    cbIsSingleRequest_OnCheckedChanged(null, null);
                }
            }
        }
        /// 
        /// 加载页面
        /// 
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //目标单位
            string AuthUnitId = string.Empty;
            string AuthUnitName = string.Empty;
            string UnitId = string.Empty; 
            AuthUnitId = string.Join(",", drpAuthUnit.SelectedValueArray);
            AuthUnitName = UnitService.getUnitNamesUnitIds(AuthUnitId);
            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(),
                IsSingleRequest = cbIsSingleRequest.Checked,
                RequestJsonBody = txtRequestJsonBody.Text.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 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(string.Empty, this.CurrUser.UserId, BLL.Const.UserMenuId);
            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;
            }
        }
        protected void cbIsSingleRequest_OnCheckedChanged(object sender, CheckedEventArgs e)
        {
            if (cbIsSingleRequest.Checked == true)
            {
                this.rowFace.Hidden = false;
                this.rowFaceFor.Hidden = true;
                this.rowCallBack.Hidden = true;
                txtRequestJsonBody.Hidden = false;
            }
            else
            {
                this.rowFace.Hidden = false;
                this.rowFaceFor.Hidden = false;
                this.rowCallBack.Hidden = false;
                txtRequestJsonBody.Hidden = true;
            }
        }
    }
}