@model Com._9pm2.ClientWeb.Models.CustomerList
@{
    ViewBag.Title = "客户管理";
}
<script src="../../Scripts/customer.js?vn=20180813"></script>
<script src="~/Scripts/jquery.treetable.js"></script>
<link href="~/Content/treetablecss/jquery.treetable.css" type="text/css" rel="stylesheet" />
<link href="~/Content/treetablecss/jquery.treetable.theme.default.css" type="text/css" rel="stylesheet" />

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

<form method="get" action="/Customer/Index">
    <div class="divSearch">
        <div>
            @Html.DropDownList("sCategory", new SelectList(Model.Categories, "ItemValue", "ItemText"), new { @class = "form-control" })&nbsp;
            @Html.TextBox("Keywords", null, new { maxlength = "50", placeholder = "关键字" })
            <button class="btn btn-default" id="btnSearch">搜索</button>
            <button class="btn btn-default" type="button" onclick="downExcel()">下载(Excel)</button>
            <div class="btn-group">
                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户创建))
                {
                    <button id="btnNew" type="button" class="btn btn-default">新增</button>
                }
                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户导入))
                {
                    <button id="btnImport" type="button" class="btn btn-default">批量导入</button>
                }
            </div>
            <button id="btnNameCheck" type="button" class="btn btn-warning">重名检测</button>
        </div>

    </div>
</form>

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

<div class="list">
    <table id="tCustomer" class="">
        <thead>
            <tr>
                <th>客户简称</th>
                <th>客户全称</th>
                <th>客户类别</th>
                <th class="th-customer-flag" style="max-width:40px">标记</th>
                <th>快速索引</th>
           
                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_联系人查询))
                {
                    <th>联系人</th>
                }
                <th>账户余额</th>

                <th>状态</th>
                <th style="width:120px;">操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Customers)
            {

                <tr class="specify" id="@item.CustomerNo">
                    <td>
                        @Html.DisplayFor(modelItem => item.CustomerShortName)
                    </td>
                    <td>@Html.DisplayFor(modelItem => item.CustomerFullName)  </td>
                    <td>@(item.CustomerCategory == null ? "" : item.CustomerCategory.Name)</td>
                    <td>
                        @if (item.CustomerCategory != null && !string.IsNullOrEmpty(item.CustomerCategory.Color))
                        {
                            <div class="customer-flag" style="background-color:@item.CustomerCategory.Color ;"></div>
                        }
                    </td>
                    <td>@Html.DisplayFor(modelItem => item.SearchKey)</td>

                   

                    @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_联系人查询))
                    {
                        <td class="td-contact">
                            <a href="javascript:void(0)" onclick="showContact(@item.CustomerNo)">
                                @(string.Join("，", item.Contact.Where(m => m.IsActive).Select(m => m.ContactName).ToList()))
                            </a>
                        </td>
                    }


                    <td>@item.Balance</td>

                    <td><span>@(item.IsActive ? "已启用" : "已停用")</span></td>
                    <td style="text-align:center" class="td-last">

                        <div class="operation-btns">
                            <div>
                                <button id="select">&nbsp;</button>
                            </div>
                            <ul>
                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户编辑))
                                {
                                    <li><a href="javascript:void(0);" onclick="EditCustomer('@item.CustomerNo');">编辑 </a></li>
                                }

                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户删除))
                                {
                                    <li><a onclick="DeleteCustomer('@item.CustomerNo');">删除</a></li>
                                }
                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户充值))
                                {
                                    <li><a href="/Customer/Deposit?customerNo=@item.CustomerNo">充值管理</a></li>
                                }

                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_联系人创建))
                                {
                                    <li><a onclick="AddContact('@item.CustomerNo','@item.CustomerShortName')">添加联系人</a></li>
                                }

                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_客户密码重置) && item.CanLogin)
                                {
                                    <li><a href="javascript:void(0);" onclick="ResetPassword('@item.CustomerNo','@item.CustomerShortName')">设置密码</a></li>
                                }
                            </ul>
                        </div>
                    </td>

                </tr>
                <tr id="C_@item.CustomerNo" data-tt-id="C_@item.CustomerNo" data-tt-parent-id="@item.CustomerNo" style="display:none;">
                    <td style="padding:0 5px 5px 15px" colspan="10">
                        <div style="width:900px;margin:0 auto;padding:10px;">
                            <a href="javascript:void(0)" style="float:right;padding:5px;" onclick="hideContact(@item.CustomerNo)">收起</a>

                            <table id="tContact" class="tableDetail" style="margin:5px 0 0 0px;width:100%; ">

                                <tr class="title">
                                    <td>姓名</td>
                                    <td>座机</td>
                                    <td>手机</td>
                                    <td>Email</td>
                                    <td>QQ</td>
                                    <td>主联系人</td>
                                    <td>备注</td>
                                    <td style="width:100px"></td>
                                </tr>

                                <tbody id="contactList">
                                    @foreach (var c in item.Contact)
                                    {
                                        <tr id="@c.ContactNo">
                                            <td>@c.ContactName</td>
                                            <td>@c.Phone</td>
                                            <td>@c.Mobile</td>
                                            <td>@c.Email</td>
                                            <td>@c.QQ</td>
                                            <td>@(c.IsMain ? "是" : "否")</td>
                                            <td>@c.Other</td>
                                            <td>
                                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_联系人编辑))
                                                {
                                                    <a href="javascript:void(0);" onclick="EditContact('@c.ContactNo','@item.CustomerNo','@item.CustomerShortName');">[编辑]</a>
                                                }
                                                @if (user.PermissionList.Contains(Com._9pm2.ClientWeb.Common.PT_PermissionType.P3_联系人删除))
                                                {
                                                    <a href="javascript:void(0);" onclick="DeleteContact('@c.ContactNo',this,'@item.CustomerNo');">[删除]</a>
                                                }

                                            </td>
                                        </tr>
                                    }
                                </tbody>

                            </table>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    @Html.Pager("Index", "Customer", Model.RouterDirectory, Model.PagerConfig, "pagerDiv", "pagerDiv", 8, true, false, true, true, false)
