<?php

namespace App\Admin\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Models\MemberInfo;
use App\Models\TalentHouseCheckLog;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class BuyHouseCheckLogController extends Controller
{
    use HasResourceActions;

    private $marry = ['未知', '未婚', '已婚', '离异', '丧偶'];
    private $status = ['未知', '待审核', '审核通过', '审核不通过'];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('审核日志')
            ->description(' ')
            ->body(view('admin.content.buy_house_check_log')->with(['grid' => $this->grid()]));
    }

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

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->id('ID');
        $grid->house_name('房源');
        $grid->user_name('用户');
        $grid->check_name('审核人');
        $status_text = $this->status;
        $grid->check_status('审核结果')->display(function () use ($status_text) {
            return $status_text[$this->check_status];
        });
        $grid->check_comment('审核备注')->style('max-width:400px');
        $grid->created_at('审核时间');

        $grid->actions(function ($actions) {
            $actions->append("&nbsp;<button class='btn btn-primary btn-xs detail' data-id='{$actions->row['id']}'>数据</button>");
        });

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('house_name', '房源');
            $filter->like('user_name', '用户');
        });
        return $grid;
    }

    /**
     * 详情
     */
    public function detail(Request $request)
    {
        $id   = $request->id;
        $log                = TalentHouseCheckLog::find($id);
        $info               = json_decode($log->data, true);
        $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'] = [];
        }

        $info['idcard'] = MemberInfo::where('uid',$info['user_id'])->first();
        $html = view('admin.ajax.buy_house_detail')->with(['info' => $info, 'photos' => $photos, 'status' => $this->status])->render();
        return response()->json(['code' => 1, 'data' => $html]);
    }
}