@model YieldStatistics

@{
    int totalWafersOut = 0;
    int totalCustomerScrap = 0;
    int totalManufacturingScrap = 0;
    int totalProdScrap = 0;
    int totalYieldedWafersOut = 0;
    int deltaToCommit = 0;
    int deltaToPlan = 0;
    float totalYield = 0f;

    string myClass;

    int numberOfDaysInWeek = Model.OutsByDay.Count;
    int yieldOutDays = Model.IsCurrentWeek ? Model.OutsByDay.Count - 1 : Model.OutsByDay.Count;
}


<table class="table table-bordered">
    <thead>
        <tr>
            <th scope="col" rowspan="2" class="text-center align-middle">SI Operations</th>
            @for (int i = 0; i < 7; i++)
            {
                <th scope="col" class="text-center">@Model.StartDate.AddDays(i).ToString("MM/dd/yyyy")</th>
            }
            <th scope="col" rowspan="2" class="text-center align-middle">Weekly Total</th>
            <th scope="col" rowspan="2" class="text-center align-middle">QTD Total</th>
            <th scope="col" rowspan="2" class="text-center align-middle">Comment</th>
        </tr>
        <tr>
            <th scope="col" class="text-center">Monday</th>
            <th scope="col" class="text-center">Tuesday</th>
            <th scope="col" class="text-center">Wednesday</th>
            <th scope="col" class="text-center">Thursday</th>
            <th scope="col" class="text-center">Friday</th>
            <th scope="col" class="text-center">Saturday</th>
            <th scope="col" class="text-center">Sunday</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td scope="row">Committed Target to meet Shipment Requirements</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyPlanWafers)</td>
            <td class="text-center">@string.Format("{0:##,###}", Model.DailyPlanWafers * 7)</td>
            <td></td>
            <td>Number updated quarterly</td>
        </tr>
        <tr>
            <td scope="row">Actual Reactor Out</td>
            @for (int i = 0; i < 7; i++)
            {

                if (i < numberOfDaysInWeek)
                {
                    int reactorMoves = Model.OutsByDay[i].TotalWafers;

                    <td class="text-center">@reactorMoves</td>

                    totalWafersOut += reactorMoves;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@totalWafersOut</td>
            <td class="text-center">@string.Format("{0:###,###}", Model.QTDOutsAndScrap.Outs)</td>
            <td>Before Scrap</td>
        </tr>
        <tr>
            <td scope="row" id="expandYield">
                Actual Yielded Wafers Out &nbsp;&nbsp;&nbsp;
                <button class="btn btn-default" onclick="expandYield()">
                    <i class="fa-solid fa-plus-circle" id="yieldIcon"></i>
                </button>
            </td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int yieldedOuts = Model.OutsByDay[i].TotalWafers - Model.ScrapByDay[i].TOT_REJ_WFRS - Model.ScrapByDay[i].TW_PROD;

                    <td class="text-center">@yieldedOuts</td>

                    totalYieldedWafersOut += yieldedOuts;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@totalYieldedWafersOut</td>
            <td class="text-center">@string.Format("{0:###,###}", Model.QTDOutsAndScrap.Outs - Model.QTDOutsAndScrap.CustomerScrap - Model.QTDOutsAndScrap.ManufacturingScrap - Model.QTDOutsAndScrap.ProductionScrap)</td>
            <td>After Scrap</td>
        </tr>
        <tr class="yield hidden">
            @{
                int index = numberOfDaysInWeek - 1;
                int modifiedYieldedOuts = 0;
                if (Model.IsCurrentWeek)
                    modifiedYieldedOuts = totalYieldedWafersOut - (Model.OutsByDay[index].TotalWafers - Model.ScrapByDay[index].TOT_REJ_WFRS - Model.ScrapByDay[index].TW_PROD);
                else
                    modifiedYieldedOuts = totalYieldedWafersOut;

                int averageYieldedOuts = yieldOutDays == 0 ? 0 : modifiedYieldedOuts / yieldOutDays;
            }
            <td scope="row" colspan="11" id="expandYield" class="text-center">Yielded Wafers Out Daily Average: @(averageYieldedOuts)</td>
        </tr>
        <tr class="yield hidden">
            <td scope="row">Customer Scrap</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int custScrap = Model.ScrapByDay[i].TOT_REJ_CUST;

                    <td class="text-center">@custScrap</td>

                    totalCustomerScrap += custScrap;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@totalCustomerScrap</td>
            <td class="text-center">@string.Format("{0:###,###}", Model.QTDOutsAndScrap.CustomerScrap)</td>
            <td></td>
        </tr>
        <tr class="yield hidden">
            <td scope="row">Manufacturing Scrap</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int manuScrap = Model.ScrapByDay[i].TOT_REJ_MANU;

                    <td class="text-center">@manuScrap</td>

                    totalManufacturingScrap += manuScrap;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@totalManufacturingScrap</td>
            <td class="text-center">@string.Format("{0:###,###}", Model.QTDOutsAndScrap.ManufacturingScrap)</td>
            <td></td>
        </tr>
        <tr class="yield hidden">
            <td scope="row">Production Scrap</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int prodScrap = Model.ScrapByDay[i].TW_PROD;

                    <td class="text-center">@prodScrap</td>

                    totalProdScrap += prodScrap;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@totalProdScrap</td>
            <td class="text-center">@string.Format("{0:###,###}", Model.QTDOutsAndScrap.ProductionScrap)</td>
            <td></td>
        </tr>
        <tr class="yield hidden">
            <td scope="row">Yield</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    float yield = ((float)Model.OutsByDay[i].TotalWafers - (float)Model.ScrapByDay[i].TOT_REJ_WFRS) / (float)Model.OutsByDay[i].TotalWafers;

                    <td class="text-center">@(string.Format("{0:P2}", yield))</td>

                    totalYield += yield;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@(string.Format("{0:P2}", (float)(totalWafersOut - totalCustomerScrap - totalManufacturingScrap) / (float)totalWafersOut))</td>
            <td class="text-center">@string.Format("{0:P2}", (float)(Model.QTDOutsAndScrap.Outs - Model.QTDOutsAndScrap.ManufacturingScrap - Model.QTDOutsAndScrap.CustomerScrap) / (float)Model.QTDOutsAndScrap.Outs)</td>
            <td>After Scrap</td>
        </tr>
        <tr>
            <td scope="row">Delta to commit</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int dayDelta = Model.OutsByDay[i].TotalWafers - Model.ScrapByDay[i].TOT_REJ_WFRS - Model.ScrapByDay[i].TW_PROD - Model.DailyWafersForQTR;

                    if (dayDelta < 0)
                        myClass = "table-danger text-danger";
                    else
                        myClass = "";

                    <td class="text-center @myClass">@dayDelta</td>

                    deltaToCommit += dayDelta;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@deltaToCommit</td>
            <td></td>
            <td>Difference to commitment</td>
        </tr>
        <tr>
            <td scope="row">Delta to the Plan</td>
            @for (int i = 0; i < 7; i++)
            {
                if (i < numberOfDaysInWeek)
                {
                    int dayDelta = Model.OutsByDay[i].TotalWafers - Model.ScrapByDay[i].TOT_REJ_WFRS - Model.ScrapByDay[i].TW_PROD - Model.DailyPlanWafers;

                    if (dayDelta < 0)
                        myClass = "table-danger text-danger";
                    else
                        myClass = "";

                    <td class="text-center @myClass">@dayDelta</td>

                    deltaToPlan += dayDelta;
                }
                else
                {
                    <td></td>
                }
            }
            <td class="text-center">@deltaToPlan</td>
            <td></td>
            <td>Difference to target</td>
        </tr>
        <tr>
            <td scope="row">Wafers Needed to make QTR</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:#,###}", Model.DailyWafersForQTR)</td>
            <td class="text-center">@string.Format("{0:##,###}", Model.DailyWafersForQTR * 7)</td>
            <td></td>
            <td>Number updated weekly</td>
        </tr>
    </tbody>
</table>
