@model Com._9pm2.ClientWeb.Models.FactoryOrderList
@{ViewBag.Title = "外协订单查询"; }

<script src="~/Scripts/factoryordercommon.js?vn=20180813"></script>
<script src="../../Scripts/factoryorder.js?vn=20180813"></script>

@{
    Com._9pm2.ClientWeb.Models.LoginUser user = Session["LoginUser"] as Com._9pm2.ClientWeb.Models.LoginUser;
}

<div class="divSearch">
    <form method="post" action="/FactoryOrder/Index">
        <div>
            @Html.DropDownList("sStatus", new SelectList(Model.DdlStatus, "ItemValue", "ItemText"))
            @Html.DropDownList("sPaymentStatus", new SelectList(Model.DdlPaymentStatus, "ItemValue", "ItemText"))
            @Html.TextBox("sBeginDate", null, new { maxlength = "50", @class = "text-date", placeholder = "交易日期" }) ~@Html.TextBox("sEndDate", null, new { maxlength = "50", @class = "text-date", placeholder = "交易日期" })
            <div class="div-search-select2">
                @Html.DropDownList("sFactoryNo", new SelectList(Model.DdlFactory, "ItemValue", "ItemText"))
            </div>
            @Html.TextBox("sOrderNo", null, new { maxlength = "50", @Style = "width:160px", placeholder = "关联的客户订单号" })
            @Html.TextBox("sKey", null, new { maxlength = "50", @Style = "width:120px", placeholder = "关键字" })

            <button class="btn btn-default">搜索</button>
            <button type="button" class="btn btn-default" onclick="btnPrint(@user.PrintDeliveryMethod);">打印</button>

        </div>
    </form>
</div>

