using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace BLL
{
    public class ConfirmFormService
    {
        public static string IsAgree(Object type, Object res)
        {
            string result = string.Empty;
            if (type.ToString().Equals(Const.ConfirmForm_Compile) || type.ToString().Equals(Const.ConfirmForm_ReCompile))
            {
                res = null;
            }
            if (res != null)
            {
                if (Convert.ToBoolean(res))
                {
                    result = "是";
                }
                else
                {
                    result = "否";
                }
            }
            return result;
        }
        /// 
        /// 根据工程签证工作委托单Id删除一个工程签证工作委托单信息
        /// 
        /// 工程签证工作委托单Id
        public static void DeleteConfirmForm(string confirmFormId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.CQMS_ConfirmForm confirmForm = db.CQMS_ConfirmForm.First(e => e.ConfirmFormId == confirmFormId);
            db.CQMS_ConfirmForm.DeleteOnSubmit(confirmForm);
            db.SubmitChanges();
        }
        /// 
        /// 增加工程签证工作委托单信息
        /// 
        /// 工程签证工作委托单实体
        public static void AddConfirmForm(Model.CQMS_ConfirmForm confirmForm)
        {
            Model.SGGLDB db = Funs.DB;
            Model.CQMS_ConfirmForm newConfirmForm = new Model.CQMS_ConfirmForm();
            newConfirmForm.ConfirmFormId = confirmForm.ConfirmFormId;
            newConfirmForm.ProjectId = confirmForm.ProjectId;
            newConfirmForm.SecretLevelId = confirmForm.SecretLevelId;
            newConfirmForm.ConfirmFormCode = confirmForm.ConfirmFormCode;
            newConfirmForm.Edition = confirmForm.Edition;
            newConfirmForm.UnitId = confirmForm.UnitId;
            newConfirmForm.ContractNo = confirmForm.ContractNo;
            newConfirmForm.WorkOrderIds = confirmForm.WorkOrderIds;
            newConfirmForm.TaskTheme = confirmForm.TaskTheme;
            newConfirmForm.CompileMan = confirmForm.CompileMan;
            newConfirmForm.CompileDate = confirmForm.CompileDate;
            newConfirmForm.State = confirmForm.State;
            db.CQMS_ConfirmForm.InsertOnSubmit(newConfirmForm);
            db.SubmitChanges();
        }
        /// 
        /// 修改工程签证工作委托单信息
        /// 
        /// 工程签证工作委托单实体
        public static void UpdateConfirmForm(Model.CQMS_ConfirmForm confirmForm)
        {
            Model.SGGLDB db = Funs.DB;
            Model.CQMS_ConfirmForm newConfirmForm = db.CQMS_ConfirmForm.First(e => e.ConfirmFormId == confirmForm.ConfirmFormId);
            newConfirmForm.SecretLevelId = confirmForm.SecretLevelId;
            newConfirmForm.ConfirmFormCode = confirmForm.ConfirmFormCode;
            newConfirmForm.Edition = confirmForm.Edition;
            newConfirmForm.UnitId = confirmForm.UnitId;
            newConfirmForm.ContractNo = confirmForm.ContractNo;
            newConfirmForm.WorkOrderIds = confirmForm.WorkOrderIds;
            newConfirmForm.TaskTheme = confirmForm.TaskTheme;
            newConfirmForm.State = confirmForm.State;
            db.SubmitChanges();
        }
        /// 
        /// 把状态转换代号为文字形式
        /// 
        /// 
        /// 
        /// 
        public static string ConvertState(object state)
        {
            if (state != null)
            {
                if (state.ToString() == BLL.Const.ConfirmForm_ReCompile)
                {
                    return "重报";
                }
                else if (state.ToString() == BLL.Const.ConfirmForm_Compile)
                {
                    return "编制";
                }
                else if (state.ToString() == BLL.Const.ConfirmForm_Audit1)
                {
                    return "分包项目经理审核";
                }
                else if (state.ToString() == BLL.Const.ConfirmForm_Audit2)
                {
                    return "总包施工经理审核";
                }
                else if (state.ToString() == BLL.Const.ConfirmForm_Complete)
                {
                    return "审批完成";
                }
            }
            return "";
        }
        /// 
        /// 把状态转换代号为文字形式
        /// 
        /// 
        /// 
        /// 
        public static string GetContents(object ConfirmFormId)
        {
            string contents = string.Empty;
            if (ConfirmFormId != null)
            {
                var details = from x in Funs.DB.CQMS_ConfirmFormDetail where x.ConfirmFormId == ConfirmFormId.ToString() select x;
                foreach (var item in details)
                {
                    contents += item.JobContent + ";";
                }
                if (!string.IsNullOrEmpty(contents))
                {
                    contents = contents.Substring(0, contents.Length - 1);
                }
            }
            return contents;
        }
        /// 
        /// 根据工程签证工作委托单Id获取一个工程签证工作委托单信息
        /// 
        /// 工程签证工作委托单Id
        /// 一个工程签证工作委托单实体
        public static Model.CQMS_ConfirmForm GetConfirmFormByConfirmFormId(string confirmFormId)
        {
            return Funs.DB.CQMS_ConfirmForm.FirstOrDefault(x => x.ConfirmFormId == confirmFormId);
        }
        public static void Init(FineUIPro.DropDownList dropName, string state, bool isShowPlease)
        {
            dropName.DataValueField = "Value";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetDHandleTypeByState(state);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        /// 
        /// 根据状态选择下一步办理类型
        /// 
        /// 
        /// 
        public static ListItem[] GetDHandleTypeByState(string state)
        {
            if (state == Const.ConfirmForm_Compile || state == Const.ConfirmForm_ReCompile)  //无是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("分包项目经理审核", Const.ConfirmForm_Audit1);
                return lis;
            }
            else if (state == Const.ConfirmForm_Audit1)//有是否同意
            {
                ListItem[] lis = new ListItem[2];
                lis[0] = new ListItem("总包施工经理审核", Const.ConfirmForm_Audit2);//是 加载
                lis[1] = new ListItem("重新编制", Const.ConfirmForm_ReCompile);//是 加载
                return lis;
            }
            else if (state == Const.ConfirmForm_Audit2)//有是否同意
            {
                ListItem[] lis = new ListItem[2];
                lis[0] = new ListItem("审批完成", Const.ConfirmForm_Complete);//是 加载
                lis[1] = new ListItem("重新编制", Const.ConfirmForm_ReCompile);//是 加载
                return lis;
            }
            else
                return null;
        }
        #region  结算单加载确认单列表
        public static void InitConfirmFormList(FineUIPro.DropDownList dropName, string projectId, string unitId, bool isShowPlease)
        {
            dropName.DataValueField = "ConfirmFormId";
            dropName.DataTextField = "ConfirmFormCode";
            dropName.DataSource = GetConfirmFormList(projectId, unitId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        public static List GetConfirmFormList(string projectId, string unitId)
        {
            var selectedList = (from x in Funs.DB.CQMS_Statement where x.ProjectId == projectId select x.ConfirmFormId).ToList();
            if (!string.IsNullOrEmpty(unitId))
            {
                return (from x in Funs.DB.CQMS_ConfirmForm where x.ProjectId == projectId && !selectedList.Contains(x.ConfirmFormId) && x.UnitId == unitId && x.State == BLL.Const.ConfirmForm_Complete select x).ToList();
            }
            else
            {
                return (from x in Funs.DB.CQMS_ConfirmForm where x.ProjectId == projectId && !selectedList.Contains(x.ConfirmFormId) && x.State == BLL.Const.ConfirmForm_Complete select x).ToList();
            }
        }
        #endregion
        #region  费用报审表加载确认单列表
        public static void InitConfirmFormList2(FineUIPro.DropDownList dropName, string projectId, string unitId, bool isShowPlease)
        {
            dropName.DataValueField = "ConfirmFormId";
            dropName.DataTextField = "ConfirmFormCode";
            dropName.DataSource = GetConfirmFormList2(projectId, unitId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        public static List GetConfirmFormList2(string projectId, string unitId)
        {
            var selectedList = (from x in Funs.DB.CQMS_CostReport where x.ProjectId == projectId select x.ConfirmFormIds).ToList();
            List list = new List();
            foreach (var item in selectedList)
            {
                list.AddRange(item.Split(',').ToList());
            }
            if (!string.IsNullOrEmpty(unitId))
            {
                return (from x in Funs.DB.CQMS_ConfirmForm where x.ProjectId == projectId && !list.Contains(x.ConfirmFormId) && x.UnitId == unitId && x.State == BLL.Const.ConfirmForm_Complete select x).ToList();
            }
            else
            {
                return (from x in Funs.DB.CQMS_ConfirmForm where x.ProjectId == projectId && !list.Contains(x.ConfirmFormId) && x.State == BLL.Const.ConfirmForm_Complete select x).ToList();
            }
        }
        #endregion
    }
}