@model DailyReport
@{
    double ASMAvailablePct = 0;
    double EPPAvailablePct = 0;
    double HTRAvailablePct = 0;

    int ASMSLL = 0;
    int HTRSLL = 0;

    int ASMUnloadTemps = 0;
    int HTRUnloadTemps = 0;

    int reportIndex = (int)DateTime.Now.DayOfWeek;

    int numberOfDaysInWeek = Model.CurrentWeek.OutsByDay.Count;

    ManualReportEntries rpt = Model.ManualReportEntries;

    string myClass;
}

@{
    ViewData["Title"] = "Daily Passdown | Mesa Reporting Services";
}

<div aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
        <li class="breadcrumb-item"><a asp-area="" asp-controller="ProductionReport" asp-action="Index">Production Reports</a></li>
        <li class="breadcrumb-item active" aria-current="page">Production Passdown Report</li>
    </ol>
</div>
<br />

<div class="text-center">
    <h2>Daily Report</h2>
    <br /><br />
</div>

<button class="btn btn-outline-dark float-start dailyReportTable" onclick="toggleWeek()">View Previous Week</button>
<button class="btn btn-outline-dark float-end dailyReportTable hidden" onclick="toggleWeek()">View Current Week</button>
<br /><br />

<div class="table-responsive dailyReportTable">
    <partial name="_DailyReportPartial" model="@Model.CurrentWeek"/>
</div>

<div class="table-responsive dailyReportTable hidden">
    <partial name="_DailyReportPartial" model="@Model.PreviousWeek" />
</div>

<br />
<h5>Daily Target Summary</h5>
<ul>
    <li>Operator HC: (Days/Nights) @rpt.OperatorHeadcountDays/@rpt.OperatorHeadcountNights</li>
    <li>Total Call Outs: (Days/Nights) @rpt.OperatorCallOutsDays/@rpt.OperatorCallOutsNights</li>
    <li>Engineering HC: @rpt.EngineeringHeadcountDays/@rpt.EngineeringHeadcountNights</li>
    <li>Total Call Outs: (Days/Nights) @rpt.EngineeringCallOutsDays/@rpt.EngineeringCallOutsNights</li>
    <li>Maintenance HC: @rpt.MaintenanceHeadcountDays/@rpt.MaintenanceHeadcountNights</li>
    <li>Total Call Outs: (Days/Nights) @rpt.MaintenanceCallOutsDays/@rpt.MaintenanceCallOutsNights</li>
</ul>
<br />
<ul>
    @{
        int bottleChanges = 0;
        int dailyPartChanges = 0;
        int weeklyPartChanges = 0;

        string bottle = "";
        string daily = "";
        string weekly = "";

        if (!string.IsNullOrEmpty(rpt.BottleChanges))
        {
            bottle = string.Join(", ", rpt.BottleChanges.Split(','));
            bottleChanges = @rpt.BottleChanges.Split(',').Length;
        }

        if (!string.IsNullOrEmpty(rpt.DailyPartChanges))
        {
            daily = string.Join(", ", rpt.DailyPartChanges.Split(','));
            dailyPartChanges = @rpt.DailyPartChanges.Split(',').Length;
        }
            

        if (!string.IsNullOrEmpty(rpt.WeeklyPartChanges))
        {
            weekly = string.Join(", ", rpt.WeeklyPartChanges.Split(','));
            weeklyPartChanges = @rpt.WeeklyPartChanges.Split(',').Length;
        }
            

    }
    <li>Bottle Change (@bottleChanges): @bottle</li>
    <li>Daily Part Changes (@dailyPartChanges): @daily</li>
    <li>Weekly Part Changes (@weeklyPartChanges): @weekly</li>
    <li>Red Hot Work Orders (@Model.CurrentHotWORunning.Count()): @string.Join(", ", Model.CurrentHotWORunning)</li>