<div class="list">

    @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_订单支付))
            {
        <div class="btn-group">
            <button type="button" class="btn btn-default" onclick="btnBatchPayment();">批量支付</button>
            <button type="button" class="btn btn-default" onclick="btnBatchInvoice();">批量开票</button>
        </div>
    }
    else
    {
        <button type="button" class="btn btn-default" onclick="btnBatchInvoice();">批量开票</button>
    }
    @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_完工设置))
            {
        <button type="button" class="btn btn-default" onclick="BatchComplete();">批量完工</button>
    }

    <div class="error-msg"></div>

    <table id="tOrder">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" id="chkAll" onclick="SelectAll();" />
                </th>
                <th>订单编号</th>
                <th style="min-width:90px">交易日期</th>
                <th style="min-width:90px">外协商</th>
                <th style="min-width:90px">订单名</th>
                <th>明细</th>
                <th style="min-width:40px">数量</th>
                <th style="min-width:40px">单价</th>
                <th style="min-width:40px">金额</th>
                <th style="min-width:50px">总金额</th>
                <th style="min-width:70px">订单状态</th>
                <th style="min-width:70px">支付状态</th>
                <th style="min-width:40px">附件</th>
                <th style="width: 100px">操作</th>
            </tr>
        </thead>
        <tbody id="orderList" class="spanTbody">
            @{
                int j = 0;

                foreach (var item in Model.Orders)
                {
                    int i = 0;

                    string className = j % 2 == 0 ? "odd" : "even";

                    foreach (var o in item.Details)
                    {
                        if (i == 0)
                        {
                            <tr id="@item.FactoryOrderNo" class="@className">
                                <td class="td-checkbox" rowspan="@item.Details.Count">
                                    <input type="checkbox" onclick="SelectOne();" />
                                </td>
                                <td rowspan="@item.Details.Count">
                                    @Html.DisplayFor(modelItem => item.FactoryOrderNo)
                                </td>
                                <td rowspan="@item.Details.Count">
                                    @item.TransactionDate.ToString("yyyy-MM-dd")
                                </td>
                                <td class="client-name" rowspan="@item.Details.Count">
                                    @Html.DisplayFor(modelItem => item.FactoryName)
                                    <input type="hidden" value="@item.FactoryNo" />
                                </td>

                                <td>@o.OrderName</td>
                                <td>
                                    <b>@o.CategoryName</b>
                                    @o.DetailText@(string.IsNullOrEmpty(o.Memo) ? "" : " 备注: " + o.Memo)
                                </td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Number)</td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Price)</td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Amount)</td>

                                <td rowspan="@item.Details.Count">@Com._9pm2.ClientWeb.Utilities.NFormat.Format(item.Amount)</td>
                                <td rowspan="@item.Details.Count">
                                    <span id="spanStatus">@Html.DisplayFor(modelItem => item.StatusDescription)</span>
                                </td>

                                <td rowspan="@item.Details.Count">
                                    <span style="display:none" id="s1">@item.Paid</span>
                                    <span style="display:none" id="s2">@item.Unpaid</span>
                                    <span id="spanPaymentStatus">@Html.DisplayFor(modelItem => item.PaymentStatusDescription)</span>
                                </td>
                                <td class="td-img" rowspan="@item.Details.Count">
                                    @if (item.AttachmentNumber > 0)
                                    {
                                        <img src="~/Images/grid/x.png" />
                                    }
                                </td>
                                <td style="text-align:center" class="td-last" rowspan="@item.Details.Count">


                                    <div class="operation-btns">
                                        <div>
                                            <button id="select">&nbsp;</button>
                                        </div>
                                        <ul>

                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_订单支付))
                                            {
                                                <li><a href="javascript:void(0);" onclick="AddPayment('@item.FactoryOrderNo')">支付</a></li>
                                            }

                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_附件上传))
                                            {
                                                <li><a href="javascript:void(0);" onclick="ShowAttachment('@item.FactoryOrderNo')">附件</a></li>
                                            }


                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_订单编辑))
                                            {
                                                <li><a href="javascript:void(0);" onclick="EditOrder('@item.FactoryOrderNo','@item.EditModel')">编辑 </a></li>
                                            }

                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_订单删除))
                                            {
                                                <li><a onclick="DeleteOrder('@item.FactoryOrderNo','@item.FactoryOrderNo','@item.OrderName')">删除</a></li>
                                            }

                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_订单创建))
                                            {
                                                <li><a href="/FactoryOrder/Create?no=@item.FactoryOrderNo">快速创建</a></li>
                                            }

                                            @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P2_外协订单_完工设置))
                                            {
                                                if (item.OrderStatus == (int)Com._9pm2.ClientWeb.Common.FactoryOrderStatus.已完工)
                                                {
                                                    <li><a href="javascript:void(0);" onclick="SetStatus('@item.FactoryOrderNo',0)">标记未完工 </a></li>
                                                }
                                                else
                                                {
                                                    <li><a href="javascript:void(0);" onclick="SetStatus('@item.FactoryOrderNo',1)">标记完工 </a></li>

                                                }

                                            }

                                        </ul>
                                    </div>

                                </td>
                            </tr>
                        }
                        else
                        {
                            <tr id="@item.FactoryOrderNo" class="@className">
                                <td>@o.OrderName</td>
                                @*<td>@o.CategoryName</td>*@

                                <td>
                                    <b>@o.CategoryName</b>
                                    @o.DetailText@(string.IsNullOrEmpty(o.Memo) ? "" : " 备注: " + o.Memo)
                                </td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Number)</td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Price)</td>
                                <td>@Com._9pm2.ClientWeb.Utilities.NFormat.Format(o.Amount)</td>

                            </tr>
                        }

                        i++;
                    }

                    j++;

                }
            }

            @if (Model.Orders.Count() > 0 && user.ShowOrderTotalAmount)
            {
                <tr class="totalTd">
                    <td colspan="2">
                        <span>总计(所有页):</span>
                    </td>
                    @*<td></td>*@
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="oderAmountTotal">@(Model.TotalAmount == 0 ? "--" : Model.TotalAmount.ToString()) </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            }
        </tbody>
    </table>
    @Html.Pager("Index", "FactoryOrder", Model.RouterDirectory, Model.PagerConfig, "pagerDiv", "pagerDiv", 8, true, false, true, true, false)
