<?php

namespace App\Admin\Controllers\Content;

use App\Admin\Exports\Content\SelectHouseExport;
use App\Http\Controllers\Controller;
use App\Models\TalentHouseApply;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class BuyHouseSelectController extends Controller
{
    use HasResourceActions;

    private $marry = ['未知', '未婚', '已婚', '离异', '丧偶'];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index($id,Content $content)
    {
        return $content
            ->header('选房顺序号')
            ->description(' ')
            ->body(view('admin.content.buy_house_select')->with(['grid' => $this->grid($id)]));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($id)
    {
        $grid = new Grid(new TalentHouseApply());

        $grid->model()->where('house_id',$id)->where('status',2)->orderBy('select_house_no', 'ASC');

        $grid->id('ID');
        $grid->name('姓名');
        $grid->mobile('联系电话');
        $grid->talent_level('人才层次');
        $grid->select_house_no('顺序号')->display(function (){
            if ($this->select_house_no == 999999) {
                return '无';
            }
            return $this->select_house_no;
        });

        $grid->actions(function ($actions) {
            $actions->append("&nbsp;<button class='btn btn-primary btn-xs detail' id=" . $actions->row['id'] . ">详情</button>");
            if ($actions->row['select_house_no'] == 999999) {
                $actions->append("<button class='btn btn-default btn-xs select_no' data-code=" . $actions->row['id'] . ">填写顺序号</button>");
            }
        });

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', '姓名');
            $filter->like('mobile', '电话');
        });

        $grid->disableExport(false); //显示导出按钮
        $grid->exporter(new SelectHouseExport()); //传入自己在第1步创建的导出类

        return $grid;
    }

    /**
     * 详情
     */
    public function detail(Request $request)
    {
        $id   = $request->id;
        $info = TalentHouseApply::find($id);;
        $info->family     = json_decode($info->family);
        $info->marry_text = $this->marry[$info->marry];

        //layer相册层
        $photos = [
            'certificates'       => [],
            'marry_prove'        => [],
            'household_register' => [],
            'work_prove'         => [],
        ];
        if (!empty(json_decode($info->certificates))) {
            $info->certificates = json_decode($info->certificates);
            $photo_data         = [];
            foreach ($info->certificates as $k => $v) {
                $photo_data[] = [
                    'alt' => $v->name,
                    'pid' => $v->uid,
                    'src' => $v->response->path,
                ];
            }
            $photos['certificates'] = [
                'title' => '证件信息',
                'id'    => 1,
                'start' => 0,
                'data'  => $photo_data,
            ];
        } else {
            $info->certificates = [];
        }
        if (!empty(json_decode($info->marry_prove))) {
            $info->marry_prove = json_decode($info->marry_prove);
            $photo_data        = [];
            foreach ($info->marry_prove as $k => $v) {
                $photo_data[] = [
                    'alt' => $v->name,
                    'pid' => $v->uid,
                    'src' => $v->response->path,
                ];
            }
            $photos['marry_prove'] = [
                'title' => '婚姻证明',
                'id'    => 1,
                'start' => 0,
                'data'  => $photo_data,
            ];
        } else {
            $info->marry_prove = [];
        }
        if (!empty(json_decode($info->household_register))) {
            $info->household_register = json_decode($info->household_register);
            $photo_data               = [];
            foreach ($info->household_register as $k => $v) {
                $photo_data[] = [
                    'alt' => $v->name,
                    'pid' => $v->uid,
                    'src' => $v->response->path,
                ];
            }
            $photos['household_register'] = [
                'title' => '户口本',
                'id'    => 1,
                'start' => 0,
                'data'  => $photo_data,
            ];
        } else {
            $info->household_register = [];
        }
        if (!empty(json_decode($info->work_prove))) {
            $info->work_prove = json_decode($info->work_prove);
            $photo_data       = [];
            foreach ($info->work_prove as $k => $v) {
                $photo_data[] = [
                    'alt' => $v->name,
                    'pid' => $v->uid,
                    'src' => $v->response->path,
                ];
            }
            $photos['work_prove'] = [
                'title' => '在职证明',
                'id'    => 1,
                'start' => 0,
                'data'  => $photo_data,
            ];
        } else {
            $info->work_prove = [];
        }
        $html = view('admin.ajax.buy_house_detail')->with(['info' => $info, 'photos' => $photos])->render();
        return response()->json(['code' => 1, 'data' => $html]);
    }

    /**
     * 选房顺序号
     */
    public function select(Request $request)
    {
        $id   = $request->id;
        $select_house_no   = $request->select_house_no;
        $url    = empty($request->url) ? admin_base_path('content/buy_house') : $request->url;

        $apply = TalentHouseApply::find($id);
        if ($apply->select_house_no != 999999) {
            admin_toastr('数据错误,请重试', 'error');
        } else {
            $check = TalentHouseApply::where('select_house_no',$select_house_no)->where('house_id',$apply['house_id'])->first();
            if ($check) {
                admin_toastr('该顺序号已存在!', 'error');
            } else {
                $apply->select_house_no = $select_house_no;
                $apply->save();
                admin_toastr('操作成功', 'success');
            }
        }

        return redirect($url);
    }
}