</ul>
<br />
<a class="btn btn-light" asp-controller="ProductionReport" asp-action="EditDailyReport">Edit</a>
<br /><br />
<h5>Current Reactors Down(@Model.ToolEvents.Where(x => x.IsInProduction == false).Count()):</h5>
<div class="row">
    <div>
        @{
            List<ToolEventView> asmTools = Model.ToolEvents.Where(x => x.IsInProduction == false && x.Type.Contains("ASM")).ToList(); 
        }
        ASM(@asmTools.Count())
        <table class="table table-sm text-center">
            <thead>
                <tr>
                    <th scope="col" style="width: 7%">Reactor</th>
                    <th scope="col" style="width: 7%">Owner</th>
                    <th scope="col" style="width: 20%" class="text-start">Category Description</th>
                    <th scope="col" style="width: 28%" class="text-start">Issue</th>
                    <th scope="col" style="width: 10%">Downtime</th>
                    <th scope="col" style="width: 28%" class="text-start">Most Recent Comment</th>
                </tr>
            </thead>
            <tbody>
                @foreach (ToolEventView tool in asmTools)
                {
                    string owner = "";
                    string hot = "";

                    if (Model.ToolStatesByOwner["Maintenance"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Maint";
                    else if (Model.ToolStatesByOwner["Engineering"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Eng";
                    else
                        owner = "Prod";

                    if (tool.Downtime > 12)
                        myClass = "tableDowntime";
                    else
                        myClass = "";

                    <tr class="@myClass">

                        @if (Model.CurrentHotWORunning.Contains("R" + tool.Reactor))
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@tool.Reactor</td>
                        <td>@owner</td>
                        <td class="text-start">@tool.MostRecentEvent.CAT_PROB_DESC</td>
                        <td class="text-start">@tool.MostRecentEvent.REASON</td>
                        <td>@string.Format("{0:##,###.##}", tool.Downtime)</td>
                        <td class="text-start">@tool.MostRecentEvent.COMMENT</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div>
        @{
            List<ToolEventView> eppTools = Model.ToolEvents.Where(x => x.IsInProduction == false && x.Type.Contains("EPP")).ToList();
        }
        EPP(@eppTools.Count())
        <table class="table table-sm text-center">
            <thead>
                <tr>
                    <th scope="col" style="width: 7%">Reactor</th>
                    <th scope="col" style="width: 7%">Owner</th>
                    <th scope="col" style="width: 20%" class="text-start">Category Description</th>
                    <th scope="col" style="width: 28%" class="text-start">Issue</th>
                    <th scope="col" style="width: 10%">Downtime</th>
                    <th scope="col" style="width: 28%" class="text-start">Most Recent Comment</th>
                </tr>
            </thead>
            <tbody>
                @foreach (ToolEventView tool in eppTools)
                {
                    string owner = "";

                    if (Model.ToolStatesByOwner["Maintenance"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Maint";
                    else if (Model.ToolStatesByOwner["Engineering"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Eng";
                    else
                        owner = "Prod";

                    if (tool.Downtime > 12)
                        myClass = "tableDowntime";
                    else
                        myClass = "";

                    <tr class="@myClass">

                        @if (Model.CurrentHotWORunning.Contains("R" + tool.Reactor))
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@tool.Reactor</td>
                        <td>@owner</td>
                        <td class="text-start">@tool.MostRecentEvent.CAT_PROB_DESC</td>
                        <td class="text-start">@tool.MostRecentEvent.REASON</td>
                        <td>@string.Format("{0:##,###.##}", tool.Downtime)</td>
                        <td class="text-start">@tool.MostRecentEvent.COMMENT</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div>
        @{
            List<ToolEventView> htrTools = Model.ToolEvents.Where(x => x.IsInProduction == false && x.Type.Contains("HTR")).ToList();
        }
        HTR(@htrTools.Count())
        <table class="table table-sm text-center">
            <thead>
                <tr>
                    <th scope="col" style="width: 7%">Reactor</th>
                    <th scope="col" style="width: 7%">Owner</th>
                    <th scope="col" style="width: 20%" class="text-start">Category Description</th>
                    <th scope="col" style="width: 28%" class="text-start">Issue</th>
                    <th scope="col" style="width: 10%">Downtime</th>
                    <th scope="col" style="width: 28%" class="text-start">Most Recent Comment</th>
                </tr>
            </thead>
            <tbody>
                @foreach (ToolEventView tool in htrTools)
                {
                    string owner = "";

                    if (Model.ToolStatesByOwner["Maintenance"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Maint";
                    else if (Model.ToolStatesByOwner["Engineering"].Contains(tool.MostRecentEvent.REACT_MODE))
                        owner = "Eng";
                    else
                        owner = "Prod";

                    if (tool.Downtime > 12)
                        myClass = "tableDowntime";
                    else
                        myClass = "";

                    <tr class="@myClass">

                        @if (Model.CurrentHotWORunning.Contains("R" + tool.Reactor))
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@tool.Reactor</td>
                        <td>@owner</td>
                        <td class="text-start">@tool.MostRecentEvent.CAT_PROB_DESC</td>
                        <td class="text-start">@tool.MostRecentEvent.REASON</td>
                        <td>@string.Format("{0:##,###.##}", tool.Downtime)</td>
                        <td class="text-start">@tool.MostRecentEvent.COMMENT</td>
                    </tr>
                }
            </tbody>
        </table>
        <p> *Rows highlighted in orange have been down for more than 12 hours</p>
    </div>
</div>
<ul>
    @{
        List<string> engTools = Model.ToolEvents
        .Where(x => x.MostRecentEvent.REACT_MODE == "ENGINEERING_DEVELOPMENT")
        .Select(x => x.Reactor).ToList();
    }
    <li>Application ENG (@engTools.Count()):
        @if (engTools.Count() > 0)
        {
            <ul>
                <li>@string.Join(',', engTools)</li>
            </ul>
        }
    </li>
    <li>Reactors (Capacity @(Model.NumberOfToolsWaferSize6IN + Model.NumberOfToolsWaferSize8IN))
        <ul>
            <li>150mm - @Model.NumberOfToolsWaferSize6IN</li>
            <li>200mm - @Model.NumberOfToolsWaferSize8IN</li>
        </ul>
    </li>
    <li>Scheduled Reactors (@(Model.NumberOfToolsWaferSize6INScheduled + Model.NumberOfToolsWaferSize8INScheduled)):
        <ul>
            <li>150mm - @Model.NumberOfToolsWaferSize6INScheduled</li>
            <li>200mm - @Model.NumberOfToolsWaferSize8INScheduled</li>
        </ul>
    </li>
    <li>Dual Layer Reactors
        <ul>
            <li>ASM - @(string.Join(',', Model.DualLayerReactors["ASM"]))</li>
            <li>HTR - @(string.Join(',', Model.DualLayerReactors["HTR"]))</li>
            <li>EpiPro - @(string.Join(',', Model.DualLayerReactors["EPP"]))</li>
        </ul>
    </li>
    <li>Metrology Down (@Model.MetrologyEvents.Count()):
        @if (@Model.MetrologyEvents.Count() > 0)
        {
            <ul>
                @foreach (ToolEvent tool in @Model.MetrologyEvents)
                {
                    <li>@tool.TOOL_ID</li>
                }
            </ul>
        }
    </li>
    <li>Cleans (@Model.CleanEvents.Count()):
        @if (@Model.CleanEvents.Count() > 0)
        {
            <ul>
                @foreach (ToolEvent tool in @Model.CleanEvents)
                {
                    <li>@tool.TOOL_ID</li>
                }
            </ul>
        }
    </li>
</ul>
<br /><br />
<div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th scope="col"></th>
                <th scope="col">Monday</th>
                <th scope="col">Tuesday</th>
                <th scope="col">Wednesday</th>
                <th scope="col">Thursday</th>
                <th scope="col">Friday</th>
                <th scope="col">Saturday</th>
                <th scope="col">Sunday</th>
                <th scope="col">WTD</th>
                <th scope="col">Daily / Weekly</th>
            </tr>
            <tr>
                <th scope="col"></th>
                @for (int i = 0; i < 7; i++)
                {
                    <th scope="col">@Model.StartDate.AddDays(i).ToString("MM/dd/yyyy")</th>
                }
                <th scope="col">Actual</th>
                <th scope="col">Target</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td scope="row">ASMs Uptime</td>
                @for (int i = 0; i < 7; i++)
                {
                    asmTools = Model.ToolEvents.Where(x => x.Type.Contains("ASM")).ToList();

                    double availablePct = 0;

                    if (i < asmTools[0].Uptime.Count)
                    {
                        for (int j = 0; j < asmTools.Count; j++)
                        {
                            availablePct += asmTools[j].Uptime[i].UptimePercentage;
                        }

                        if (availablePct / asmTools.Count < .82)
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@string.Format("{0:P2}", availablePct / asmTools.Count)</td>

                        ASMAvailablePct += availablePct / asmTools.Count;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@string.Format("{0:P2}", ASMAvailablePct / numberOfDaysInWeek)</td>
                <td>82%</td>
            </tr>
            <tr>
                <td scope="row">EPPs Uptime</td>
                @for (int i = 0; i < 7; i++)
                {
                    eppTools = Model.ToolEvents.Where(x => x.Type.Contains("EPP")).ToList();

                    double availablePct = 0;

                    if (i < eppTools[0].Uptime.Count)
                    {
                        for (int j = 0; j < eppTools.Count; j++)
                        {
                            availablePct += eppTools[j].Uptime[i].UptimePercentage;
                        }

                        if (availablePct / eppTools.Count < .60)
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@string.Format("{0:P2}", availablePct / eppTools.Count)</td>

                        EPPAvailablePct += availablePct / eppTools.Count;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@string.Format("{0:P2}", EPPAvailablePct / numberOfDaysInWeek)</td>
                <td>60%</td>
            </tr>
            <tr>
                <td scope="row">HTRs Uptime</td>
                @for (int i = 0; i < 7; i++)
                {
                    htrTools = Model.ToolEvents.Where(x => x.Type.Contains("HTR")).ToList();

                    double availablePct = 0;

                    if (i < htrTools[0].Uptime.Count)
                    {
                        for (int j = 0; j < htrTools.Count; j++)
                        {
                            availablePct += htrTools[j].Uptime[i].UptimePercentage;
                        }

                        if (availablePct / htrTools.Count < .78)
                            myClass = "table-danger text-danger";
                        else
                            myClass = "";

                        <td class="@myClass">@string.Format("{0:P2}", availablePct / htrTools.Count)</td>

                        HTRAvailablePct += availablePct / htrTools.Count;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@string.Format("{0:P2}", HTRAvailablePct / numberOfDaysInWeek)</td>
                <td>78%</td>
            </tr>
            <tr>
                <td scope="row">ASMs SLL Tool Count</td>
                @for (int i = 0; i < 7; i++)
                {
                    if (i < Model.SLLTools.Count())
                    {
                        <td>@Model.SLLTools[i].ASM</td>

                        ASMSLL += @Model.SLLTools[i].ASM;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@(Math.Round((double)ASMSLL / (double)numberOfDaysInWeek))</td>
                <td>0</td>
            </tr>
            <tr>
                <td scope="row">HTRs SLL Tool Count</td>
                @for (int i = 0; i < 7; i++)
                {
                    int index = i == 6 ? 0 : i + 1;

                    if (i < Model.SLLTools.Count())
                    {
                        <td>@Model.SLLTools[i].HTR</td>

                        HTRSLL += @Model.SLLTools[i].HTR;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@(Math.Round((double)HTRSLL / (double)numberOfDaysInWeek))</td>
                <td>0</td>
            </tr>
            <tr>
                <td scope="row">ASMs &lt;700C (Unload Temps)</td>
                @for (int i = 0; i < 7; i++)
                {
                    if (i < Model.UnloadTempsByDay.Count())
                    {
                        <td>@Model.UnloadTempsByDay[i].ASMUnloadsBelow700</td>

                        ASMUnloadTemps += @Model.UnloadTempsByDay[i].ASMUnloadsBelow700;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@(Math.Round((double)ASMUnloadTemps / (double)numberOfDaysInWeek))</td>
                <td>0</td>
            </tr>
            <tr>
                <td scope="row">HTRs &lt;700C (Unload Temps)</td>
                @for (int i = 0; i < 7; i++)
                {
                    if (i < Model.UnloadTempsByDay.Count())
                    {
                        <td>@Model.UnloadTempsByDay[i].HTRUnloadsBelow700</td>

                        HTRUnloadTemps += @Model.UnloadTempsByDay[i].HTRUnloadsBelow700;
                    }
                    else
                    {
                        <td></td>
                    }
                }
                <td>@(Math.Round((double)HTRUnloadTemps / (double)numberOfDaysInWeek))</td>
                <td>0</td>
            </tr>
        </tbody>
    </table>
</div>