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

namespace FineUIPro.Web.SES
{
    public partial class ContractorQueCopy : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
               
                

               
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string foId = Request.Params["Id"];

            if (txtFoNo.Text.Trim() != "")
            {
                var fo=BLL.SESRelatedDataService.GetSESRelatedDataByFoNo(txtFoNo.Text.Trim());
                if (fo != null)
                {
                    var queList = from x in Funs.DB.EMC_Contractor_Qualification where x.FoId == fo.ID select x;
                    foreach (var que in queList)
                    {
                        Model.EMC_Contractor_Qualification newQue = new Model.EMC_Contractor_Qualification();
                        string newCode= SQLHelper.GetNewID(typeof(Model.EMC_Contractor_Qualification));
                        newQue.FoId = Convert.ToInt32(foId);
                        newQue.QueId = newCode;
                        newQue.QueType = que.QueType;
                        newQue.QueName = que.QueName;
                        newQue.AttachUrl = que.AttachUrl;
                        newQue.QueDef = que.QueDef;
                        newQue.LimitStartDate = que.LimitStartDate;
                        newQue.LimitEndDate = que.LimitEndDate;
                        newQue.Remark = que.Remark;
                        Funs.DB.EMC_Contractor_Qualification.InsertOnSubmit(newQue);
                        Funs.DB.SubmitChanges();

                        var att = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == que.QueId && x.MenuId == BLL.Const.ContractorQualificationMenuId);
                        if (att != null)
                        {
                            Model.AttachFile newAtt = new Model.AttachFile();
                            newAtt.AttachFileId = SQLHelper.GetNewID(typeof(Model.AttachFile));
                            newAtt.ToKeyId = newCode;
                            newAtt.AttachSource=att.AttachSource;
                            newAtt.AttachUrl = att.AttachUrl;
                            newAtt.MenuId = att.MenuId;
                            Funs.DB.AttachFile.InsertOnSubmit(newAtt);
                            Funs.DB.SubmitChanges();
                        }
                    }
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                    ShowNotify(Resources.Lan.SaveSuccessfully, MessageBoxIcon.Success);
                }
                else
                {
                    Alert.ShowInParent("Please select FoNO Error!");
                    return;
                }
            }
            else
            {
                Alert.ShowInParent("Please select FoNO!");
                return;
            }
        }
    }
}