</div>

<div id="divPayment" title="订单支付信息" style="display:none">
    <div id="divPaymentContent" class="list">
        <div id="paymentStatusDiv" class="FL" style="margin-bottom:5px;">
            <span class="OrderStatus">支付状态：</span>
            <span id="pdPaymentStatus" class="OrderStatus"></span>
        </div>
        <div id="unpaidDiv" class="FR" style="float:right;margin-bottom:5px;">
            <span class="OrderStatus">未付金额：</span>
            <span class="OrderStatusUnPaid" style="color:Red" id="unpaidAmount"></span>
        </div>
        <div class="gray clear">提示：支付记录不可删除。如果录入错误，可以随后补录一笔负数冲抵。</div>
        <table class="tableEdit">
            <tr>
                <td class="label">
                    支付记录
                </td>
                <td colspan="3" id="PaymentHistory"></td>
            </tr>
            <tr>
                <td class="label">
                    订单金额
                </td>
                <td class='money' id="Amount"></td>
                <td class="label">
                    已付金额
                </td>
                <td class='money' id="Paid"></td>
            </tr>
            <tr id="trAddPayment2">
                <td class="label">
                    支付金额
                </td>
                <td>
                    <input type="text" id="PaymentAmount" class="text-money" /> &nbsp;<span class="redStar">*</span>
                </td>
                <td class="label">
                    支付方式
                </td>
                <td>
                    @Html.DropDownList("PaymentType", new SelectList(Model.DdlPaymentType, "ItemValue", "ItemText"), new { @style = "width:160px;" }) &nbsp;<span class="redStar">*</span>
                </td>
            </tr>
            <tr id="trAddPayment3">
                <td class="label">
                    备注
                </td>
                <td colspan="3">
                    <input type="text" style="width: 400px" id="PaymentMemo" />
                </td>
            </tr>
        </table>
    </div>
    <div style="text-align: right;">
        <div class="error-msg"></div>
        <button class="btn btn-primary" id="btnAddPayment" type="button" onclick="PaymentSubmit()">支付</button>
        <button class="btn btn-default" type="button" onclick="$('#divPayment').dialog('close');">关闭</button>
    </div>
</div>

<div id="divBatchPayment" title="批量支付" style="display: none">
    <div id="divBatchPaymentContent" class="list" style="overflow-y: auto;">

        <table class="tableEdit">
            <tr>
                <td class="label">
                    未付金额
                </td>
                <td style="font-weight:bold; font-size:1.3em;color:#a94442" id="batchUnpaidAmount"></td>
            </tr>
            <tr>
                <td class="label">
                    支付金额
                </td>
                <td>
                    <input type="text" id="PaymentAmount" class="text-money" /> &nbsp;<span class="redStar">*</span>
                </td>
            </tr>
            <tr>
                <td class="label">
                    支付方式
                </td>
                <td>
                    @Html.DropDownList("batchPaymentType", new SelectList(Model.DdlPaymentType, "ItemValue", "ItemText"), new { @style = "width:160px;" }) &nbsp;<span class="redStar">*</span>
                </td>
            </tr>
            <tr>
                <td class="label">
                    备注
                </td>
                <td>
                    <input style="width: 400px" type="text" id="batchPaymentMemo" />
                </td>
            </tr>
        </table>
    </div>
    <div style="text-align: right;">

        <div class="error-msg"></div>
        <button class="btn btn-primary" type="button" id="btnAddBatchPayment" onclick="BatchPaymentSubmit()">保存</button>
        <button class="btn btn-default" type="button" onclick="$('#divBatchPayment').dialog('close');">关闭</button>
    </div>
</div>

@Html.Partial("FactoryOrderDetail", Session["LoginUser"] as Com._9pm2.ClientWeb.Models.LoginUser)
@Html.Partial("FactoryOrderAttachment")
<input type="hidden" id="FactoryOrderNo" />