</div>

<div id="divCustomer" title="客户信息" style="display:none">
    <div id="divCustomerContent">
        @using (Html.BeginForm())
        {
            <table id="tableEdit" class="font85 tableEdit">
                <tr>
                    <td class="label">客户全称</td>
                    <td>
                        @Html.TextBox("CustomerFullName", null, new { maxlength = "100", @style = "width:260px" })&nbsp;<span class="redStar">*</span>
                    </td>
                </tr>
                <tr>
                    <td class="label">客户简称</td>
                    <td>
                        @Html.TextBox("CustomerShortName", null, new { maxlength = "100", @style = "width:260px" })&nbsp;<span class="redStar">*</span>
                    </td>
                </tr>
                <tr>
                    <td class="label">客户类别</td>
                    <td>
                        @Html.DropDownList("CategoryId", new SelectList(Model.Categories, "ItemValue", "ItemText"), new { @class = "form-control" })
                    </td>
                </tr>
                <tr>
                    <td class="label">快速索引</td>
                    <td>
                        @Html.TextBox("SearchKey", null, new { maxlength = "50", @style = "width:260px", placeholder = "请使用拼音首字母缩写作为索引键" })&nbsp;<span class="redStar">*</span>
                    </td>
                </tr>
                <tr id="trContact">
                    <td class="label">联系人</td>
                    <td>
                        @Html.TextBox("Contact", null, new { maxlength = "50", @style = "width:260px" })
                    </td>
                </tr>
                <tr>
                    <td class="label">客户电话</td>
                    <td>
                        @Html.TextBox("Phone", null, new { maxlength = "50", @style = "width:260px" })
                    </td>
                </tr>
                <tr>
                    <td class="label">送货地址</td>
                    <td>
                        @Html.TextBox("Address", null, new { maxlength = "100", @style = "width:260px" })
                    </td>
                </tr>
                <tr>
                    <td class="label">余额支付优先</td>
                    <td>
                        @Html.CheckBox("UseBalanceFirst")
                    </td>
                </tr> 

                <tr>
                    <td class="label">备注</td>
                    <td>@Html.TextArea("Description", null, new { maxlength = "200", @style = "height:60px;width:90%" })</td>
                </tr>

                <tr>
                    <td class="label">状态</td>
                    <td>
                        @Html.CheckBox("IsActive") 启用
                    </td>
                </tr>

            </table>
            <br />

            @Html.Hidden("CustomerNo")
        }
    </div>
    <div style="text-align:right;">

        <div class="error-msg"></div>
        <button type="button" class="btn btn-default" id="btnCancel">关闭</button>
        <button type="button" class="btn btn-primary" id="btnSave">保存</button>

    </div>
</div>

