<style>
table {
    width: 100%;
    border-collapse: collapse;
    page-break-inside: auto;
    font-family: Arial;
    font-size: 13px;
}
td, th {
    padding: 8px;
    word-wrap: break-word;
}
.yieldReport {
    margin: 0 30px;
}
.yieldReport .table-header {
    border-collapse: collapse;
    width: 100%;
}
.yieldReport .table-header th,
.yieldReport .table-header td {
    text-align: center;
}
.yieldReport .table-header .left {
    text-align: left;
}
</style>

<%
function nv(val) {
  if (val == null || val === "" || val === undefined) return "";
  if (typeof val === "string" && val.trim().toUpperCase() === "NULL") return "";
  return val;
}

const M = master && master.length ? master[0] : {};
const D = detail && detail.length ? detail[0] : {};
%>

<div class="yieldReport">
    <table class="table-header" border="1">
        <thead>
            <tr>
                <% if (M.selectedYield == 'Yield Reconciliation') {  %>
                    <th colspan="4">YIELD RECONCILIATION OF LUBRICATED GRANULE                  
                        <% if (M.Layer == 'Layer 1') { %>
                            (Layer I)
                        <% } 
                        else if (M.Layer == 'Layer 2') { %>
                            (Layer I + Layer II)
                        <% } 
                        else if (M.Layer == 'Layer 3') { %>
                            (Layer I + Layer II + Layer III)
                        <% } %>
                    </th>

                <% } else if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <th colspan="5">YIELD RECONCILIATION (AS PER BATCH SIZE)</th>
                <% } %>
            </tr>

            <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                <tr>
                    <th colspan="5">Average Weight of Sachet: __________________ &nbsp;&nbsp;&nbsp; Standard B.Size : </th>
                </tr>
            <% } %>
            
            <tr>
                <th>Sr. No.</th>
                <th>Description</th>
                <th>Quantity (Kg)</th>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <th>Quantity (Nos)</th>
                <% } %>

                <th>Yield (%)</th>
            </tr>
        </thead>
        
        <tbody>
            <tr>
                <td>A</td>

                <% if (M.selectedYield == 'Yield Reconciliation') { %>
                    <td class="left">Theoretical Yield</td>
                <% }
                else if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td class="left">Batch Size</td>
                <% } %>
                
                <td><%= nv(D.TheoreticalYield_KG_1) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.TheoreticalYield_NOS_1) %></td>
                <% } %>

                <td><%= nv(D.TheoreticalYield_P_1) %></td>
            </tr>
            <tr>
                <td>B</td>

                <td class="left">
                    Yield of <%= M.selectedYield == 'Yield Reconciliation' ? 'Lubricated Granules' : 'Blend' %><br> 
                    (To be transferred to next stage)
                </td>

                <td><%= nv(D.YieldOfLubricatedGranules_KG_3) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.YieldOfLubricatedGranules_NOS_3) %></td>
                <% } %>

                <td><%= nv(D.YieldOfLubricatedGranules_P_3) %></td>
            </tr>
            <tr>
                <td>C</td>

                <td class="left">Sample Quantity (Inprocess<%= M.selectedYield == 'Batch Yield Reconciliation' ? ' Checks' : '' %>)</td>

                <td><%= nv(D.SampleQuantityInProcess_KG_7) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.SampleQuantityInProcess_NOS_7) %></td>
                <% } %>

                <td><%= nv(D.SampleQuantityInProcess_P_7) %></td>
            </tr>
            <tr>
                <td>D</td>

                <td class="left">Sample Quanity (Quality Control)</td>

                <td><%= nv(D.SampleQuantityQualityControl_KG_8) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.SampleQuantityQualityControl_NOS_8) %></td>
                <% } %>

                <td><%= nv(D.SampleQuantityQualityControl_P_8) %></td>
            </tr>
            <tr>
                <td>E</td>

                <td class="left">Validation Samples Quanity (if any)</td>

                <td><%= nv(D.ValidationSamples_KG_9) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.ValidationSamples_NOS_9) %></td>
                <% } %>

                <td><%= nv(D.ValidationSamples_P_9) %></td>
            </tr>
            <tr>
                <td>F</td>

                <td class="left">
                    Yield of <%= M.selectedYield == 'Yield Reconciliation' ? 'Lubricated Granules' : 'blend' %> including samples <br>
                    (B+C+D+E) (Limit: NLT 94.00 %)
                </td>

                <td><%= nv(D.YieldIncudingSample_KG_15) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.YieldIncudingSample_NOS_15) %></td>
                <% } %>

                <td><%= nv(D.YieldIncudingSample_P_15) %></td>
            </tr>
            <tr>
                <td>G</td>

                <td class="left">Rejects (if any)</td>

                <td><%= nv(D.Rejects_KG_10) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.Rejects_NOS_10) %></td>
                <% } %>

                <td><%= nv(D.Rejects_P_10) %></td>
            </tr>
            <tr>
                <td>H</td>

                <td class="left">Total Accounted For (F+G)</td>

                <td><%= nv(D.TotalAccounted_KG_12) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.TotalAccounted_NOS_12) %></td>
                <% } %>

                <td><%= nv(D.TotalAccounted_P_12) %></td>
            </tr>
            <tr>
                <td>I</td>

                <td class="left">Unaccountable Loss (A-H)</td>

                <td><%= nv(D.UnaccountedLoss_KG_13) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.UnaccountedLoss_NOS_13) %></td>
                <% } %>

                <td><%= nv(D.UnaccountedLoss_P_13) %></td>
            </tr>
            <tr>
                <td>J</td>

                <td class="left">Total loss (A-F) <br> (Limit NMT: 6.00 %)</td>
                
                <td><%= nv(D.TotalLoss_KG_14) %></td>

                <% if (M.selectedYield == 'Batch Yield Reconciliation') { %>
                    <td><%= nv(D.TotalLoss_NOS_14) %></td>
                <% } %>

                <td><%= nv(D.TotalLoss_P_14) %></td>
            </tr>
        </tbody>
    </table><br><br>

    <table style="border: none; text-align: left;">
        <tr>
            <th>CALCULATED BY</th>
            <td>: <%= nv(M.UserName) || '____________________'  %> <br> (PRODUCTION OFFICER)</td>

            <th>VERIFIED BY</th>
            <td>: <%= nv(M.CheckedByName) || '____________________'  %> <br> (PRODUCTION OFFICER)</td>
        </tr>
        <tr>
            <th>DATE</th>
            <td>: <%= nv(M.DoneBy_Date) ? moment(nv(M.DoneBy_Date)).format(dateFormat) : "____________________" %></td>

            <th>DATE</th>
            <td>: <%= nv(M.CheckedByDate) ? moment(nv(M.CheckedByDate)).format(dateFormat) : "____________________" %></td>
        </tr>
    </table>
</div>