<?php
use \common\libs\MyLib;
?>
<?php $this->beginBlock('header_css'); ?>
<?php $this->endBlock(); ?>

    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <form role="form" class="form-horizontal data-from" id="theFrm">
                    <input type="hidden" name="id" value="<?=intval($info->id)?>">
                    <div class="tabs-container">
                        <ul class="nav nav-tabs">
                            <li class="active"><a data-toggle="tab" href="#tab-1"> 员工信息</a></li>
                            <li class=""><a data-toggle="tab" href="#tab-2"> 薪酬信息</a></li>
                            <li class=""><a data-toggle="tab" href="#tab-3"> 权限信息</a></li>
                        </ul>
                        <div class="tab-content ">
                            <div id="tab-1" class="tab-pane active">
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">工号</label>
                                        <div class="col-sm-3">
                                            <input type="text" name="username" value="<?=$info->username?>" class="form-control">
                                        </div>
                                        <label class="col-sm-1 control-label">登录密码</label>
                                        <div class="col-sm-3">
                                            <input type="text" name="password" value="" class="form-control">
                                        </div>
                                        <label class="col-sm-1 control-label">姓名</label>
                                        <div class="col-sm-3">
                                            <input type="text" name="name" value="<?=$info->name?>" class="form-control">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">联系电话</label>
                                        <div class="col-sm-3">
                                            <input type="text" name="phone" value="<?=$info->phone?>" class="form-control">
                                        </div>
                                        <label class="col-sm-1 control-label">所属岗位</label>
                                        <div class="col-sm-3">
                                            <select name="group_id" class="form-control">
                                                <option value="0">&nbsp;</option>
                                                <?php
                                                foreach($group_items as $group) {
                                                    echo '<option value="'.$group->id.'"';
                                                    if($group->id == $info->group_id)
                                                        echo ' selected ';
                                                    echo '>'.$group->name.'</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>
                                        <label class="col-sm-1 control-label">角色</label>
                                        <div class="col-sm-3">
                                            <select name="role_id" class="form-control">
                                                <?php
                                                foreach($role_items as $role) {
                                                    echo '<option value="'.$role->id.'"';
                                                    if($role->id == $info->role_id)
                                                        echo ' selected ';
                                                    echo '>'.$role->name.'</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">允许登录</label>
                                        <div class="col-sm-3">
                                            <div class="checkbox checkbox-inline">
                                                <input type="checkbox" id="is_login" name="is_login" value="1" <?=$info->is_login==1?'checked':''?>>
                                                <label for="is_login"> 是 </label>
                                            </div>
                                        </div>
                                        <label class="col-sm-1 control-label">外网登陆</label>
                                        <div class="col-sm-3">
                                            <div class="checkbox checkbox-inline">
                                                <input type="checkbox" id="is_outer" name="is_outer" value="1" <?=$info->is_outer==1?'checked':''?>>
                                                <label for="is_outer"> 允许 </label>
                                            </div>
                                        </div>
                                        <label class="col-sm-1 control-label">是否离职</label>
                                        <div class="col-sm-3">
                                            <div class="checkbox checkbox-inline">
                                                <input type="checkbox" id="is_leave" name="is_leave" value="1" <?=$info->is_leave==1?'checked':''?>>
                                                <label for="is_leave"> 是 </label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="tab-2" class="tab-pane">
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">入职日期</label>
                                        <div class="col-sm-3">
                                            <div class="input-group date">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                <input type="text" name="enter_date" class="form-control" value="<?=$info->enter_date?>" autocomplete="off">
                                            </div>
                                        </div>
                                        <label class="col-sm-1 control-label">转正时间</label>
                                        <div class="col-sm-3">
                                            <div class="input-group date">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                <input type="text" name="job_date" class="form-control" value="<?=$info->job_date?>" autocomplete="off">
                                            </div>
                                        </div>
                                        <label class="col-sm-1 control-label">调整工资</label>
                                        <div class="col-sm-3">
                                            <input type="text" name="ext_pay" value="<?=$info->ext_pay?>" class="form-control">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">总工资</label>
                                        <div class="col-sm-3">
                                            <label class="form-control-static"><span id="total_pay">0.00</span>元</label>
                                        </div>
                                        <label class="col-sm-1 control-label">试用工资</label>
                                        <div class="col-sm-3">
                                            <div class="input-group">
                                                <input type="text" name="try_rate" value="<?=$info->try_rate?>" class="form-control">
                                                <div class="input-group-addon">%</div>
                                            </div>
                                        </div>
                                        <label class="col-sm-1 control-label">试用工资</label>
                                        <div class="col-sm-3">
                                            <div class="input-group">
                                                <input type="text" name="try_pay" value="<?=$info->try_pay?>" class="form-control">
                                                <div class="input-group-addon">元</div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">是否双休</label>
                                        <div class="col-sm-3">
                                            <div class="checkbox checkbox-inline">
                                                <input type="checkbox" id="is_double" name="is_double" value="1" <?=$info->is_double==1?'checked':''?>>
                                                <label for="is_double"> 是 </label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="tab-3" class="tab-pane">
                                <div class="panel-body">
                                    <TABLE id=tb3 name="tb" class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th width="100">权限</th>
                                            <th>一级菜单（二级菜单）</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        foreach($permissions as $permission) {
                                            $parent_menus = $permission->getMenus()
                                                ->where('parent_id=0')
                                                ->all();
                                            ?>
                                            <tr>
                                                <td width="100" height="25" class="bg_tr">
                                                    <?php
                                                    echo '<label class="i-checks">';
                                                    echo '<input class="parent_checkbox" type="checkbox" name="permissions[]" value="'.$permission->id.'"';
                                                    if($info->getPermissions()->where('id='.$permission->id)->count() > 0)
                                                        echo ' checked ';
                                                    echo '>'.$permission->name;
                                                    echo '</label>';
                                                    ?>
                                                </td>
                                                <td height="25" class="td_bg">
                                                    <?php
                                                    foreach ($parent_menus as $parent_menu) {
                                                        echo '<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" id="mainTable">';
                                                        echo '<tr>';
                                                        echo '<td width="100">';
                                                        echo '<label class="i-checks">';
                                                        echo '<input class="sub_checkbox" type="checkbox" name="menus[]" value="'.$parent_menu->id.'"';
                                                        if($info->getMenus()->where('id='.$parent_menu->id)->count() > 0)
                                                            echo ' checked ';
                                                        echo '>' . $parent_menu->name;
                                                        echo '</label>';
                                                        echo '</td>';
                                                        echo '<td>';
                                                        $child_menus = $permission->getMenus()
                                                            ->where('parent_id=' . $parent_menu->id)
                                                            ->all();
                                                        foreach ($child_menus as $child_menu) {
                                                            echo '<label class="i-checks">';
                                                            echo '<input class="three_checkbox" type="checkbox" name="menus[]" value="'.$child_menu->id.'"';
                                                            if($info->getMenus()->where('id='.$child_menu->id)->count() > 0)
                                                                echo ' checked ';
                                                            echo '>' . $child_menu->name;
                                                            echo '</label>';
                                                        }
                                                        echo '</td>';
                                                        echo '</tr>';
                                                        echo '</table>';
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="row" style="margin-top: 8px;">
                            <div class="col-sm-5 col-sm-offset-5">
                                <button class="btn btn-primary btn-save" type="button">
                                    <strong><?=$info ? '保 存':'添 加'?></strong>
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

<?php $this->beginBlock('footer_js'); ?>
    <script type="text/javascript" language="javascript">
        $(function () {
            $('.input-group.date').datepicker({
                todayBtn: "linked",
                keyboardNavigation: false,
                forceParse: false,
                calendarWeeks: true,
                autoclose: true
            });

            //提交按钮
            $(".btn-save").click(function(){
                if(confirm('是否确认提交?')) {
                    var params = $("#theFrm").serialize();
                    $.post('/user/save',params,function(obj){
                        parent.layer.msg(obj.msg);
                        if(obj.success) {
                            parent.refreshList();
                            layer_close();
                        }
                    },'json');
                }
            });

            //处理自动选择功能
            $('.parent_checkbox').click(function() {
                var checked = $(this).prop('checked');
                var subs = $(this).parent().parent().next().find('.sub_checkbox');
                for(var i = 0; i < subs.length; i++) {
                    if(checked) {
                        $(subs[i]).prop('checked', 'checked');
                    } else {
                        $(subs[i]).prop('checked', '');
                    }
                }
                var threes = $(this).parent().parent().next().find('.three_checkbox');
                for(var i = 0; i < threes.length; i++) {
                    if(checked) {
                        $(threes[i]).prop('checked', 'checked');
                    } else {
                        $(threes[i]).prop('checked', '');
                    }
                }
            });
            $('.sub_checkbox').click(function() {
                var checked = $(this).prop('checked');
                var parents = $(this).parent().parent().parent().parent().parent().parent().prev().find('.parent_checkbox');
                for(var i = 0; i < parents.length; i++) {
                    if(checked) {
                        $(parents[i]).prop('checked', 'checked');
                    }
                }
                var threes = $(this).parent().parent().next().find('.three_checkbox');
                for(var i = 0; i < threes.length; i++) {
                    if(checked) {
                        $(threes[i]).prop('checked', 'checked');
                    } else {
                        $(threes[i]).prop('checked', '');
                    }
                }
            });
            $('.three_checkbox').click(function() {
                var checked = $(this).prop('checked');
                var parents = $(this).parent().parent().parent().parent().parent().parent().prev().find('.parent_checkbox');
                for(var i = 0; i < parents.length; i++) {
                    if(checked) {
                        $(parents[i]).prop('checked', 'checked');
                    }
                }
                var subs = $(this).parent().parent().prev().find('.sub_checkbox');
                for(var i = 0; i < subs.length; i++) {
                    if(checked) {
                        $(subs[i]).prop('checked', 'checked');
                    }
                }
            });
        })
    </script>
<?php $this->endBlock('footer_js'); ?>