using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foresight.Fleet.Services.Inspection;
using Foresight.Fleet.Services.User;
using IronIntel.Contractor;
using IronIntel.Contractor.Site;

public partial class InspectionReport : InspectionBasePage
{
    protected string ReportID;
    protected bool TeamIntelligence = false;
    public bool ReportReadonly = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserToken();
        if (!CheckLoginSession())
        {
            RedirectToLoginPage();
        }
        else
        {
            string methodName = Request.Form["MethodName"];
            if (!string.IsNullOrEmpty(methodName))
            {
                ProcessRequest(methodName);
            }
            else if (!IsPostBack)
            {
                this.Title = PageTitle;
                ReportID = Request.Params["rid"];
                if (!string.IsNullOrEmpty(ReportID))
                {
                    object ii = GetInspectItem(ReportID);
                    if (ii == null)
                    {//没有权限或ID不正确
                        Response.Write("You have no right to access the report.");
                        Response.End();
                    }
                    if (ii is TeamInspectItem)
                        TeamIntelligence = true;
                    ReportReadonly = Helper.IsTrue(Request.Params["ro"]);

                    if (!ReportReadonly)
                    {
                        var user = GetCurrentUser();
                        if (user.UserType == IronIntel.Contractor.Users.UserTypes.Common)
                        {
                            var client = FleetServiceClientHelper.CreateClient<PermissionProvider>();
                            Tuple<Feature, Permissions>[] pmss = client.GetUserPermissions(SystemParams.CompanyID, user.IID);

                            if (pmss.Length > 0)
                            {
                                Tuple<Feature, Permissions> reportpms = null;
                                if (TeamIntelligence)
                                    reportpms = pmss.FirstOrDefault(m => m.Item1.Id == Feature.TEAM_REPORTS);
                                else
                                    reportpms = pmss.FirstOrDefault(m => m.Item1.Id == Feature.INSPECTION_REPORTS);

                                if (reportpms == null || reportpms.Item2 == Permissions.ReadOnly)
                                    ReportReadonly = true;
                            }
                        }
                    }
                }
            }
        }
    }

    protected override bool CanDirectAccess
    {
        get
        {
            return true;
        }
    }
}