@extends('admin.layout-content')

@section('header')
    <style>

    </style>
@endsection

@section('content')
    <div class="layui-container sg-create-container">
        <div class="layui-col-sm8 layui-col-sm-offset2">
            <form class="layui-form" method="POST" action="{{ $pre_uri . 'store' }}">

                {{ csrf_field() }}
                <input type="hidden" name="data[user_id]" value="{{ request('user_id') }}">
{{--                @include('share.layui-form-item', ['type' => 'select', 'name' => 'project_id', 'label' => '项目', 'selected_id' => (old('data') ? old('data')['project_id'] : ''), 'options' => $project_options])--}}
                {{--                @include('share.layui-form-item', ['type' => 'select', 'name' => 'project_role_id', 'label' => '角色', 'selected_id' => (old('data') ? old('data')['project_role_id'] : ''), 'options' => $role_options])--}}
                <div class="layui-form-item">
                    <label class="layui-form-label" style="padding: 0px;">项目</label>
                    <div class="layui-input-block">
                        <select name="project_id" required lay-verify="required"  lay-filter="project_id" utocomplete="off"  >
                        @foreach($project_options as $option)
                                <option value="{{ $option['id'] }}" {{ $option['id'] =  old('data')['project_id'] ? 'selected' : ''}}>{{ $option['name'] }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="padding: 0px;">角色</label>
                    <div class="layui-input-block">
                        <select name="project_role_id" required lay-verify="required"  autocomplete="off"  >
                                <option value="6" >领导账号</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
                <div class="layui-form-item">
                </div>
            </form>
        </div>
    </div>
@endsection

@section('footer')
    <script>

        $(function () {
            layui.use(['form'], function(){
                var form = layui.form;

                var roleList = {!! json_encode($role_options) !!}
                console.log(roleList)
                form.on('select(project_id)', function(data) {
                    project_id = data.value;
                    console.log(project_id)
                    let options = '';
                    if(project_id == 1){
                        options = '<option value="6" selected>领导账号</option>'
                    } else {
                        for(let i = 0; i < roleList.length; ++i) {
                            options += '<option value=' + parseInt(roleList[i].id) + (parseInt(roleList[i].id) === parseInt(project_id) ? ' selected' : '') + '>' + roleList[i].name + '</option>'
                        }
                    }
                    $("select[name='project_role_id']").html(options);
                    form.render('select');
                });


            });
        })
    </script>
@endsection

