<?phpnamespace App\Admin\Controllers;
use App\Company;
use App\Facades\CommonFacade;
use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;
use Encore\Admin\Facades\Admin;
class ChannelStaffController extends Controller
{
    use HasResourceActions;
    
    public function index(Content $content)
    {
        return $content
            ->header('员工')
            ->description('列表')
            ->body($this->grid());
    }
    
    public function show($id, Content $content)
    {
        return $content
            ->header('员工')
            ->description('详情')
            ->body($this->detail($id));
    }
    
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改')
            ->description('员工')
            ->body($this->form()->edit($id));
    }
    
    public function create(Content $content)
    {
        return $content
            ->header('创建')
            ->description('员工')
            ->body($this->form());
    }
    
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->model()->where('company_id', CommonFacade::getCompanyId())->where('id', '<>', Admin::user()->id);
        $grid->avatar('头像')->image('', 50,50);
        $grid->username('用户名');
        $grid->name('姓名');
        $grid->work_type('类型')->using(CommonFacade::getWorkTypeOptions());
        $grid->sex('性别')->using([0 => '男', 1 => '女']);
        $grid->jobNo('工号');
        $grid->mail('邮箱');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));
        $show->id('Id');
        $show->username('手机号');
        $show->name('姓名');
        $show->avatar('头像')->image();
        $show->work_type('类型')->using(CommonFacade::getWorkTypeOptions());
        $show->sex('性别')->using([0 => '男', 1 => '女']);
        $show->idNo('身份证');
        $show->jobNo('工号');
        $show->mail('邮箱');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new User);
        $form->select('work_type', '类型')->options(CommonFacade::getWorkTypeOptions());
        $form->mobile('username', '手机')->options(['mask' => '99999999999'])->rules('required');
        $form->text('name', '姓名')->rules('required');
        $form->text('jobNo', '工号');
        $form->password('password', trans('admin.password'))->rules('required|confirmed|')->default(function ($form) {
            return $form->model()->password;
        });
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')->default(function ($form) {
                return $form->model()->password;
        });
        $form->ignore(['password_confirmation']);
        $form->radio('sex', '性别')->options([0 => '男', 1 => '女'])->rules('required');
        $form->email('mail', '邮箱');
        $form->text('idNo', '身份证');
        $form->image('avatar', '头像');
        $form->saving(function (Form $form)  {
            if( $form->model()->company_id != CommonFacade::getCompanyId() && $form->model()->id !== null) {
                $error = new MessageBag([
                    'title'   => '错误',
                    'message' => '该员工不属于本公司',
                ]);
                return back()->with(compact('error'));
            }
            if (!empty(User::where('username',$form->username)->first()) && $form->username != $form->model()->username) {
                $error = new MessageBag([
                    'title'   => '错误',
                    'message' => '该账号已被占用,请更换',
                ]);
                return back()->with(compact('error'));
            }
                       if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });
        $form->saved(function (Form $form)  {
            $form->model()->company_id = CommonFacade::getCompanyId();
            $role_id = 5;
            if(!DB::table('admin_role_users')->where('user_id', $form->model()->id)->exists()) {
                Db::table('admin_role_users')->insert(['role_id' => $role_id,'user_id' => $form->model()->id]);
            }
            $form->model()->service_user_id = Admin::user()->service_user_id;
            $form->model()->save();
        });
        return $form;
    }
}