<div id="divDetail" title="客户信息" style="display:none">
    <div id="divDetailContent">
        @using (Html.BeginForm())
        {
            <table id="tableEdit" class="font85 tableEdit">
                <tr>
                    <td class="label">客户全称</td>
                    <td id="dCustomerFullName"></td>
                </tr>
                <tr>
                    <td class="label">客户简称</td>
                    <td id="dCustomerShortName"></td>
                </tr>
                <tr>
                    <td class="label">客户类别</td>
                    <td id="dCategoryName"></td>
                </tr>
                <tr>
                    <td class="label">快速索引</td>
                    <td id="dSearchKey"></td>
                </tr>
                <tr>
                    <td class="label">客户电话</td>
                    <td id="dPhone"></td>
                </tr>
                <tr>
                    <td class="label">送货地址</td>
                    <td id="dAddress"></td>
                </tr>
                <tr>
                    <td class="label">余额支付优先</td>
                    <td id="dUseBalanceFirst"></td>
                </tr>
                <tr>
                    <td class="label">账户余额</td>
                    <td id="dBalance"></td>
                </tr>

                <tr>
                    <td class="label">登录账号</td>
                    <td id="dLoginId"></td>
                </tr>

                <tr id="trPassword" style="display:none">
                    <td class="label">登录密码</td>
                    <td id="dPassword"></td>
                </tr>

                <tr>
                    <td class="label">备注</td>
                    <td id="dDescription"></td>
                </tr>

                <tr>
                    <td class="label">状态</td>
                    <td id="dStatus"></td>
                </tr>

            </table>

        }
    </div>
    <div style="text-align:right;">

        <div class="error-msg"></div>
        <button type="button" class="btn btn-default" onclick="$('#divDetail').dialog('close');">关闭</button>

    </div>
</div>

<div id="divPassword" title="重置密码" style="display:none">
    <div id="divPasswordContent">

        <table id="tableEdit" class="font85 tableEdit">
            <tr>
                <td class="label" style="height:41px">新密码</td>
                <td>
                    <input type="password" id="newPassword" placeholder="请输入新密码" />&nbsp;<span class="redStar">*</span>
                </td>
            </tr>
            <tr>
        </table>
    </div>
    <div style="text-align:right;">
        <div class="error-msg"></div>
        <button class="btn btn-default" type="button" onclick="$('#divPassword').dialog('close');">关闭</button>
        <button class="btn btn-primary" type="button" onclick="SavePassword()">保存</button>
    </div>
</div>


<div id="divContact" title="联系人信息" style="display:none">
    <div id="divContactContent" style="overflow-y: auto;">
        <form id="formContact">
            <table id="tableEdit" class="font85 tableEdit">
                <tr>
                    <td class="label">所属客户</td>
                    <td>
                        <span id="CustomerName"></span>
                        <input type="hidden" id="ContactNo" name="ContactNo" />
                        <input type="hidden" id="CustomerNo" name="CustomerNo" />
                    </td>
                </tr>
                <tr>
                    <td class="label">姓名</td>
                    <td>
                        @Html.TextBox("ContactName", null, new { maxlength = "50" })&nbsp;<span class="redStar">*</span>
                    </td>
                </tr>
                <tr>
                    <td class="label">座机</td>
                    <td>
                        @Html.TextBox("Phone", null, new { maxlength = "50" })
                    </td>
                </tr>
                <tr>
                    <td class="label">手机</td>
                    <td>
                        @Html.TextBox("Mobile", null, new { maxlength = "50" })
                    </td>
                </tr>
                <tr>
                    <td class="label">Email</td>
                    <td>
                        @Html.TextBox("Email", null, new { maxlength = "50" })
                    </td>
                </tr>
                <tr>
                    <td class="label">QQ</td>
                    <td>
                        @Html.TextBox("QQ", null, new { maxlength = "50" })
                    </td>
                </tr>
                <tr>
                    <td class="label">备注</td>
                    <td>
                        @Html.TextBox("Description", null, new { maxlength = "200", @style = "width:90%;height:60px" })
                    </td>
                </tr>
                <tr>
                    <td class="label">主联系人</td>
                    <td>
                        @Html.CheckBox("IsMain")
                    </td>
                </tr>
            </table>
            <br />
        </form>
    </div>
    <div style="text-align:right;">

        <div class="error-msg"></div>
        <button class="btn btn-default" type="button" onclick="$('#divContact').dialog('close');">取消</button>
        <button class="btn btn-primary" type="button" onclick="SaveContact()">保存</button>

    </div>
</div>
