@{
    ViewBag.Title = "选择人员";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using MES.Production.Service;
@using Central.Control.Domain;
@using Ant.Service.Common;
@using Ant.Service.Common.Enums;
@using CommonToolHelper;
@using MES.Production.Entity;
@model PageInfo
<style type="text/css">
    body {
        background-color: #fff;
    }

    .tn {
        text-align: center !important;
    }

    .ckcenter {
        margin: 6px auto !important;
    }

    .table tbody tr td {
        padding: 5px 3px !important;
        vertical-align: middle;
        text-align: left;
    }

        .table tbody tr td a {
            margin-left: 10px;
        }

    .userlist span {
        vertical-align: middle;
        text-align: center;
        line-height: 25px;
        cursor: pointer;
    }

        .userlist span input[type=checkbox] {
            margin-top: -4px;
        }

    .show-content ul {
        list-style: none;
        line-height: 25px;
        color: #337ab7;
        display: inline-block;
        margin-top: 20px;
    }

        .show-content ul li {
            float: left;
            width: 90px;
            margin-right: 15px;
            border: 1px solid #18a689;
            padding: 2px 3px;
            margin-bottom: 10px;
            text-align: center;
            color: #18a689
        }

            .show-content ul li input[type=checkbox] {
                display: inline;
                margin-top: 5px;
            }

            .show-content ul li:hover {
                background-color: #18a689;
                color: #ffffff;
                cursor: pointer;
            }

    .show-content .active {
        background-color: #18a689;
        color: #ffffff;
    }
</style>
<!-- table star -->
<div class="row col-lg-12" style=" overflow-y:auto;">
    <div class="wrapper-content">
        <div class="tab-content">
            @using (Ajax.BeginForm("SelectUser", null, new AjaxOptions() { }, new { @id = "form1", @class = "form-horizontal", @method = "get" }))
            {
                <div id="tab-1" class="tab-pane active">
                    <!-- search star -->
                    <div class="form-horizontal clearfix">
                        <div class="col-lg-4 col-sm-3 pl0" style="width:380px;margin-top:5px;">
                            <div class="form-group">
                                <label class="control-label" style="width:70px; float:left;">岗位:</label>
                                <div class="col-lg-8 col-sm-7" style="width:300px !important;overflow-x:hidden;">
                                    <select id="postid" name="postid" class="input-sm form-control input-s-sm inline">
                                        <option value="" @(ViewData["postid"] == null || ViewData["postid"] == "" ? "selected=\"selected\"" : "")>--请选择--</option>
                                        @{ var post = ViewData["postType"] as List<SYS_CODE>;
                                            foreach (var item in post)
                                            {
                                                <option value="@item.CODEVALUE" @(ViewData["postid"] != null && ViewData["postid"] != "" && Convert.ToInt32(ViewData["postid"]) == Convert.ToInt32(item.CODEVALUE) ? "selected" : "")>@item.NAMETEXT</option>
                                            }
                                        }
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-4 col-sm-4" style="float:right; width:200px; margin-right:30px;padding-top:5px;">
                            <div class="form-group">
                                <div class="col-lg-12 col-sm-12 input-group">
                                    @Html.TextBox("Search", null, new { @class = "input-sm form-control", @placeholder = "请输入查询用户名" })
                                    <span class="input-group-btn">
                                        <button type="button" onclick="submit()" class="btn btn-sm btn-primary" id="btn-search"> 搜索</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- search end -->
                    <div id="example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                        <table class="table table-striped table-bordered table-hover dataTables-example dataTable" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th class="tn"><input id="checkall" class="ckcenter" type="checkbox" value=""></th>
                                    <th style="width:180px !important;">帐号</th>
                                    <th>姓名</th>
                                    <th>角色</th>
                                </tr>
                            </thead>
                            <tbody class="tbshow">

                                @{foreach (var item in Model.List)
                                    {

                                        <tr data-id="@item.ID">
                                            <td class="tn">
                                                <input name="ckb_per" class="ckcenter" type="checkbox" value="@item.ID" data-parent="@item.NAME">
                                                <input name="hid_per" type="hidden" value="@item.NAME" />
                                            </td>
                                            <td>
                                                @item.ACCOUNT
                                            </td>
                                            <td>@item.NAME</td>
                                            <td>@item.ROLENAME</td>
                                        </tr>
                                    }
                                }

                            </tbody>
                        </table>
                        <!-- page -->
                        <div class="bottom">
                            @if (Model != null)
                            {
                                <div class="dataTables_info" id="example_info" Post="status" aria-live="polite">显示第 @Model.BeginPage 至 @Model.EndPage 项结果,共 @Model.Count 项</div>

                                <div class="dataTables_length" id="example_length">
                                    <label>
                                        显示
                                        <select name="example_length" onchange="submit()" aria-controls="example" class="form-control input-sm">
                                            <option value="10" @(Model.PageSize == 10 ? "selected" : "")>10</option>
                                            <option value="25" @(Model.PageSize == 25 ? "selected" : "")>25</option>
                                            <option value="50" @(Model.PageSize == 50 ? "selected" : "")>50</option>
                                            <option value="100" @(Model.PageSize == 100 ? "selected" : "")>100</option>
                                        </select> 项结果
                                    </label>
                                </div>
                                @CommonHtmlHelper.PaginationPager(Model.Index, Model.PageSize, Model.Count, 7, Model.Url);
                            }
                            <div class="clear"></div>
                        </div>
                        <!-- page -->
                    </div>
                </div>
            }
        </div>
    </div>
</div>
<div class="hr-line-dashed">
    <div class="form-group" style="margin-top:10px !important;">
        <div class="col-sm-2 col-sm-offset-2">
            <button class="btn btn-primary" id="btn-save" type="submit"><i class="fa fa-check"></i> <span>确定保存</span></button>
            <button class="btn btn-warning" id="btn-dig-close1" type="button"><i class="im-undo2"></i> 取消返回</button>
        </div>
    </div>
</div>
<!-- table end -->
@section scripts{
    <script type="text/javascript">
        //控制条件显示隐藏
        function Show() {
            var d = $('#dpt').val();
            var s = $('#Search').val();
            if (d == '' && s == '') {
                $('.tbshow').children('tr').show();
                $('.tbshow').children('tr').find('.userlist').children('li').show();
            } else if (d != '' && s == '') {
                $('.tbshow').children('tr').hide();
                $('.tbshow').children('tr').each(function () {
                    if ($(this).attr('data-id') == d) {
                        $(this).show();
                        $(this).children('td').find('.userlist').children('li').show();
                    }
                });
            } else if (d == '' && s != '') {
                var tr = '';
                $('.tbshow').children('tr').children('td').find('.userlist').children('li').each(function () {
                    var li = $(this).attr('data-user');
                    if (li.indexOf(s) >= 0) {
                        $(this).show();
                        tr += $(this).parent().parent().parent().attr('data-id') + ',';
                    } else {
                        $(this).hide();
                    }
                });
                if (tr != '') {
                    $('.tbshow').children('tr').each(function () {
                        if (tr.indexOf($(this).attr('data-id') + ',') >= 0) {
                            $(this).show();
                        } else {
                            $(this).hide();
                        }
                    });
                } else {
                    $('.tbshow').children('tr').hide();
                }
            } else {
                $('.tbshow').children('tr').hide();
                $('.tbshow').children('tr').each(function () {
                    if ($(this).attr('data-id') == d) {
                        $(this).show();
                        $(this).children('td').find('.userlist').children('li').each(function () {
                            var li = $(this).attr('data-user');
                            if (li.indexOf(s) >= 0) {
                                $(this).show();
                            } else {
                                $(this).hide();
                            }
                        });
                    }
                });
            }
        }
        $(function () {
            $('.select2').select2();
            var dialog = top.dialog.get(window);
            //全选 反选
            $('input[id="checkall"]').change(function () {
                if ($(this).prop("checked")) {
                    $("input[name='ckb_module']:checkbox,input[id='checkall']:checkbox").each(function () {
                        if ($(this).prop("checked") == false) {
                            $(this).prop("checked", true);
                            $('.ckcenter').each(function () {
                                $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]:checkbox').prop('checked', true);
                            });
                        }
                    });
                }
                else {
                    $("input[name='ckb_module']:checkbox,input[id='checkall']:checkbox").each(function () {
                        if ($(this).prop("checked") == true) {
                            $(this).prop("checked", false);
                            $('.ckcenter').each(function () {
                                $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]:checkbox').prop('checked', false);
                            });
                        }
                    });
                }
            });
            //自身选中 反选
            $('.userlist span').click(function () {
                var ckb = $(this).prev('input');
                if (ckb.prop('checked') == true) {
                    ckb.prop('checked', false);
                } else {
                    ckb.prop('checked', true);
                }
            });
            //递归子节点的ParentId
            function GetParentModuleId(t, s) {
                $('input[name="ckb_module"]').each(function () {
                    if ($(this).attr('data-parent') == t) {
                        s += $(this).attr('value') + ',';
                        GetParentModuleId($(this).attr('value'), s);
                    }
                });
                return s;
            }
            //选择部门 选中人员
            $('.ckcenter').bind('change', function () {
                //选中
                if ($(this).prop('checked') == true) {
                    //选中当前部门下的人员
                    $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]').prop('checked', true);
                    //选中当前部门下级部门的人员
                    //var moduleId = $(this).attr('value') + ',';
                    //moduleId = GetParentModuleId($(this).attr('value'), moduleId);
                    //$('input[name="ckb_module"]').each(function () {
                    //    var parentId = $(this).attr('data-parent');
                    //    if (parentId != '' && moduleId.indexOf(parentId) > -1) {
                    //        $(this).prop('checked', true);
                    //        $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]').prop('checked', true);
                    //    }
                    //});
                } else {
                    //取消选中当前部门下的人员
                    $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]').prop('checked', false);
                    //取消选中当前部门下级部门的人员
                    //var moduleId = $(this).attr('value') + ',';
                    //moduleId = GetParentModuleId($(this).attr('value'), moduleId);
                    //$('input[name="ckb_module"]').each(function () {
                    //    var parentId = $(this).attr('data-parent');
                    //    if (parentId != '' && moduleId.indexOf(parentId) > -1) {
                    //        $(this).prop('checked', false);
                    //        $(this).parent().next().next().find('.userlist').find('input[name="ckb_per"]').prop('checked', false);
                    //    }
                    //});
                }
            });
            $('#btn-dig-close1').click(function () {
                dialog.close('no').remove();
            });
            $('#btn-save').click(function () {
                var perid = '', n = '', a = '';
                $('input[name="ckb_per"]:checked').each(function () {
                    perid += $(this).attr('value') + ',';
                    n += $(this).next('span').html() + ',';
                    a += $(this).attr('data-parent') + ',';
                });
                if (perid != '') {
                    perid = perid.substr(0, perid.length - 1);
                    n = n.substr(0, n.length - 1);
                    a = a.substr(0, a.length - 1);
                }

                //if (perid.split(',').length == $('input[name="ckb_per"]').length) {
                //    perid = 'all';
                //    n = '全体成员';
                //    a = '全体成员';
                //}
                dialog.close(perid + '|' + a + '|' + n).remove();
            });
            /*检索功能--部门*/
            $('#dpt').change(function () { Show(); });
            /*检索功能--关键词*/
            //$('#btn-search').click(function () { Show(); });
            /*检索功能--回车*/
            $('#Search').keydown(function (e) { if (e.keyCode == 13) { Show(); } });
        });
    </script>
}