<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Renter;
use App\Models\Residence;
use App\Models\ResidenceChange;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ResidenceController extends BaseController
{
    //
    /**
     * @api {post} /create Lưu thông tin tạm trú
     * @apiName /create
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     * @apiParam {String} name Tên
     * @apiParam {String} other_name Tên khác
     * @apiParam {String} day_birth ngày sinh
     * @apiParam {String} month_birth tháng sinh
     * @apiParam {String} year_birth năm sinh
     * @apiParam {String} gender Giới tính. Free text
     * @apiParam {String} birth_location Nơi sinh
     * @apiParam {String} hometown Nguyên quán
     * @apiParam {String} folk Dân tộc
     * @apiParam {String} religion Tôn giáo
     * @apiParam {String} nationality Quốc tịch
     * @apiParam {String} id_number Số CMND
     * @apiParam {String} passport_number Số hộ chiếu
     * @apiParam {String} residence_location Nơi thường trú
     * @apiParam {String} current_address Địa chỉ hiện tại
     * @apiParam {String} learning Trình độ học vấn
     * @apiParam {String} skill Trình độ chuyên môn
     * @apiParam {String} folk_lang Biết tiếng dân tộc
     * @apiParam {String} foreign_lang Ngoại ngữ
     * @apiParam {String} job Nghề nghiệp
     * @apiParam {String} summarize_yourself[] Tóm tắt bản thân. Gửi lên dạng json dạng mảng bao gồm 3 truờng
     * @apiParam {String} summarize_yourself.date Thông tin ngày tháng
     * @apiParam {String} summarize_yourself.location Chỗ ở
     * @apiParam {String} summarize_yourself.job Nghề nghiệp, nơi làm việc
     * @apiParam {String} criminal Thông tin tiền án tiền sự
     * @apiParam {String} summarize_family[] Tóm tắt gia đình
     * @apiParam {String} summarize_family.name Họ tên
     * @apiParam {String} summarize_family.birthday Ngày tháng năm sinh
     * @apiParam {String} summarize_family.gender Giới tính
     * @apiParam {String} summarize_family.relation Quan hệ
     * @apiParam {String} summarize_family.job Nghề nghiệp
     * @apiParam {String} summarize_family.current_location Chỗ ở hiện nay
     *
     *
     *
     * @apiDescription Api lưu thông tin tạm trú
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function store(Request $request)
    {
        $data = $request->except([
            'summarize_yourself',
            'summarize_family'
        ]);

        if (empty($data['user_id'])) {
            return response([
                'status' => 0,
                'message' => 'Dữ liệu không hợp lệ'
            ]);
        }

        $summarizeYourself = $request->input('summarize_yourself');
        $summarizeFamily = $request->input('summarize_family');

        if (!empty($summarizeYourself)) {
            $data['summarize_yourself'] = json_decode($summarizeYourself);
        }
        if (!empty($summarizeFamily)) {
            $data['summarize_family'] = json_decode($summarizeFamily);
        }

        Residence::query()
            ->updateOrCreate([
                'user_id' => $data['user_id']
            ], $data);

        return response([
            'status' => 1
        ]);
    }

    /**
     * @api {post} /create-change Lưu kê khai thay đổi nhân khẩu
     * @apiName /create-change
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     * @apiParam {String} user_create_name họ tên
     * @apiParam {String} user_create_gender giới tính
     * @apiParam {String} user_create_id_number CMND
     * @apiParam {String} user_create_passport_number Hộ chiếu
     * @apiParam {String} user_create_residence_location Nơi thường trú
     * @apiParam {String} user_create_current_address Địa chỉ chỗ ở hiện nay
     * @apiParam {String} user_create_phone Số điện thoại liên hệ
     * @apiParam {String} name Họ và tên
     * @apiParam {String} gender  Giới tính
     * @apiParam {String} birthday Ngày, tháng, năm sinh
     * @apiParam {String} folk  Dân tộc
     * @apiParam {String} nationality Quốc tịch
     * @apiParam {String} id_number Số CMND
     * @apiParam {String} passport_number Số hộ chiếu
     * @apiParam {String} birth_location Nơi sinh
     * @apiParam {String} hometown Nguyên quán
     * @apiParam {String} job Nghề nghiệp, nơi làm việc
     * @apiParam {String} residence_location Nơi thường trú
     * @apiParam {String} current_address Địa chỉ chỗ ở hiện nay
     * @apiParam {String} phone Số điện thoại liên hệ
     * @apiParam {String} owner_name Họ và tên chủ hộ
     * @apiParam {String} owner_relation Quan hệ với chủ hộ
     * @apiParam {String} change_content Nội dung thay đổi hộ khẩu nhân khẩu
     * @apiParam {String} users[] Những người cùng khai
     * @apiParam {String} users.name
     * @apiParam {String} users.birthday
     * @apiParam {String} users.gender
     * @apiParam {String} users.birth_location
     * @apiParam {String} users.job
     * @apiParam {String} users.folk
     * @apiParam {String} users.nationality
     * @apiParam {String} users.id_number
     * @apiParam {String} users.relation
     *
     *
     *
     *
     *
     *
     *  * @apiParam {String} user_name 1
     * @apiParam {String} user_birthday 2
     * @apiParam {String} user_blood_type 3
     * @apiParam {String} user_gender 4 (1 là nam, 0 là nữ)
     * @apiParam {String} marriage_status 5 (1 chưa, 2 là đã, 3 là ly)
     * @apiParam {String} location_birthday 6
     * @apiParam {String} hometown 7
     * @apiParam {String} folk 8
     * @apiParam {String} nationality 9 (1 là việt nam, quốc tịch khác thì string)
     * @apiParam {String} religion 10
     * @apiParam {String} id_number 11
     * @apiParam {String} residence 12
     * @apiParam {String} current_location 13
     * @apiParam {String} name_father 14
     * @apiParam {String} nationality_father
     * @apiParam {String} id_number_father
     * @apiParam {String} id_identity_father
     * @apiParam {String} name_mother
     * @apiParam {String} nationality_mother
     * @apiParam {String} id_number_mother
     * @apiParam {String} id_identity_mother
     * @apiParam {String} name_wife
     * @apiParam {String} nationality_wife
     * @apiParam {String} id_number_wife
     * @apiParam {String} id_identity_wife
     * @apiParam {String} collect_owner_name 15
     * @apiParam {String} collect_owner_relation 16
     * @apiParam {String} number_residence 17
     * @apiParam {String} date 18
     * @apiParam {String} type 0 là cái tờ 2. 1 là tờ 3
     * @apiDescription Api lưu thông tin tạm trú / thu thập thông tin
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function storeChange(Request $request)
    {
        $data = $request->except([
            'users',
            'type'
        ]);
        $type = $request->input('type', 0);
        $users = $request->input('users');
        $currentResidenceChange = ResidenceChange::query()->where('user_id', $data['user_id'])->first();
        if (!empty($users)) {
            $data['users'] = json_decode($users, true);
        }
        ResidenceChange::query()
            ->updateOrCreate([
                'user_id' => $data['user_id'],
                'type' => $type
            ], [
                'user_id' => $data['user_id'],
                'content' => $data,
                'type' => $type
            ]);
        return response([
            'status' => 1
        ]);
    }

    /**
     * @api {get} /print In thông tin tạm trú
     * @apiName /print
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     *
     *
     *
     * @apiDescription Api In thông tin tạm trú
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function printResidence(Request $request)
    {
        $renterId = $request->input('user_id');


        $residence = Residence::query()
            ->where('user_id', $renterId)
            ->first();
        if (!$residence) {
            $residence = Residence::create([
                'user_id' => $renterId
            ]);
        }

        //return view( 'admin2.renter.print_residence_mobile', compact( 'residence' ) )->render();
        return response([
            'status' => 1,
            'data' => view('admin2.renter.print_residence_mobile', compact('residence'))->render()
        ]);
    }

    /**
     * @api {get} /print-change In bản kê khai
     * @apiName /print-change
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     *
     *
     *
     * @apiDescription Api In bản kê khai
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function printResidenceChange(Request $request)
    {
        $renterId = $request->input('user_id');
        $type = $request->input('type', 0);


        $residence = ResidenceChange::query()
            ->where('user_id', $renterId)
            ->where('type', $type)
            ->first();
        if (!$residence) {
            $residence = ResidenceChange::create([
                'user_id' => $renterId,
                'type' => 0
            ]);
        }
        $content = [];
        if ($residence) {
            $content = $residence->content;
        }

        //return view( 'admin2.renter.print_residence_mobile', compact( 'residence' ) )->render();
        return response([
            'status' => 1,
            'data' => view('admin2.renter.print_residence_change_mobile', compact('residence', 'content'))->render()
        ]);
    }

    /**
     * @api {get} /item Xem thông tin tạm trú
     * @apiName /item
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     *
     *
     *
     * @apiDescription Api xem thông tin tạm trú
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function getResidence(Request $request)
    {
        $renterId = $request->input('user_id');


        $residence = Residence::query()
            ->where('user_id', $renterId)
            ->first();
        if (!$residence) {
            $residence = Residence::create([
                'user_id' => $renterId
            ]);
        }

        return response([
            'status' => 1,
            'data' => $residence
        ]);
    }

    /**
     * @api {get} /item-change Xem thông tin kê khai
     * @apiName /item-change
     * @apiGroup residence
     *
     * @apiParam {String} user_id id khách trọ
     *
     *
     *
     * @apiDescription Api Xem thông tin kê khai
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String}  message  Tin nhắn hệ thống.
     * @apiSuccess {String}  data
     */
    public function getResidenceChange(Request $request)
    {
        $renterId = $request->input('user_id');
        $type = $request->input('type', 0);


        $residence = ResidenceChange::query()
            ->where('user_id', $renterId)
            ->where('type', $type)
            ->first();
        if (!$residence) {
            $residence = ResidenceChange::create([
                'user_id' => $renterId,
                'type' => $type
            ]);
        }

        return response([
            'status' => 1,
            'data' => $residence
        ]);
    }
}
