<?php

namespace App\Http\Controllers\Business;

use App\Models\CustomContent;
use App\Models\MstCityPlanReason;
use App\Models\MstCompany;
use App\Models\MstShared;
use App\Models\MstUseDistrict;
use App\Models\RelationShared;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Dompdf\Dompdf;
use App\Models\MstPropertyType;
use App\Models\MstPropertyTypeSub;
use App\Models\Article;
use App\Models\Facility;
use App\Models\Vendor;
use App\Models\UserInfo;
use App\Models\MstPrefecture;
use App\Models\MstCity;
use App\Models\MstTown;
use App\Models\MstLine;
use App\Models\MstStation;
use App\Models\MstFloorType;
use App\Models\MstGenkyou;
use App\Models\MstUseArea;
use App\Models\MstStore;
use App\Models\MstStructure;
use App\Models\MstStructureSub;
use App\Models\MstSchool;
use App\Models\MstManner;
use App\Models\MstParking;
use App\Models\MstStatus;
use App\Models\RelationArticlePhoto;
use App\Models\RelationArticleFacility;
use App\Models\RelationVendorArticle;
use App\Models\RelationPriceHistory;
use App\Models\MstManagementForm;
use App\Models\MstFacilityType;
use App\Models\MstCityPlan;

// use Illuminate\Support\Facades\Auth;
// use Illuminate\Routing\Redirector;

// use Illuminate\Support\Facades\App;

class SearchController extends Controller
{
    private function getCustomData()
    {
        return CustomContent::whereNotNull("name")->where("company_id", \auth()->user()->company_id)->get();
    }

    /**
     * 売出物件　検索
     */
    public function forSale(Request $request)
    {
        $data = $this->getSearchProperty();
        return view('business.pages.for-sale', $data);
    }

    public function searchWithOldData(Request $request, $path)
    {
        $params = $request->all();
        $oldInput = $params;

        foreach ($params as $key => $value) {
            if (is_array($value)) {
                $oldInput[$key . '[]'] = array_first($value);
            } else {
                $oldInput[$key] = $value;
            }
        }

        return redirect('/business/' . $path)->withInput($oldInput);
    }

    /**
     * 成約物件　検索
     */
    public function close(Request $request)
    {
        $data = $this->getSearchProperty();
        $data['isclose'] = true;
        return view('business.pages.close', $data);
    }

    /**
     * マンション建物　検索
     */
    public function mansion(Request $request)
    {
        $data = $this->getSearchPropertyMansion();
        return view('business.pages.mansion', $data);
    }

    /**
     * 分譲地　検索
     */
    public function lot(Request $request)
    {
        $data = $this->getSearchPropertyLot();
        return view('business.pages.lot', $data);
    }

    /**
     * 売出物件　一覧
     * Refactor: using for print and pdf too
     */
    public function forSaleList(Request $request)
    {

        $user = Auth::user();
        $count = 0;
        $tmp_con = $request->all();
        $condition = $this->createConditionSearch($tmp_con);

        $floor_type_list = MstFloorType::getData();
        $floor_type = [];
        foreach ($floor_type_list as $row) {
            $floor_type[$row['code']] = $row['name'];
        }
        // $current_status_list = MstGenkyou:: getData();
        // $current_status = [];
        // foreach($current_status_list as $row) {
        //     $current_status[$row['code']] = $row['name'];
        // }

        $data['hide_items'] = [];
        if (!empty($request->cookie('hide_items'))) {
            $hideItems = $request->cookie('hide_items');
            $hideItems = str_replace(['[', ']'], '', $hideItems);
            $hideItems = explode(',', $hideItems);
            $data['hide_items'] = $hideItems;
            $request->hide_items = $hideItems;
        }

        $params = $request->all();
        unset($params['page']);
        unset($params['notcondition']);
        //unset($params['sort']);
        unset($params['_token']);
        $article_list = [];
        $article_list = collect($article_list);
        $action = $request->get('action', '');
        if(isset($request->address1)){
            for ($i = 0; $i < count($request->address1); $i++) {
                $newRequest = new \stdClass();
                foreach ($request->all() as $key => $value) {
                    if ($key == 'land_condition') {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                        }
                    } else {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i] ?? null];
                        }
                    }
                }
                $newRequest->image = $request->get("image");
                $newRequest->sort = $request->get("sort");
                if(!$request->ajax()){
                    $newRequest->hide_items = $request->hide_items;
                }
                $art_list = Article::getSearchDataByCondition($newRequest, null, null, 2, null, null);
                $article_list = $article_list->merge($art_list)->unique();
            }
        }else{
            $buyer = session('buyer_active');
            $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
            if(!empty($onlyItems)){
                $article_list = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
            }else{
                $article_list = $buyer->articles()->get();
            }
            foreach ($article_list as $key => $row) {
                $article_list[$key] = $row->article;
            }
        }

        $page = !empty($request->get("page")) ? $request->get("page") : 1;
        $data['type_page'] = !empty($request->get("type_page")) ? $request->get("type_page") : 1;
        $perPage = !empty($request->get('per_page')) ? $request->get('per_page') : 30;
        if ($request->print == 'all') {
            $perPage = 9999;
        }
        $article_list_id = new LengthAwarePaginator(
            $article_list->forPage($page, config('const.MAX_PERPAGE')),
            $article_list->count(),
            config('const.MAX_PERPAGE'),
            $page,
            ['path' => route('business-for-sale-list')]
        );
        $data['article_list_id'] = json_encode($article_list_id->pluck('building_id')->toArray());
        //dd(count($article_list));
        $article_list = new LengthAwarePaginator(
            $article_list->forPage($page, $perPage),
            $article_list->count(),
            $perPage,
            $page,
            ['path' => route('business-for-sale-list')]
        );

        foreach ($article_list as &$row) {
            $row->article_name = $row->name;
            // NEW判定
            if ($row->created_at > date("Y-m-d", strtotime("-" . config('const.NEW') . " day"))) {
                $row->new = true;
            } else {
                $row->new = false;
            }

            $address = "";
            if (isset($row->address1)) {
                $add = MstPrefecture::getData($row->address1);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address2)) {
                $add = MstCity::getData($row->address1, $row->address2);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address3)) {
                $add = MstTown::getData($row->address1, $row->address2, $row->address3);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address4)) {
                $address .= $row->address4;
            }
            $row->address = $address;
            if (isset($row->floor_plan_type)) {
                $row['floor_plan_text'] = $row->floor_plan . ($floor_type[$row->floor_plan_type] ?? '');

            }
            if (isset($row->current_status)) {
                $status = MstGenkyou::getDataByCode($row->current_status);
                $row->current_status = $status->name ?? '';
            }
            if (isset($row->age_month)) {
                $row->age = intval(date('Y')) - intval($row->age_year);
            }

            if (isset($row->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($row->primary_school_school);
                if (isset($primary->name)) {
                    $row->primary_school_name = $primary->name;
                    $row->primary_school_name = str_replace('義務教育学校', '', $row->primary_school_name);
                    $row->primary_school_name = str_replace('小学校', '', $row->primary_school_name);
                }
            }
            if (isset($row->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($row->secondary_school_school);
                if (isset($secondary->name)) {
                    $row->secondary_school_name = $secondary->name;
                    $row->secondary_school_name = str_replace('義務教育学校', '', $row->secondary_school_name);
                    $row->secondary_school_name = str_replace('中学校', '', $row->secondary_school_name);
                }
            }
            if (isset($row->other_primary_school_2)) {
                $other_school_1 = MstSchool::getSchoolByCode($row->other_primary_school_2);
                if (isset($other_school_1->name)) {
                    $row->other_school_name = $other_school_1->name;
                    $row->other_school_name = str_replace('義務教育学校', '', $row->other_school_name);
                    $row->other_school_name = str_replace('中学校', '', $row->other_school_name);
                }
            }
            if (isset($row->other_secondary_school_2)) {
                $other_school_2 = MstSchool::getSchoolByCode($row->other_secondary_school_2);
                if (isset($other_school_2->name)) {
                    $row->other_school_name_2 = $other_school_2->name;
                    $row->other_school_name_2 = str_replace('義務教育学校', '', $row->other_school_name_2);
                    $row->other_school_name_2 = str_replace('中学校', '', $row->other_school_name_2);
                }
            }


            // 間取り画像取得
            $count = 0;
            if (isset($row->floor_file_path) && !empty($row->floor_file_path)) {
                $row->floor_file_path = str_replace('public', '/storage', $row->floor_file_path);
                //$count = 1;
            } else {
                $row->floor_file_path = '';
            }

            // 写真
            $top_image = RelationArticlePhoto::getTopImagePath($row->building_id);
            if (isset($top_image->file_path)) {
                $row->top_file_path = str_replace('public', '/storage', $top_image->file_path);
            } else {
                $row->top_file_path = '';
            }

            // 写真の枚数
            $count = RelationArticlePhoto::getCountImgByArticleId($row->building_id);
            $row->file_count = $count;

            // 価格履歴の取得
            $history = RelationPriceHistory::getHistoryById($row->building_id);
            $row->history = $history;

            // 物件種別名、業者取得
            if (is_null($row->property)) {
                // マンションの場合
                $mansion = Article::getDataById($row->mansion_id);

                if (is_object($mansion)) {
                    $row->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
                    $vendor_info = Vendor::getDataListByArticleId($row->building_id);

                    // 学校
                    if (isset($mansion->primary_school_school)) {
                        $primary = MstSchool::getSchoolByCode($mansion->primary_school_school);
                        if (isset($primary->name)) {
                            $row->primary_school_name = $primary->name;
                            $row->primary_school_name = str_replace('義務教育学校', '', $row->primary_school_name);
                            $row->primary_school_name = str_replace('小学校', '', $row->primary_school_name);
                        }
                    }
                    if (isset($mansion->secondary_school_school)) {
                        $secondary = MstSchool::getSchoolByCode($mansion->secondary_school_school);
                        if (isset($secondary->name)) {
                            $row->secondary_school_name = $secondary->name;
                            $row->secondary_school_name = str_replace('義務教育学校', '', $row->secondary_school_name);
                            $row->secondary_school_name = str_replace('中学校', '', $row->secondary_school_name);
                        }
                    }

                    // 築年月
                    $row->age_year = $mansion->age_year;
                    $row->age_month = $mansion->age_month;
                    $row->age = intval(date('Y')) - intval($mansion->age_year);

                    $this->mapTrafficMansion($row, $mansion);

                }
            } else {
                // それ以外
                $row->property_name = $this->getPropertyName($row->property, $row->property_sub);
                $vendor_info = Vendor::getDataListByArticleId($row->building_id);
            }

            if (!empty($row->mansion_id)) {
                $row->mansion_file_count = RelationArticlePhoto::countImageArticleHouse($row->mansion_id, [1,2,3,4,5,6]);
                $row->file_count = RelationArticlePhoto::countImageArticleHouse($row->building_id, [2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
                $row->image_mansion = RelationArticlePhoto::getTopImagePath($row->mansion_id);
            } elseif ($row->property == 4 || $row->property == 5) {
                $row->mansion_file_count = RelationArticlePhoto::countImageArticleHouse($row->building_id, [4, 5, 6, 17, 20]);
                $row->file_count = RelationArticlePhoto::countImageArticleHouse($row->building_id, [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21]);
            }

            $row['vendor_info'] = $vendor_info ?? [];
            $row->file_path2 = $row->getFilePath2();
            //$row->vertical = $row->getVertical();
        }
        // 表示中のページの最初のアイテムのインデックス番号
        $first = $article_list->firstItem();

        // 表示中のページの最後のアイテムのインデックス番号
        $last = $article_list->lastItem();

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $count = count($article_list);
        $data['article_list'] = $article_list;
        $data['condition'] = $condition;
        $data['count'] = $count;
        $data['first'] = $first;
        $data['last'] = $last;
        $data['params'] = $params;
        if ($request->ajax()) {
            $data['type'] = 'for-sale';
            $data['flag'] = 1;
            $data['controlArticleBuyer'] = true;
            $print_list = $article_list->pluck('building_id');
            $view = view("business.objects.components.article_list_image", $data)->render();
            $view2 = view("business.objects.components.article_list_no_image", $data)->render();

            $response = [
                'html'=>$view,
                'html2'=> $view2,
                'print_list' => $print_list,
                'count' => $count
            ];
            return response()->json($response);
        }

        if (!$request->action) {
            return view('business.pages.for-sale-list', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView('business.pages.print.for-sale-list-print', $data)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('title.pdf');
        }

        if (!empty($request->get('only_items'))) {
            $data['total'] = $article_list->total();
            /*$sortArr = explode(',', $request->only_items);
            $sortArr = array_map('intval', $sortArr);

            $article_list = $article_list->sortBy(function ($art) use ($sortArr) {
                return array_search($art->building_id, $sortArr);
            });*/
            $data['article_list'] = $article_list;
        }

        return view('business.pages.print.for-sale-list-print', $data);
    }

    /**
     * 成約物件　一覧
     * Refactor: using for print and pdf too
     */
    public function closeList(Request $request)
    {
        $user = Auth::user();
        $count = 0;
        $tmp_con = $request->all();
        $condition = $this->createConditionSearch($tmp_con);

        $floor_type_list = MstFloorType::getData();
        $floor_type = [];
        foreach ($floor_type_list as $row) {
            $floor_type[$row['code']] = $row['name'];
        }
        // $current_status_list = MstGenkyou:: getData();
        // $current_status = [];
        // foreach($current_status_list as $row) {
        //     $current_status[$row['code']] = $row['name'];
        // }

        $data['hide_items'] = [];
        if (!empty($request->cookie('hide_items'))) {
            $hideItems = $request->cookie('hide_items');
            $hideItems = str_replace(['[', ']'], '', $hideItems);
            $hideItems = explode(',', $hideItems);
            $request->hide_items = $hideItems;
            $data['hide_items'] = $hideItems;
        }

        $params = $request->all();
        unset($params['page']);
        unset($params['notcondition']);
        unset($params['sort']);
        unset($params['_token']);

        $action = $request->get('action', '');
        $ConditionSearch = [];
        $ConditionSearch = explode(",", $request->get('only_items', ''));
        $StatusNotIn = [];
        $notCondition = $request->get('notcondition', '');
        $notCondition === 'stopSelling' ? $StatusNotIn = [5] : [];
        $article_list = [];
        $article_list = collect($article_list);
        for ($i = 0; $i < count($request->address1); $i++) {
            $newRequest = new \stdClass();
            foreach ($request->all() as $key => $value) {
                if ($key == 'land_condition') {
                    if (is_array($value)) {
                        $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                    }
                } else {
                    if (is_array($value)) {
                        $newRequest->{$key} = [$value[$i] ?? null];
                    }
                }

            }
            $newRequest->image = $request->get("image");
            $newRequest->sort = $request->get("sort");
            $newRequest->hide_items = $request->hide_items;
            //$art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, null, null  , $StatusNotIn);
            $art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, null, $action === 'print' ? [] : [], $StatusNotIn);
            $article_list = $article_list->merge($art_list)->unique();
        }
        $page = !empty($request->get("page")) ? $request->get("page") : 1;
        $perPage = !empty($request->get('per_page')) ? $request->get('per_page') : 30;
        if ($request->print == 'all') {
            $perPage = 9999;
        }

        $data['type_page'] = !empty($request->get("type_page")) ? $request->get("type_page") : 1;

        $article_list_id = new LengthAwarePaginator(
            $article_list->forPage($page, config('const.MAX_PERPAGE')),
            $article_list->count(),
            config('const.MAX_PERPAGE'),
            $page,
            ['path' => route('business-close-list')]
        );
        $data['article_list_id'] = json_encode($article_list_id->pluck('building_id')->toArray());

        $article_list = new LengthAwarePaginator(
            $article_list->forPage($page, $perPage),
            $article_list->count(),
            $perPage,
            $page,
            ['path' => route('business-close-list')]
        );

        foreach ($article_list as &$row) {
            $row->article_name = $row->name;
            // NEW判定
            if ($row->created_at > date("Y-m-d", strtotime("-" . config('const.NEW') . " day"))) {
                $row->new = true;
            } else {
                $row->new = false;
            }

            $address = "";
            if (isset($row->address1)) {
                $add = MstPrefecture::getData($row->address1);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address2)) {
                $add = MstCity::getData($row->address1, $row->address2);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address3)) {
                $add = MstTown::getData($row->address1, $row->address2, $row->address3);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address4)) {
                $address .= $row->address4;
            }
            $row->address = $address;
            if (isset($row->floor_plan_type)) {
                $row['floor_plan_text'] = $row->floor_plan . ($floor_type[$row->floor_plan_type] ?? '');

            }
            if (isset($row->current_status)) {
                $status = MstGenkyou::getDataByCode($row->current_status);
                $row->current_status = $status->name ?? '';
            }
            if (isset($row->age_month)) {
                $row->age = intval(date('Y')) - intval($row->age_year);
            }

            if (isset($row->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($row->primary_school_school);
                if (isset($primary->name)) {
                    $row->primary_school_name = $primary->name;
                    $row->primary_school_name = str_replace('義務教育学校', '', $row->primary_school_name);
                    $row->primary_school_name = str_replace('小学校', '', $row->primary_school_name);
                }
            }
            if (isset($row->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($row->secondary_school_school);
                if (isset($secondary->name)) {
                    $row->secondary_school_name = $secondary->name;
                    $row->secondary_school_name = str_replace('義務教育学校', '', $row->secondary_school_name);
                    $row->secondary_school_name = str_replace('中学校', '', $row->secondary_school_name);
                }
            }

            // 間取り画像取得
            $count = 0;
            if (isset($row->floor_file_path)) {
                $row->floor_file_path = str_replace('public', '/storage', $row->floor_file_path);
                $count = 1;
            } else {
                $row->floor_file_path = '';
            }

            // 写真
            $top_image = RelationArticlePhoto::getTopImagePath($row->building_id);
            if (isset($top_image->file_path)) {
                $row->top_file_path = str_replace('public', '/storage', $top_image->file_path);
            } else {
                $row->top_file_path = '';
            }

            // 写真の枚数
            $count = RelationArticlePhoto::getCountImgByArticleId($row->building_id);
            $row->file_count = $count;

            // 価格履歴の取得
            $history = RelationPriceHistory::getHistoryById($row->building_id);
            $row->history = $history;

            if (!empty($row->mansion_id)) {
                $row->mansion_file_count = RelationArticlePhoto::getCountImgByArticleId($row->mansion_id, 1);
                $row->file_count = RelationArticlePhoto::getCountImgByArticleId($row->building_id, 2);
                $row->image_mansion = RelationArticlePhoto::getTopImagePath($row->mansion_id);
            } elseif ($row->property == 4 || $row->property == 5) {
                $row->mansion_file_count = RelationArticlePhoto::getCountImgByArticleId($row->building_id, 1);
                $row->file_count = RelationArticlePhoto::getCountImgByArticleId($row->building_id, 2);
            }

            // 物件種別名、業者取得
            if (is_null($row->property)) {
                // マンションの場合
                $mansion = Article::getDataById($row->mansion_id);

                if ($mansion) {
                    $row->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
                    $vendor_info = Vendor::getDataListByArticleId($row->building_id);

                    // 学校
                    if (isset($mansion->primary_school_school)) {
                        $primary = MstSchool::getSchoolByCode($mansion->primary_school_school);
                        if (isset($primary->name)) {
                            $row->primary_school_name = $primary->name;
                            $row->primary_school_name = str_replace('義務教育学校', '', $row->primary_school_name);
                            $row->primary_school_name = str_replace('小学校', '', $row->primary_school_name);
                        }
                    }
                    if (isset($mansion->secondary_school_school)) {
                        $secondary = MstSchool::getSchoolByCode($mansion->secondary_school_school);
                        if (isset($secondary->name)) {
                            $row->secondary_school_name = $secondary->name;
                            $row->secondary_school_name = str_replace('義務教育学校', '', $row->secondary_school_name);
                            $row->secondary_school_name = str_replace('中学校', '', $row->secondary_school_name);
                        }
                    }

                    // 築年月
                    $row->age_year = $mansion->age_year;
                    $row->age_month = $mansion->age_month;
                    $row->age = intval(date('Y')) - intval($mansion->age_year);
                }
            } else {
                // それ以外
                $row->property_name = $this->getPropertyName($row->property, $row->property_sub);
                $vendor_info = Vendor::getDataListByArticleId($row->building_id);
            }

            $row['vendor_info'] = $vendor_info ?? [];
        }
        // 表示中のページの最初のアイテムのインデックス番号
        $first = $article_list->firstItem();

        // 表示中のページの最後のアイテムのインデックス番号
        $last = $article_list->lastItem();

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        //$data["title"] = "成約物件";

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $count = count($article_list);
        $data['article_list'] = $article_list;
        $data['condition'] = $condition;
        $data['count'] = $count;
        $data['first'] = $first;
        $data['last'] = $last;
        $data['params'] = $params;
        $data['close'] = 1;

        if ($request->ajax()) {
            $data['type'] = 'close';
            $data['flag'] = 2;
            $print_list = $article_list->pluck('building_id');
            $view = view("business.objects.components.article_list_image", $data)->render();
            $view2 = view("business.objects.components.article_list_no_image", $data)->render();

            $response = [
                'html'=>$view,
                'html2'=> $view2,
                'print_list' => $print_list,
                'count' => count($article_list)
            ];

            return response()->json($response);
        }

        if (!$request->action) {
            return view('business.pages.close-list', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView('business.pages.print.for-sale-list-print', $data)
                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('title.pdf');
        }

        if (!empty($request->get('only_items'))) {
            $data['total'] = $article_list->total();
            $sortArr = explode(',', $request->only_items);
            $sortArr = array_map('intval', $sortArr);

            $article_list = $article_list->sortBy(function ($art) use ($sortArr) {
                return array_search($art->building_id, $sortArr);
            });
            $data['article_list'] = $article_list;
        }
        return view('business.pages.print.for-sale-list-print', $data);
    }

    /**
     * Refactor: using for print and pdf too
     * 売出物件　一覧
     */
    public function mansionList(Request $request)
    {
        $params = $request->all();
        unset($params['_token']);
        unset($params['sort']);
        unset($params['page']);
        $timeStart = microtime();

        $tmp_con = $request->all();
        $condition = $this->createConditionSearch($tmp_con);

        $floor_type_list = MstFloorType::getData();
        $floor_type = [];
        foreach ($floor_type_list as $row) {
            $floor_type[$row['code']] = $row['name'];
        }
        $current_status_list = MstGenkyou:: getData();
        $current_status = [];

        foreach ($current_status_list as $row) {
            $current_status[$row['code']] = $row['name'];
        }

        $article_list = [];
        $article_list = collect($article_list);
        for ($i = 0; $i < count($request->address1); $i++) {
            $newRequest = new \stdClass();
            foreach ($request->all() as $key => $value) {
                if ($key == 'shared') {
                    if (is_array($value)) {
                        $newRequest->{$key} = [$value[$i * 8] ?? null, $value[$i * 8 + 1] ?? null, $value[$i * 8 + 2] ?? null, $value[$i * 8 + 3] ?? null, $value[$i * 8 + 4] ?? null,
                            $value[$i * 8 + 5] ?? null, $value[$i * 8 + 6] ?? null, $value[$i * 8 + 7] ?? null];
                    }
                } else {
                    if (is_array($value)) {
                        $newRequest->{$key} = [$value[$i] ?? null];
                    }
                }

            }
            $newRequest->image = $request->get("image");
            $newRequest->sort = $request->get("sort");
            $art_list = Article::getMansionDataByCondition($newRequest, null, null, null);
            $article_list = $article_list->merge($art_list)->unique();
        }
        $page = !empty($request->get("page")) ? $request->get("page") : 1;
        $perPage = !empty($request->get('per_page')) ? $request->get('per_page') : 30;

        if ($request->print == 'all') {
            $perPage = 9999;
        }

        $data['type_page'] = !empty($request->get("type_page")) ? $request->get("type_page") : 1;

        $article_list_id = new LengthAwarePaginator(
            $article_list->forPage($page, config('const.MAX_PERPAGE')),
            $article_list->count(),
            config('const.MAX_PERPAGE'),
            $page,
            ['path' => route('business-mansion-list')]
        );
        $data['article_list_id'] = json_encode($article_list_id->pluck('building_id')->toArray());

        $article_list = new LengthAwarePaginator(
            $article_list->forPage($page, $perPage),
            $article_list->count(),
            $perPage,
            $page,
            ['path' => route('business-mansion-list')]
        );

        // dd($timeStart, microtime());

        $room_count = 0;
        foreach ($article_list as &$row) {
            $count = 0;
            $room_count++;

            // 部屋取得
            $room_list = Article::getRoomList($row['building_id']);
            foreach ($room_list as &$room) {
                $room->floor_plan_name = MstFloorType::find($room->floor_plan_type)->name ?? '';
                if (isset($room->floor_plan_type)) {
                    $room->floor_plan_text = $room->floor_plan . $floor_type[$room->floor_plan_type];

                }
                // 業者の取得
                $vendor = RelationVendorArticle::getVendorDataByArticleId($room->building_id);
                $room->vendor_info = $vendor;

                // 価格履歴の取得
                $history = RelationPriceHistory::getHistoryById($room->building_id);
                $room->history = $history;
                $room->file_count = RelationArticlePhoto::getCountImgByArticleId($room->building_id);
            }
            $row->room_list = $room_list;
            $row->room_count = count($room_list);

            $address = "";
            if (isset($row->address1)) {
                $add = MstPrefecture::getData($row->address1);
                count($add) > 0 && $address .= $add[0]["name"];
            }
            if (isset($row->address2)) {
                $add = MstCity::getData($row->address1, $row->address2);
                count($add) > 0 && $address .= $add[0]["name"];
            }
            if (isset($row->address3)) {
                $add = MstTown::getData($row->address1, $row->address2, $row->address3);
                count($add) > 0 && $address .= $add[0]["name"];
            }
            if (isset($row->address4)) {
                $address .= $row->address4;
            }
            $row->address = $address;
            if (isset($row->floor_plan_type)) {
                $row->floor_plan_text = $row->floor_plan . ($floor_type[$row->floor_plan_type] ?? '');

            }
            if (isset($row->current_status) && $row->current_status != '') {
                $row->current_status = $current_status[$row->current_status] ?? '';
            }
            if (isset($row->age_month)) {
                $row->age = intval(date('Y')) - intval($row->age_year);
            }

            if (isset($row->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($row->primary_school_school);
                if (isset($primary->name)) {
                    $row->primary_school_name = $primary->name;
                    $row->primary_school_name = str_replace('義務教育学校','',$row->primary_school_name);
                    $row->primary_school_name = str_replace('小学校','',$row->primary_school_name);
                }
            }
            if (isset($row->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($row->secondary_school_school);
                if (isset($secondary->name)) {
                    $row->secondary_school_name = $secondary->name;
                    $row->secondary_school_name = str_replace('義務教育学校','',$row->secondary_school_name);
                    $row->secondary_school_name = str_replace('中学校','',$row->secondary_school_name);
                }
            }

            // NEW判定
            if ($row->created_at > date("Y-m-d", strtotime("-" . config('const.NEW') . " day"))) {
                $row->new = true;
            } else {
                $row->new = false;
            }
            // 物件種別
            $row->property_name = $this->getPropertyName($row->property, $row->property_sub);

            // // 業者取得
            // $vendor_info = Vendor::getDataListByArticleId($row->id);
            // $row->vendor_info = $vendor_info;

            $count = RelationArticlePhoto::countImageArticleHouse($row->building_id, [1,2,3,4,5,6]);
            $row->file_count = $count;
            $row->rooms_file_count = RelationArticlePhoto::whereIn("article_id", $row->rooms()->pluck("building_id")->toArray())->count();

            $images = RelationArticlePhoto::where('article_id', '=', $row->building_id)->where('company_id', '=', $row->company_id)
                ->orderBy('order')->get();

            $firstImage = Arr::get($images, 0);
            $secondImage = Arr::get($images, 1);

            if ($firstImage)
                $row->top_file_path = str_replace('public', '/storage', $firstImage->file_path);

            if ($secondImage)
                $row->floor_file_path = str_replace('public', '/storage', $secondImage->file_path);
        }
        // 表示中のページの最初のアイテムのインデックス番号
        $first = $article_list->firstItem();

        // 表示中のページの最後のアイテムのインデックス番号
        $last = $article_list->lastItem();
        //dd($article_list->toArray()['data'][0]['room_count']);
        $data['article_list'] = $article_list;
        $data['total'] = $article_list->total();
        $params['totalArt'] = $data['total'];
        $data['condition'] = $condition;
        $data['first'] = $first;
        $data['last'] = $last;
        $data['params'] = $params;

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $data['room_count'] = $room_count;

        //
        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        $ad_instructions = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => '未'],
            ['code' => 2, 'name' => '入稿'],
        ];

        $data['method_conf'] = [
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        $data['price_change'] = [
            ['code' => 1, 'name' => '価格変更'],
        ];

        if ($request->ajax()) {
            $data['type'] = 'mansion';
            $data['flag'] = 3;
            $print_list = $article_list->pluck('building_id');
            $view = view("business.objects.components.article_list_image_mansion", $data)->render();
            $view2 = view("business.objects.components.article_list_no_image_mansion", $data)->render();

            $response = [
                'html'=>$view,
                'html2'=> $view2,
                'print_list' => $print_list,
                'count' => count($article_list)
            ];
            return response()->json($response);
        }

        if (!$request->action) {
            return view('business.pages.mansion-list', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView('business.pages.print.mansion-list-print', $data)
                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('title.pdf');
        }

        if ($request->action == 'print') {
            $show_ite = $request->get('show_ite');
            if (isset($show_ite) && $show_ite != '') {
                $show_ite = explode(',', $show_ite);
                foreach ($data['article_list'] as $key => $val) {
                    if (in_array($val['building_id'], $show_ite)) {
                        $val['show_ite'] = true;
                    } else {
                        $val['show_ite'] = false;
                    }
                }
            }

            $show_ite_lv_2 = $request->get('show_ite_lv_2');
            if (isset($show_ite_lv_2) && $show_ite_lv_2 != '') {
                $show_ite_lv_2 = explode(',', $show_ite_lv_2);
                foreach ($data['article_list'] as $key => $val) {
                    foreach ($val['room_list'] as $key => $value) {
                        if (in_array($value['building_id'], $show_ite_lv_2)) {
                            $value['show_ite_lv_2'] = true;
                        } else {
                            $value['show_ite_lv_2'] = false;
                        }
                    }
                }
            }
        }
        if ($request->type == 'hasImage') {
            return view('business.pages.print.mansion-list-print_image', $data);
        } else {
            return view('business.pages.print.mansion-list-print', $data);
        }
    }

    /**
     * 売出物件　一覧
     * Refactor: using for print and pdf too
     */
    public function lotList(Request $request)
    {
        $params = $request->all();
        unset($params['_token']);
        unset($params['sort']);
        unset($params['page']);
        $count = 0;
        $tmp_con = $request->all();
        $condition = $this->createConditionSearch($tmp_con);

        $floor_type_list = MstFloorType::getData();
        $floor_type = [];
        foreach ($floor_type_list as $row) {
            $floor_type[$row['code']] = $row['name'];
        }
        $current_status_list = MstGenkyou:: getData();
        $current_status = [];
        foreach ($current_status_list as $row) {
            $current_status[$row['code']] = $row['name'];
        }
        $article_list = [];
        $article_list = collect($article_list);
        for ($i = 0; $i < count($request->address1); $i++) {
            $newRequest = new \stdClass();
            foreach ($request->all() as $key => $value) {
                {
                    if (is_array($value)) {
                        $newRequest->{$key} = [$value[$i] ?? null];
                    }
                }

            }
            $newRequest->image = $request->get("image");
            $newRequest->sort = $request->get("sort");
            $art_list = Article::getSearchDataByCondition($newRequest, null, null, null, '', 1);
            $article_list = $article_list->merge($art_list)->unique();
        }
        $page = !empty($request->get("page")) ? $request->get("page") : 1;
        $perPage = !empty($request->get('per_page')) ? $request->get('per_page') : 30;

        if ($request->print == 'all') {
            $perPage = 9999;
        }

        $data['type_page'] = !empty($request->get("type_page")) ? $request->get("type_page") : 1;

        $article_list_id = new LengthAwarePaginator(
            $article_list->forPage($page, config('const.MAX_PERPAGE')),
            $article_list->count(),
            config('const.MAX_PERPAGE'),
            $page,
            ['path' => route('business-lot-list')]
        );
        $data['article_list_id'] = json_encode($article_list_id->pluck('building_id')->toArray());

        $article_list = new LengthAwarePaginator(
            $article_list->forPage($page, $perPage),
            $article_list->count(),
            $perPage,
            $page,
            ['path' => route('business-lot-list')]
        );

        $propety_list = [];
        $lot_count = 0;

        foreach ($article_list as &$row) {
            $row->article_name = $row->name;
            $row->disp_file = str_replace('public', '/storage', $row->floor_file_path);
            // 部屋取得
            $lot_list = Article::getLotList($row['building_id']);

//            $lot_count = 0;
            foreach ($lot_list as &$lot) {
                $lot_count++;
                $lot->floor_plan_name = MstFloorType::find($lot->floor_plan_type)->name ?? '';
                if (isset($lot->floor_plan_type)) {
                    $lot->floor_plan_text = $lot->floor_plan . $floor_type[$lot->floor_plan_type];

                }
                // 物件種別
                $property_name = $this->getPropertyName($lot->property, $lot->property_sub);

                $lot->property_name = $property_name;
                if (!in_array($property_name, $propety_list)) {
                    $propety_list[] = $property_name;
                }

                // 写真
                $top_image = RelationArticlePhoto::getTopImagePath($lot->building_id);
                if (isset($top_image->file_path)) {
                    $row->top_file_path = str_replace('public', '/storage', $top_image->file_path);
                } else {
                    $row->top_file_path = '';
                }

                // 写真の枚数
                if ($lot->property < 4) {
                    $count = RelationArticlePhoto::getCountImgByArticleId($lot->building_id);
                    $lot->file_count = $count;
                } else {
                    $lot->file_count = RelationArticlePhoto::getCountImgByArticleId($lot->building_id, 2);
                    $lot->file_count_out = RelationArticlePhoto::getCountImgByArticleId($lot->building_id, 1);
                }

                // 業者の取得
                $vendor = RelationVendorArticle::getVendorDataByArticleId($lot->building_id);
                $lot->vendor_info = $vendor;

                // 価格履歴の取得
                $history = RelationPriceHistory::getHistoryById($lot->building_id);
                $lot->history = $history;
            }
            $row->property_list = $propety_list;

            $row->lot_list = $lot_list;
            $row->lot_count = count($lot_list);

            $address = "";
            if (isset($row->address1)) {
                $add = MstPrefecture::getData($row->address1);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address2)) {
                $add = MstCity::getData($row->address1, $row->address2);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address3)) {
                $add = MstTown::getData($row->address1, $row->address2, $row->address3);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($row->address4)) {
                $address .= $row->address4;
            }
            $row->address = $address;
            if (isset($row->floor_plan_type)) {
                $row->floor_plan_text = $row->floor_plan . ($floor_type[$row->floor_plan_type] ?? '');

            }
            if (isset($row->current_status)) {
                $row->current_status = $current_status[$row->current_status] ?? '';
            }
            if (isset($row->age_month)) {
                $row->age = intval(date('Y')) - intval($row->age_year);
            }

            if (isset($row->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($row->primary_school_school);
                if (isset($primary->name)) {
                    $row->primary_school_name = $primary->name;
                    $row->primary_school_name = str_replace('義務教育学校','',$row->primary_school_name);
                    $row->primary_school_name = str_replace('小学校','',$row->primary_school_name);
                }
            }
            if (isset($row->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($row->secondary_school_school);
                if (isset($secondary->name)) {
                    $row->secondary_school_name = $secondary->name;
                    $row->secondary_school_name = str_replace('義務教育学校','',$row->secondary_school_name);
                    $row->secondary_school_name = str_replace('中学校','',$row->secondary_school_name);
                }
            }

            // NEW判定
            if ($row->created_at > date("Y-m-d", strtotime("-" . config('const.NEW') . " day"))) {
                $row->new = true;
            } else {
                $row->new = false;
            }
            // 物件種別
            $row->property_name = $this->getPropertyName($row->property, $row->property_sub);

            // 業者取得
            $vendor_info = Vendor::getDataListByArticleId($row->id);
            $row->vendor_info = $vendor_info;
        }

        // 表示中のページの最初のアイテムのインデックス番号
        $first = $article_list->firstItem();

        // 表示中のページの最後のアイテムのインデックス番号
        $last = $article_list->lastItem();

        $data['article_list'] = $article_list;
        $data['total'] = $article_list->total();
        $data['condition'] = $condition;
        $data['first'] = $first;
        $data['last'] = $last;
        $params['totalArt'] = $data['total'];
        $data['params'] = $params;

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $data['lot_count'] = $lot_count;

        //
        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        $data['method_conf'] = [
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        $data['price_change'] = [
            ['code' => 1, 'name' => '価格変更'],
        ];

        if ($request->ajax()) {
            $data['type'] = 'lot';
            $data['flag'] = 3;
            $print_list = $article_list->pluck('building_id');
            $view = view("business.objects.components.article_list_image_lot", $data)->render();
            $view2 = view("business.objects.components.article_list_no_image_lot", $data)->render();

            $response = [
                'html'=>$view,
                'html2'=> $view2,
                'print_list' => $print_list,
                'count' => count($article_list)
            ];
            return response()->json($response);
        }

        if (!$request->action) {
            return view('business.pages.lot-list', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView('business.pages.print.lot-list-print', $data)
                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('lot_list.pdf');
        }

        if ($request->action == 'print') {
            $show_ite = $request->get('show_ite');
            if (isset($show_ite) && $show_ite != '') {
                $show_ite = explode(',', $show_ite);
                foreach ($data['article_list'] as $key => $val) {
                    if (in_array($val['building_id'], $show_ite)) {
                        $val['show_ite'] = true;
                    } else {
                        $val['show_ite'] = false;
                    }
                }
            }
            $show_ite_lv_2 = $request->get('show_ite_lv_2');
            if (isset($show_ite_lv_2) && $show_ite_lv_2 != '') {
                $show_ite_lv_2 = explode(',', $show_ite_lv_2);
                foreach ($data['article_list'] as $key => $val) {
                    foreach ($val['lot_list'] as $key => $value) {
                        if (in_array($value['building_id'], $show_ite_lv_2)) {
                            $value['show_ite_lv_2'] = true;
                        } else {
                            $value['show_ite_lv_2'] = false;
                        }
                    }
                }
            }
        }

        if (!empty($request->get('only_items'))) {
            $sortArr = explode(',', $request->only_items);
            $sortArr = array_map('intval', $sortArr);
            $article_list = $article_list->sortBy(function ($art) use ($sortArr) {
                return array_search($art->building_id, $sortArr);
            });
            $data['article_list'] = $article_list;
        }

        if ($request->type == 'hasImage') {
            return view('business.pages.print.lot-list-print_image', $data);
        } else {
            return view('business.pages.print.lot-list-print', $data);
        }
        return view('business.pages.print.lot-list-print', $data);
    }


    public function getSearchProperty()
    {
        $data = [];
        $user = Auth::user();

        $data['property'] = [
            ['code' => 61, 'name' => 'マンション'],
            ['code' => 1, 'name' => '土地'],
            ['code' => 41, 'name' => '新築戸建'],
            ['code' => 42, 'name' => '中古戸建'],
            ['code' => 5, 'name' => 'テラスハウス'],
            ['code' => 7, 'name' => 'タウンハウス'],
        ];

        $data['revenue'] = [
            ['code' => 1, 'name' => '収益'],
        ];

        $data['un_revenue'] = [
            ['code' => 1, 'name' => '収益除'],
        ];

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        $data['ad_instructions'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => '未'],
            ['code' => 2, 'name' => '入稿'],
        ];

        $data['pet'] = [
            ['code' => 1, 'name' => '不可'],
            ['code' => 2, 'name' => '可'],
            ['code' => 3, 'name' => '相談']
        ];
        $data['pet_num'] = [
            ['code' => '', 'name' => '匹数'],
            ['code' => 1, 'name' => 1],
            ['code' => 2, 'name' => 2],
            ['code' => 3, 'name' => 3],
            ['code' => 4, 'name' => 4],
            ['code' => 5, 'name' => 5],
            ['code' => 6, 'name' => '無制限'],
        ];
        $data['balcony'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => '北'],
            ['code' => 2, 'name' => '北東'],
            ['code' => 3, 'name' => '東'],
            ['code' => 4, 'name' => '南東'],
            ['code' => 5, 'name' => '南'],
            ['code' => 6, 'name' => '南西'],
            ['code' => 7, 'name' => '西'],
            ['code' => 8, 'name' => '北西'],
        ];
        // 都道府県・市区郡・町村の取得
        $data['pref'] = MstPrefecture::getData();
        //if (count($data['pref']) > 1) {
            $data['pref'] = array_merge([['name' => '未選択', 'code' => -1]], $data['pref']);
       // }
        $defaultPrefCode = array_get(array_first($data['pref']), 'code');
        if ($user->company_id === 1) {
            $city = MstCity::getData($defaultPrefCode, null, $user->company_id);
            $data['city'] = $city;
        } else {
            $data['city'] = MstCity::getData($defaultPrefCode, null, $user->company_id);

        }
        $data['town'] = [];

        // 学校区の取得
        $data['primary_school'] = MstSchool::getSchoolByType(1);
        $data['secondary_school'] = MstSchool::getSchoolByType(2);

        // 路線の取得
        $data['line'] = MstLine::getData();

        // 現況
        $data['land_status'] = MstGenkyou::getData();

        // 用途
        $data['use_area'] = MstUseArea::getData();

        // 店舗
        $user = Auth::user();
        $data['shop'] = MstStore::getDataAll($user->company_id);

        // 構造
        $data['construction'] = MstStructure::getData();

        // 間取り
        $data['floor_plan'] = [
            ['code' => 1, 'name' => '1R - 1SLDK'],
            ['code' => 2, 'name' => '2K - 2SLDK'],
            ['code' => 3, 'name' => '3K - 3SLDK'],
            ['code' => 4, 'name' => '4K - 4SLDK'],
            ['code' => 5, 'name' => '5K〜'],
        ];

        $data['parking'] = MstParking::getData();

        array_pop($data['parking']);

        $data['land_direction'] = [
            ['code' => 1, 'name' => '北'],
            ['code' => 2, 'name' => '北東'],
            ['code' => 3, 'name' => '東'],
            ['code' => 4, 'name' => '南東'],
            ['code' => 5, 'name' => '南'],
            ['code' => 6, 'name' => '南西'],
            ['code' => 7, 'name' => '西'],
            ['code' => 8, 'name' => '北西'],
        ];

        $data['walk'] = [
            ['code' => 1, 'name' => '徒歩'],
            ['code' => 2, 'name' => 'バス'],
        ];

        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        return $data;
    }

    public function getSearchPropertyMansion()
    {
        $user = Auth::user();
        $data = [];
        $data['property'] = [
            ['code' => 1, 'name' => '土地'],
            ['code' => 2, 'name' => '戸建'],
            ['code' => 3, 'name' => 'マンション'],
        ];


        $data['pet'] = [
            ['code' => 1, 'name' => '不可'],
            ['code' => 2, 'name' => '可'],
            ['code' => 3, 'name' => '相談'],
        ];
        $data['pet_num'] = [
            ['code' => '', 'name' => '匹数'],
            ['code' => 1, 'name' => 1],
            ['code' => 2, 'name' => 2],
            ['code' => 3, 'name' => 3],
            ['code' => 4, 'name' => 4],
            ['code' => 5, 'name' => 5],
            ['code' => 6, 'name' => '無制限'],
        ];
        $data['balcony'] = [
            ['code' => 1, 'name' => '東'],
            ['code' => 2, 'name' => '西'],
            ['code' => 3, 'name' => '南'],
            ['code' => 4, 'name' => '北'],
            ['code' => 5, 'name' => '北東'],
            ['code' => 6, 'name' => '北西'],
            ['code' => 7, 'name' => '南東'],
            ['code' => 8, 'name' => '南西'],
        ];
        // 都道府県・市区郡・町村の取得
        $data['pref'] = MstPrefecture::getData();
//        if (count($data['pref']) > 1) {
            $data['pref'] = array_merge([['name' => '未選択', 'code' => -1]], $data['pref']);
//        }
        $firstPrefCode = array_get(array_first($data['pref']), 'code');
        if ($user->company_id === 1) {
            $city = MstCity::getData($firstPrefCode, null, $user->company_id);
            $data['city'] = $city;
        } else {
            $data['city'] = MstCity::getData($firstPrefCode, null, $user->company_id);
        }
        $data['town'] = [];

        // 学校区の取得
        $data['primary_school'] = MstSchool::getSchoolByType(1);
        $data['secondary_school'] = MstSchool::getSchoolByType(2);

        // 路線の取得
        $data['line'] = MstLine::getData();

        // 現況
        $data['land_status'] = MstGenkyou::getData();

        // 用途
        $data['use_area'] = MstUseArea::getData();

        // 店舗
        $user = Auth::user();
        $data['shop'] = MstStore::getDataAll($user->company_id);

        // 構造
        $data['construction'] = MstStructure::getData();

        // 間取り
        $data['floor_plan'] = [
            ['code' => 1, 'name' => '1R - 1SLDK'],
            ['code' => 2, 'name' => '2k - 2SLDK'],
            ['code' => 3, 'name' => '3k - 3SLDK'],
            ['code' => 4, 'name' => '4k - 4SLDK'],
            ['code' => 5, 'name' => '5K〜'],
        ];

        $data['parking'] = MstParking::getDataByType(1);

        $data['walk'] = [
            ['code' => 1, 'name' => '徒歩'],
            ['code' => 2, 'name' => 'バス'],
        ];

        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['shared'] = [
            ['code' => 1, 'name' => 'ゲストルーム'],
            ['code' => 2, 'name' => '24時間ゴミ出し可'],
            ['code' => 3, 'name' => 'プール'],
            ['code' => 4, 'name' => 'エレベーター'],
            ['code' => 5, 'name' => '宅配ボックス'],
            ['code' => 6, 'name' => 'キッズルーム・託児所'],
            ['code' => 7, 'name' => '共用露天風呂'],
            ['code' => 8, 'name' => 'オートロック'],
        ];

        return $data;
    }

    public function getSearchPropertyLot()
    {
        $user = Auth::user();
        $data = [];
        $data['property'] = [
            ['code' => 1, 'name' => '土地'],
            ['code' => 2, 'name' => '戸建'],
            ['code' => 3, 'name' => 'マンション'],
        ];


        $data['pet'] = [
            ['code' => 1, 'name' => '不可'],
            ['code' => 2, 'name' => '可'],
            ['code' => 3, 'name' => '相談'],
        ];
        $data['pet_num'] = [
            ['code' => '', 'name' => '匹数'],
            ['code' => 1, 'name' => 1],
            ['code' => 2, 'name' => 2],
            ['code' => 3, 'name' => 3],
            ['code' => 4, 'name' => 4],
            ['code' => 5, 'name' => 5],
            ['code' => 6, 'name' => '無制限'],
        ];
        $data['balcony'] = [
            ['code' => 1, 'name' => '東'],
            ['code' => 2, 'name' => '西'],
            ['code' => 3, 'name' => '南'],
            ['code' => 4, 'name' => '北'],
            ['code' => 5, 'name' => '北東'],
            ['code' => 6, 'name' => '北西'],
            ['code' => 7, 'name' => '南東'],
            ['code' => 8, 'name' => '南西'],
        ];
        // 都道府県・市区郡・町村の取得
        $data['pref'] = MstPrefecture::getData();
//        if (count($data['pref']) > 1) {
            $data['pref'] = array_merge([['name' => '未選択', 'code' => -1]], $data['pref']);
//        }
        $firstPrefCode = array_get(array_first($data['pref']), 'code');
        if ($user->company_id === 1) {
            $city = MstCity::getData($firstPrefCode, null, $user->company_id);
            $data['city'] = $city;
        } else {
            $data['city'] = MstCity::getData($firstPrefCode, null, $user->company_id);
        }
        $data['town'] = [];

        // 学校区の取得
        $data['primary_school'] = MstSchool::getSchoolByType(1);
        $data['secondary_school'] = MstSchool::getSchoolByType(2);

        // 路線の取得
        $data['line'] = MstLine::getData();

        // 現況
        $data['land_status'] = MstGenkyou::getData();

        // 用途
        $data['use_area'] = MstUseArea::getData();

        // 店舗
        $user = Auth::user();
        $data['shop'] = MstStore::getDataAll($user->company_id);

        // 構造
        $data['construction'] = MstStructure::getData();

        // 間取り
        $data['floor_plan'] = [
            ['code' => 1, 'name' => '1R - 1SLDK'],
            ['code' => 2, 'name' => '2k - 2SLDK'],
            ['code' => 3, 'name' => '3k - 3SLDK'],
            ['code' => 4, 'name' => '4k - 4SLDK'],
            ['code' => 5, 'name' => '5K〜'],
        ];

        $data['walk'] = [
            ['code' => 1, 'name' => '徒歩'],
            ['code' => 2, 'name' => 'バス'],
        ];

        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['shared'] = [
            ['code' => 1, 'name' => 'ゲストルーム'],
            ['code' => 2, 'name' => '24時間ゴミ出し可'],
            ['code' => 3, 'name' => 'プール'],
            ['code' => 4, 'name' => 'エレベーター'],
            ['code' => 5, 'name' => '宅配ボックス'],
            ['code' => 6, 'name' => 'キッズルーム・託児所'],
            ['code' => 7, 'name' => '共用露天風呂'],
            ['code' => 8, 'name' => 'オートロック'],
        ];

        return $data;
    }

    public function forSaleDetail(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $params = $request->all();
        unset($params['_token'], $params['open']);
        // 対象の一覧取得
        //$article_list = Article::getSearchDataByCondition($request, 10, null, $request->is_close ? 1 : 2);
        $id = $request->detail_id;
        if ($request->detail_id == null) {
            $id = $request->id;
        }
        if (isset($request->only_items)) {
            $buyer = session('buyer_active');
            $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
            $article_list = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
            foreach ($article_list as $key => $row) {
                $article_list[$key] = $row->article;
            }
        } else {
            if ($request->print == 'all') {
                $data['hide_items'] = [];
                if (!empty($request->cookie('hide_items'))) {
                    $hideItems = $request->cookie('hide_items');
                    $hideItems = str_replace(['[', ']'], '', $hideItems);
                    $hideItems = explode(',', $hideItems);
                    $request->hide_items = $hideItems;
                }

                $params = $request->all();
                $article_list = [];
                $article_list = collect($article_list);
                if(isset($request->address1)) {
                    for ($i = 0; $i < count($request->address1); $i++) {
                        $newRequest = new \stdClass();
                        foreach ($request->all() as $key => $value) {
                            if ($key == 'land_condition') {
                                if (is_array($value)) {
                                    $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                                }
                            } else {
                                if (is_array($value)) {
                                    $newRequest->{$key} = [$value[$i] ?? null];
                                }
                            }
                        }
                        $newRequest->image = $request->get("image");
                        $newRequest->sort = $request->get("sort");
                        $newRequest->hide_items = $request->hide_items;
                        if (strpos(url()->full(), '/close/detail') !== false) {
                            $art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, null);
                        } else {
                            $art_list = Article::getSearchDataByCondition($newRequest, null, null, 2, null, null);
                        }
                        $article_list = $article_list->merge($art_list)->unique();
                    }
                }else{
                    $buyer = session('buyer_active');
                    $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
                    if(!empty($onlyItems)){
                        $article_list = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
                    }else{
                        $article_list = $buyer->articles()->get();
                    }
                    foreach ($article_list as $key => $row) {
                        $article_list[$key] = $row->article;
                    }
                }
            } else {
                $data['set_count'] = 1;
                $article_list = Article::whereIn("building_id", [$id])->get();
            }
        }
        $data["customs"] = $this->getCustomData();
        foreach ($article_list as $index => $item) {
            // 対象１件を取得
            $id = $request->action == null || $request->print_type == 'single' ? $id : $item->building_id;
            $item = Article::getDataById($id);

            $item->shared_building_id = $item->building_id;

            if($item->land_right == 2 or $item->land_right == 3){
                if($item->leasehold_period_year1 != null && $item->leasehold_period_month1 != null){
                    $dt = Carbon::create($item->leasehold_period_year1, $item->leasehold_period_month1)->lastOfMonth();
                    $now = Carbon::now()->firstOfMonth();
                    $df_new = $dt->diff($now);
                    $item->leasehold_period_year = $df_new->format('%Y');
                    $item->leasehold_period_month = $df_new->format('%m');
                }
            }

            if ($item->property == null) {
                $mansion = Article::getDataById($item->mansion_id);

                if ($mansion) {
                    $item->total_unit = $mansion->total_unit;
                    $item->management_form = $mansion->management_form ?? null;
                    $item->zip = $mansion->zip;

                    $item->use_area = $mansion->use_area ?? null;
                    $item->land_use = $mansion->land_use ?? null;
                    $item->land_use2 = $mansion->land_use2 ?? null;

                    $this->mapTrafficMansion($item, $mansion);

                    $item->city_plan = $mansion->city_plan;
                    $item->land_right = $mansion->land_right;
                    $item->land_area_val = $mansion->land_area_val;
                    $item->construction = $mansion->construction;
                    $item->construction_sub = $mansion->construction_sub;

                    $item->shared_building_id = $mansion->building_id;

                    $item->pet = $mansion->pet;
                    $item->pet_count = $mansion->pet_count;

                    $item->primary_school_school = $mansion->primary_school_school;
                    $item->primary_school2_school = $mansion->primary_school2_school;
                    $item->primary_school3_school = $mansion->primary_school3_school;

                    $item->secondary_school_school = $mansion->secondary_school_school;
                    $item->secondary_school2_school = $mansion->secondary_school2_school;
                    $item->secondary_school3_school = $mansion->secondary_school3_school;

                    $item->primary_school_distance = $mansion->primary_school_distance;
                    $item->primary_school_time = $mansion->primary_school_time;
                    $item->primary_school2_distance = $mansion->primary_school2_distance;
                    $item->primary_school2_time = $mansion->primary_school2_time;
                    $item->primary_school3_distance = $mansion->primary_school3_distance;
                    $item->primary_school3_time = $mansion->primary_school3_time;

                    $item->secondary_school_distance = $mansion->secondary_school_distance;
                    $item->secondary_school_time = $mansion->secondary_school_time;
                    $item->secondary_school2_distance = $mansion->secondary_school2_distance;
                    $item->secondary_school2_time = $mansion->secondary_school2_time;
                    $item->secondary_school3_distance = $mansion->secondary_school3_distance;
                    $item->secondary_school3_time = $mansion->secondary_school3_time;


                    if($mansion->land_right == 2 or $mansion->land_right == 3){
                        if($mansion->leasehold_period_year1 != null && $mansion->leasehold_period_month1 != null) {
                            $dt = Carbon::create($mansion->leasehold_period_year1, $mansion->leasehold_period_month1)->lastOfMonth();
                            $now = Carbon::now()->firstOfMonth();
                            $df_new = $dt->diff($now);
                            $item->leasehold_period_year = $df_new->format('%Y');
                            $item->leasehold_period_month = $df_new->format('%m');
                        }
                    }
                }
            }
            $item->name = $item->article_name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas_name = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "都市ガス";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 0) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 1) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }

            // 担当者
            $charge_info = UserInfo::getDataById($item->hp_charge);
            if ($charge_info != null) {
                $item->hp_charge_name = $charge_info->last_name . ' ' . $charge_info->first_name;
            } else {
                $item->hp_charge_name = "-";
            }

            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';

            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得

            $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);

            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

//            dump($id);
//            dump($photos);

            $item['photos'] = $photos;
            $item->file_path2 = $item->getFilePath2();
            //$item->vertical = $item->getVertical();

            // プロパティ名を取得
            if (!empty($mansion->property)) {
                $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
            } elseif (!empty($lot->property)) {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            } else {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            }

            // 周辺物件を取得
            if (!empty($item->mansion_id)) {
                $tmp_facilities = RelationArticleFacility::getData($item->mansion_id);
            } else {
                $tmp_facilities = RelationArticleFacility::getData($item->building_id);
            }
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];

            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                if(!empty($facility)){
                    $facility->disp_file = str_replace('public', '/storage', $facility->file_path);
                    if($facility->company_id == $user->company_id) {
                        $facilities[] = $facility;
                    }
                }

            }
            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

            // 都市計画
            if ($item->city_plan != 0) {
                $cp = MstCityPlan::where('code', '=', $item->city_plan)->first();
                if ($cp != null) {
                    $item->city_plan_name = $cp->name ?? '';
                }
            } else {
                $item->city_plan_name = '';
            }

            $item->city_plan_reason_name = optional(MstCityPlanReason::where("code", $item->city_plan_reason)->first())->name;
            // show kind_name
            $facility = RelationArticleFacility::getFacilityDataByRainsNoSort($item->building_id);
            foreach ($facility as &$row) {
                $row->kind_name = MstFacilityType::find($row->kind)->name ?? '';
            }
            $data['facility'] = $facility;

            // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);

            // // 表示中のページの最初のアイテムのインデックス番号
            // $first = $article_list->firstItem();

            // // 表示中のページの最後のアイテムのインデックス番号
            // $last = $article_list->lastItem();

            $flag = 0;
            if (isset($request->open)) {
                $flag = 1;
            }

            $item['facilities'] = $facilities;
            $item['relation_facilities'] = $tmp_facilities;
            $item['facility_count'] = $facility_count;
            $data['facility_count'] = $facility_count;

            $vendor_info = Vendor::getDataListByArticleId($item->building_id);

            $item['vendor_info'] = $vendor_info;

            if ($item->primary_school_school) {
                $school = MstSchool::where('code', $item->primary_school_school)->first();
                $item->primary_school_name = $school->name;
            }
            if ($item->primary_school2_school) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if ($item->primary_school3_school) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }

            if ($item->secondary_school_school) {
                $school = MstSchool::where('code', $item->secondary_school_school)->first();
                $item->secondary_school_name = $school->name;
            }
            if ($item->secondary_school2_school) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if ($item->secondary_school3_school) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }

            $item->water_supply_name = getWaterSupplyName($item->water_supply);
            $item->land_status_name = getCurrentStatusName($item->land_status);
            $item->spring_name = getSpringName($item);
            $item->council_name = getCouncilName($item);
            $item->another_cost = getAnotherCost($item);
            $item->price_histories = RelationPriceHistory::getHistoryById($item->building_id);
            $history = RelationPriceHistory::getHistoryById($item->building_id);
            $item->history = $history;
            $photosMansion = [];
            if (!empty($item->mansion_id)) {
                $photosMansion = RelationArticlePhoto::getDispImgByArticleId($item->mansion_id);
            }
            $item->photos_mansion = $photosMansion;

            $shared_text = "-";
            $checked_shared = RelationShared::getDataByArticleId($user->company_id, $item->shared_building_id);

            if (count($checked_shared) > 0) {
                $shared_text = '';
                foreach ($checked_shared as $row) {
                    $shared_item = MstShared::getName($row);
                    if ($shared_text != "") {
                        $shared_text .= " / ";
                    }
                    $shared_text .= $shared_item->name;
                }
            }
            $item->shared_text = $shared_text;

            $article_list[$index] = $item;

            if ($request->print_type == 'single' || !$request->action) {
                $article_list = [$item];
                break;
            }
        }


        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $data['disp_flag'] = $flag ?? 0;
        $data['article_list'] = $article_list;
        $data['item'] = $article_list[0];
        $data['photos'] = $article_list[0]->photos;

        $articleCurrent = $article_list[0] ?? null;
        $mansion = optional($articleCurrent)->getMansion();
        if ($mansion) {
            $photosOutdoor = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);

            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photosOutdoor[] = $photo;
            }
            $data['photosOutdoor'] = $photosOutdoor;
        } elseif (!empty($articleCurrent) && $articleCurrent->property == 4 || $articleCurrent->property == 5) {
            $photoIn = [];
            $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 2);
            foreach ($photosElmIn as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoIn[] = $photo;
            }
            $data['photoIn'] = $photoIn;

            $photoOut = [];
            $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 1);
            foreach ($photosElmOut as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoOut[] = $photo;
            }
            $data['photoOut'] = $photoOut;
        }

        $data['manner_name'] = $manner_name;
        if ($article_list[0]->shop != null) {
            $data['shop_name'] = MstStore::getDataByCode($user->company_id, $article_list[0]->shop)->name;
        }

        $data['params'] = $params;
        $data['params_single'] = ['target_id' => $request->detail_id];

        $data['count'] = count($article_list);
        $data['total_article'] = $request->get('total_article');
        // $data['first'] = $first;
        // $data['last'] = $last;
        // get address
//        dd($data);
        $store = auth()->user()->store;
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['tmp_facilities'] = $tmp_facilities;
        $printContentBlade = 'business.pages.print.for-sale.main-sheet';
        if ($request->tab == 'summary') {
            $printContentBlade = 'business.pages.print.for-sale.summary';
        } else if ($request->tab == 'pictures') {
            $printContentBlade = 'business.pages.print.for-sale.pictures';
        } else if ($request->tab == 'supplier_property') {
            $printContentBlade = 'business.pages.print.for-sale.supplier-property';
        }

        if ($request->action == null) {
            if (!empty($request->get('show_form_check'))) {
                $data['showNavSheetTab'] = true;
                $data['showFormCheck'] = true;
            }
            return view('business.pages.for-sale-detail', $data);
        }
        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView($printContentBlade, $data)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('forsale_list.pdf');
        }

        return view($printContentBlade, $data);
    }

    public function forSaleDetails(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $params = $request->all();

        unset($params['_token'], $params['open']);

        // 対象の一覧取得
        //$article_list = Article::getSearchDataByCondition($request, 10, null, $request->is_close ? 1 : 2);
        $id = $request->detail_id;
        if ($request->detail_id == null) {
            $id = $request->id;
        }
        $article_list = Article::whereIn("building_id", [$id])->get();
        $data["customs"] = $this->getCustomData();

        foreach ($article_list as $index => $item) {
            // 対象１件を取得
            $id = $request->action == null || $request->print_type == 'single' ? $id : $item->building_id;
            $item = Article::getDataById($id);

            $item->shared_building_id = $item->building_id;

            if ($item->property == null) {
                $mansion = Article::getDataById($item->mansion_id);

                if ($mansion) {
                    $item->total_unit = $mansion->total_unit;
                    $item->management_form = $mansion->management_form ?? null;
                    $item->use_area = $mansion->use_area ?? null;
                    $item->land_use = $mansion->land_use ?? null;
                    $item->land_use2 = $mansion->land_use2 ?? null;

                    $this->mapTrafficMansion($item, $mansion);

                    $item->city_plan = $mansion->city_plan;
                    $item->land_right = $mansion->land_right;
                    $item->land_area_val = $mansion->land_area_val;
                    $item->construction = $mansion->construction;
                    $item->construction_sub = $mansion->construction_sub;

                    $item->shared_building_id = $mansion->building_id;

                    $item->pet = $mansion->pet;
                    $item->pet_count = $mansion->pet_count;
                }
            }
            $item->name = $item->article_name;

            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas_name = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "都市ガス";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 0) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 1) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }

            // 担当者
            $charge_info = UserInfo::getDataById($item->hp_charge);
            if ($charge_info != null) {
                $item->hp_charge_name = $charge_info->last_name . ' ' . $charge_info->first_name;
            } else {
                $item->hp_charge_name = "-";
            }

            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';

            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得

            $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
//            dump($id);

            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

//            dump($id);
//            dump($photos);

            $item['photos'] = $photos;

            // プロパティ名を取得
            if (!empty($mansion->property)) {
                $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
            } elseif (!empty($lot->property)) {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            } else {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            }

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($item->building_id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

            // 都市計画
            if ($item->city_plan != 0) {
                $cp = MstCityPlan::where('code', '=', $item->city_plan)->first();
                if ($cp != null) {
                    $item->city_plan_name = $cp->name ?? '';
                }
            } else {
                $item->city_plan_name = '';
            }
            $item->city_plan_reason_name = optional(MstCityPlanReason::where("code", $item->city_plan_reason)->first())->name;


            // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);

            // // 表示中のページの最初のアイテムのインデックス番号
            // $first = $article_list->firstItem();

            // // 表示中のページの最後のアイテムのインデックス番号
            // $last = $article_list->lastItem();

            $flag = 0;
            if (isset($request->open)) {
                $flag = 1;
            }

            $item['facilities'] = $facilities;
            $item['relation_facilities'] = $tmp_facilities;
            $item['facility_count'] = $facility_count;
            $data['facility_count'] = $facility_count;

            $vendor_info = Vendor::getDataListByArticleId($item->building_id);

            $item['vendor_info'] = $vendor_info;

            if ($item->primary_school_school) {
                $school = MstSchool::where('code', $item->primary_school_school)->first();
                $item->primary_school_name = $school->name;
            }
            if ($item->primary_school2_school) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if ($item->primary_school3_school) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }

            if ($item->secondary_school_school) {
                $school = MstSchool::where('code', $item->secondary_school_school)->first();
                $item->secondary_school_name = $school->name;
            }
            if ($item->secondary_school2_school) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if ($item->secondary_school3_school) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }


            $item->water_supply_name = getWaterSupplyName($item->water_supply);
            $item->land_status_name = getCurrentStatusName($item->land_status);
            $item->spring_name = getSpringName($item);
            $item->council_name = getCouncilName($item);
            $item->another_cost = getAnotherCost($item);
            $item->price_histories = RelationPriceHistory::where('article_id', $item->building_id)->get();

            $shared_text = "-";
            $checked_shared = RelationShared::getDataByArticleId($user->company_id, $item->shared_building_id);

            if (count($checked_shared) > 0) {
                $shared_text = '';
                foreach ($checked_shared as $row) {
                    $shared_item = MstShared::getName($row);
                    if ($shared_text != "") {
                        $shared_text .= " / ";
                    }
                    $shared_text .= $shared_item->name;
                }
            }
            $item->shared_text = $shared_text;

            $article_list[$index] = $item;

            if ($request->print_type == 'single' || !$request->action) {
                $article_list = [$item];
                break;
            }
        }


        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $data['disp_flag'] = $flag ?? 0;
        $data['article_list'] = $article_list;
        $data['item'] = $article_list[0];
        $data['photos'] = $article_list[0]->photos;

        $articleCurrent = $article_list[0] ?? null;
        $mansion = optional($articleCurrent)->getMansion();
        if ($mansion) {
            $photosOutdoor = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);

            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photosOutdoor[] = $photo;
            }
            $data['photosOutdoor'] = $photosOutdoor;
        } elseif (!empty($articleCurrent) && $articleCurrent->property == 4 || $articleCurrent->property == 5) {
            $photoIn = [];
            $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 2);
            foreach ($photosElmIn as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoIn[] = $photo;
            }
            $data['photoIn'] = $photoIn;

            $photoOut = [];
            $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 1);
            foreach ($photosElmOut as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoOut[] = $photo;
            }
            $data['photoOut'] = $photoOut;
        }

        $data['manner_name'] = $manner_name;

        $data['shop'] = MstStore::getDataByCode($user->company_id, $article_list[0]->shop);
        $data['params'] = $params;
        $data['params_single'] = ['target_id' => $request->detail_id];


        $data['count'] = count($article_list);
        // $data['first'] = $first;
        // $data['last'] = $last;
        // get address
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);

        $printContentBlade = 'business.pages.print.for-sale.main-sheet';
        if ($request->tab == 'summary') {
            $printContentBlade = 'business.pages.print.for-sale.summary';
        } else if ($request->tab == 'pictures') {
            $printContentBlade = 'business.pages.print.for-sale.pictures';
        }

        if ($request->action == null) {
            return view('business.pages.for-sale-details', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView($printContentBlade, $data)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('forsale_list.pdf');
        }

        return view($printContentBlade, $data);

    }

    public function forSaleDetailPopup(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $params = $request->all();

        unset($params['_token'], $params['open']);

        // 対象の一覧取得
        //$article_list = Article::getSearchDataByCondition($request, 10, null, $request->is_close ? 1 : 2);
        $id = $request->detail_id;
        if ($request->detail_id == null) {
            $id = $request->id;
        }

        if ($request->print == "all") {
            if (isset($request->building_id_print) && !empty($request->building_id_print)) {
                $arrBuildingId = explode(',', $request->building_id_print);
                $article_list = Article::whereIn("building_id", $arrBuildingId)->get();
            }else{
                $lot = null;
                if(isset($request->property) && $request->property[0] == 99){
                    $lot = 1;
                }
                if(isset($request->room_lists)){
                    $newRequest = new \stdClass();
                    $newRequest->only_items = $request->room_lists;
                    $article_list = Article::getSearchDataByCondition($newRequest, null, null, $request->is_close ? 1 : null, null, $lot);
                }else{
                    $article_list = Article::getSearchDataByCondition($request, null, null, $request->is_close ? 1 : null, null, $lot);
                }
            }
        } else {
            $article_list = Article::whereIn("building_id", [$id])->get();
        }
        $data["customs"] = $this->getCustomData();

        $tmp_facilities = [];

        foreach ($article_list as $index => $item) {
            // 対象１件を取得
            $id = $request->action == null || $request->print_type == 'single' ? $id : $item->building_id;
            $item = Article::getDataById($id);

            $item->shared_building_id = $item->building_id;
            if ($item->property == null) {
                $params['property'] = null;
                $mansion = Article::getDataById($item->mansion_id);
                if ($mansion) {
                    $item->total_unit = $mansion->total_unit;
                    $item->management_form = $mansion->management_form ?? null;
                    $item->use_area = $mansion->use_area ?? null;
                    $item->land_use = $mansion->land_use ?? null;
                    $item->land_use2 = $mansion->land_use2 ?? null;

                    $this->mapTrafficMansion($item, $mansion);

                    $item->city_plan = $mansion->city_plan;
                    $item->land_right = $mansion->land_right;
                    $item->land_area_val = $mansion->land_area_val;
                    $item->construction = $mansion->construction;
                    $item->construction_sub = $mansion->construction_sub;

                    $item->shared_building_id = $mansion->building_id;
                    $item->address = $mansion->address;
                    $item->pet = $mansion->pet;
                    $item->pet_count = $mansion->pet_count;
                    $item->underground = $mansion->underground;
                    $sold = 1;
                    if(in_array($item->status, [4,5])){
                        $sold = 2;
                    }
                    $rooms = Article::getDataByRoom($item->mansion_id, $sold);
                    $room_lists = [];
                    foreach ($rooms as $room){
                        $room_lists[] = $room->building_id;
                    }
                    $params['room_lists'] = implode(',', $room_lists);
                }
            }
            if (in_array($item->property, [4,5])) {
                $lot_list = Article::getLotList($item->sale_no);
                $room_lists = [];
                foreach ($lot_list as $room){
                    $room_lists[] = $room->building_id;
                }
                $params['room_lists'] = implode(',', $room_lists);
            }

            $item->name = $item->article_name;

            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas_name = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "都市ガス";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 0) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 1) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }

            // 担当者
            $charge_info = UserInfo::getDataById($item->hp_charge);
            if ($charge_info != null) {
                $item->hp_charge_name = $charge_info->last_name . ' ' . $charge_info->first_name;
            } else {
                $item->hp_charge_name = "-";
            }

            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseDistrict::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';

            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得

            $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
//            dump($id);

            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

//            dump($id);
//            dump($photos);

            $item['photos'] = $photos;

            $photosMansion = [];
            if (!empty($item->mansion_id)) {
                $photosMansion = RelationArticlePhoto::getDispImgByArticleId($item->mansion_id);
            }
            $item->photos_mansion = $photosMansion;

            // プロパティ名を取得
            if (!empty($mansion->property)) {
                $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
            } elseif (!empty($lot->property)) {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            } else {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            }

            // 周辺物件を取得
            if (!empty($item->mansion_id)) {
                $tmp_facilities = RelationArticleFacility::getData($item->mansion_id);
            } else {
                $tmp_facilities = RelationArticleFacility::getData($item->building_id);
            }
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

            // 都市計画
            if ($item->city_plan != 0) {
                $cp = MstCityPlan::where('code', '=', $item->city_plan)->first();
                if ($cp != null) {
                    $item->city_plan_name = $cp->name ?? '';
                }
            } else {
                $item->city_plan_name = '';
            }

            $item->city_plan_reason_name = optional(MstCityPlanReason::where("code", $item->city_plan_reason)->first())->name;


            // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);

            // // 表示中のページの最初のアイテムのインデックス番号
            // $first = $article_list->firstItem();

            // // 表示中のページの最後のアイテムのインデックス番号
            // $last = $article_list->lastItem();

            $flag = 0;
            if (isset($request->open)) {
                $flag = 1;
            }

            $item['facilities'] = $facilities;
            $item['relation_facilities'] = $tmp_facilities;
            $item['facility_count'] = $facility_count;
            $data['facility_count'] = $facility_count;

            $vendor_info = Vendor::getDataListByArticleId($item->building_id);

            $item['vendor_info'] = $vendor_info;

            if ($item->primary_school_school) {
                $school = MstSchool::where('code', $item->primary_school_school)->first();
                $item->primary_school_name = $school->name;
            }
            if ($item->primary_school2_school) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if ($item->primary_school3_school) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }

            if ($item->secondary_school_school) {
                $school = MstSchool::where('code', $item->secondary_school_school)->first();
                $item->secondary_school_name = $school->name;
            }
            if ($item->secondary_school2_school) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if ($item->secondary_school3_school) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }


            $item->water_supply_name = getWaterSupplyName($item->water_supply);
            $item->land_status_name = getCurrentStatusName($item->land_status);
            $item->spring_name = getSpringName($item);
            $item->council_name = getCouncilName($item);
            $item->another_cost = getAnotherCost($item);
            $item->price_histories = RelationPriceHistory::where('article_id', $item->building_id)->get();

            $shared_text = "-";
            $checked_shared = RelationShared::getDataByArticleId($user->company_id, $item->shared_building_id);

            if (count($checked_shared) > 0) {
                $shared_text = '';
                foreach ($checked_shared as $row) {
                    $shared_item = MstShared::getName($row);
                    if ($shared_text != "") {
                        $shared_text .= " / ";
                    }
                    $shared_text .= $shared_item->name;
                }
            }
            $item->shared_text = $shared_text;

            $article_list[$index] = $item;

            if ($request->print_type == 'single' || !$request->action) {
                $article_list = [$item];
                break;
            }
        }


        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;
        $data['tmp_facilities'] = $tmp_facilities;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        $data['disp_flag'] = $flag ?? 0;
        $data['article_list'] = $article_list;
        $data['item'] = $article_list[0];
        $data['photos'] = $article_list[0]->photos;

        $articleCurrent = $article_list[0] ?? null;
        $mansion = optional($articleCurrent)->getMansion();
        if ($mansion) {
            $photosOutdoor = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);

            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photosOutdoor[] = $photo;
            }
            $data['photosOutdoor'] = $photosOutdoor;
        } elseif (!empty($articleCurrent) && $articleCurrent->property == 4 || $articleCurrent->property == 5) {
            $photoIn = [];
            $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 2);
            foreach ($photosElmIn as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoIn[] = $photo;
            }
            $data['photoIn'] = $photoIn;

            $photoOut = [];
            $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($articleCurrent->building_id, 1);
            foreach ($photosElmOut as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoOut[] = $photo;
            }
            $data['photoOut'] = $photoOut;
        }

        $data['manner_name'] = $manner_name;

        //$data['shop'] = MstStore::getDataByCode($user->company_id, $article_list[0]->shop);
        //            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['shop'] = $shop;
        $data['params'] = $params;
        $data['params_single'] = ['target_id' => $id];

        $data['count'] = count($article_list);
        // $data['first'] = $first;
        // $data['last'] = $last;
        // get address
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);

        $printContentBlade = 'business.pages.print.for-sale.main-sheet';
        if ($request->tab == 'summary') {
            $printContentBlade = 'business.pages.print.for-sale.summary';
        } else if ($request->tab == 'pictures') {
            $printContentBlade = 'business.pages.print.for-sale.pictures';
        }

        if ($request->action == null) {
            return view('business.pages.for-sale-detail-popup', $data);
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView($printContentBlade, $data)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('forsale_list.pdf');
        }

        return view($printContentBlade, $data);

    }


    private function mapTrafficMansion($article, $mansion)
    {
        $article->main_traffic = $mansion->main_traffic;
        $article->main_traffic_line = $mansion->main_traffic_line;
        $article->main_traffic_station = $mansion->main_traffic_station;
        $article->main_traffic_time = $mansion->main_traffic_time;
        $article->main_traffic_bus = $mansion->main_traffic_bus;
        $article->main_traffic_bus_time = $mansion->main_traffic_bus_time;
        $article->main_traffic_bus_walk = $mansion->main_traffic_bus_walk;

        $article->sub_traffic1 = $mansion->sub_traffic1;
        $article->sub_traffic1_line = $mansion->sub_traffic1_line;
        $article->sub_traffic1_station = $mansion->sub_traffic1_station;
        $article->sub_traffic1_time = $mansion->sub_traffic1_time;
        $article->sub_traffic1_bus = $mansion->sub_traffic1_bus;
        $article->sub_traffic1_bus_time = $mansion->sub_traffic1_bus_time;
        $article->sub_traffic1_bus_walk = $mansion->sub_traffic1_bus_walk;

        $article->sub_traffic2 = $mansion->sub_traffic2;
        $article->sub_traffic2_line = $mansion->sub_traffic2_line;
        $article->sub_traffic2_station = $mansion->sub_traffic2_station;
        $article->sub_traffic2_time = $mansion->sub_traffic2_time;
        $article->sub_traffic2_bus = $mansion->sub_traffic2_bus;
        $article->sub_traffic2_bus_time = $mansion->sub_traffic2_bus_time;
        $article->sub_traffic2_bus_walk = $mansion->sub_traffic2_bus_walk;
    }

    public function forSaleSinglePrint(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);
        // 管理形態


        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        // ガス・オール電化
        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
        if ($item->land_condition == 1) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 2) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }


        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $item->shop = $shop;

        $data['shop'] = $shop;
        $item->manner_name = $manner_name;

        $data['items'][0] = $item;
        $data['article_list'] = [$item];
        $data['url'] = url('');


        if ($request->summary == '1') {
            return view('business.pages.for-sale-print-summary', $data);
        }

        return view('business.pages.print.for-sale.main-sheet', $data);
    }

    public function forSaleMultiPrint(Request $request)
    {
        $user = Auth::user();
        // only select these items
        // $request->only_items = '2680';
        $tmp_con = $request->all();
        $condition = $this->createConditionList($tmp_con);
            $perPage = !empty($request->get('per_page')) ? null : 30;

            if ($request->print == 'all') {
                $perPage = 9999;
            }

            $data['hide_items'] = [];
            if (!empty($request->cookie('hide_items'))) {
                $hideItems = $request->cookie('hide_items');
                $hideItems = str_replace(['[', ']'], '', $hideItems);
                $hideItems = explode(',', $hideItems);
                $data['hide_items'] = $hideItems;
                $request->hide_items = $hideItems;
            }

            $params = $request->all();
            unset($params['page']);
            unset($params['notcondition']);

            $article_list = [];
            $article_list = collect($article_list);
            //dd($request->all());
            if(isset($request->address1)){
                for ($i = 0; $i < count($request->address1); $i++) {
                    $newRequest = new \stdClass();
                    foreach ($request->all() as $key => $value) {
                        if ($key == 'land_condition') {
                            if (is_array($value)) {
                                $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                            }
                        } else {
                            if (is_array($value)) {
                                $newRequest->{$key} = [$value[$i] ?? null];
                            }
                        }
                    }
                    $newRequest->image = $request->get("image");
                    $newRequest->sort = $request->get("sort");
                    $newRequest->hide_items = $request->hide_items;
                    $lot = null;
                    if(isset($newRequest->property) && $newRequest->property[0] == 99){
                        $lot = 1;
                    }
                    if (isset($request->close) && $request->close == 1) {
                        $art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, $lot);
                    } else {
                        if(isset($request->room_lists)){
                            $newRequest = new \stdClass();
                            $newRequest->only_items = $request->room_lists;
                        }
                        $art_list = Article::getSearchDataByCondition($newRequest, null, null, 2, null, $lot);
                    }
                    $article_list = $article_list->merge($art_list)->unique();
                }
            }else{
                $buyer = session('buyer_active');
                $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
                if(!empty($onlyItems)){
                    $buyer_article = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
                }else{
                    $buyer_article = $buyer->articles()->get();
                }
                $newOnlyItems = [];
                foreach ($buyer_article as $key => $row) {
                    $newOnlyItems[] = $row['article_id'];
                }
                $request->only_items = implode(',', $newOnlyItems);
                $art_list = Article::getSearchDataByCondition($request, null, null, 2, null, null);

                $article_list = $article_list->merge($art_list)->unique();
            }

        if(!isset($total)){
            $total = $article_list->count();
        }
        $data['total'] = $total;
        //dd($tmp_con);
        foreach ($article_list as $index => &$item) {
            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';
            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
//            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
//            if ($item->land_condition == 1) {
//                $item->land_condition_name = "条件なし";
//            } elseif ($item->land_condition == 2) {
//                $item->land_condition_name = "建築条件付き";
//            } else {
//                $item->land_condition_name = "-";
//            }
            if ($item->land_condition == 0) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 1) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }

            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
            $photos = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photos[] = $photo;
            }
            $item->photos = $photos;

            $item->dataLand = $item;
//            $mansion = $item->getMansion();
            $mansion = Article::getDataById($item->mansion_id);
//            dd($mansion);
            if ($mansion) {
                $photosOutdoor = [];
                $item->dataLand = $mansion;
                $item->mansion = $mansion;
                $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);
//                dd($photosElm);
                foreach ($photosElm as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photosOutdoor[] = $photo;
                }

                $item->photosOutdoor = $photosOutdoor;

                $item->total_unit = $mansion->total_unit;
                $item->management_form = $mansion->management_form ?? null;
                $item->zip = $mansion->zip;

                $item->use_area = $mansion->use_area ?? null;
                $item->land_use = $mansion->land_use ?? null;
                $item->land_use2 = $mansion->land_use2 ?? null;

                $this->mapTrafficMansion($item, $mansion);

                $item->city_plan = $mansion->city_plan;
                $item->land_right = $mansion->land_right;
                $item->land_area_val = $mansion->land_area_val;
                $item->construction = $mansion->construction;
                $item->construction_sub = $mansion->construction_sub;

                $item->shared_building_id = $mansion->building_id;

                $item->pet = $mansion->pet;
                $item->pet_count = $mansion->pet_count;

                $item->primary_school_school = $mansion->primary_school_school;
                $item->primary_school2_school = $mansion->primary_school2_school;
                $item->primary_school3_school = $mansion->primary_school3_school;

                $item->secondary_school_school = $mansion->secondary_school_school;
                $item->secondary_school2_school = $mansion->secondary_school2_school;
                $item->secondary_school3_school = $mansion->secondary_school3_school;

                $item->primary_school_distance = $mansion->primary_school_distance;
                $item->primary_school_time = $mansion->primary_school_time;
                $item->primary_school2_distance = $mansion->primary_school2_distance;
                $item->primary_school2_time = $mansion->primary_school2_time;
                $item->primary_school3_distance = $mansion->primary_school3_distance;
                $item->primary_school3_time = $mansion->primary_school3_time;

                $item->secondary_school_distance = $mansion->secondary_school_distance;
                $item->secondary_school_time = $mansion->secondary_school_time;
                $item->secondary_school2_distance = $mansion->secondary_school2_distance;
                $item->secondary_school2_time = $mansion->secondary_school2_time;
                $item->secondary_school3_distance = $mansion->secondary_school3_distance;
                $item->secondary_school3_time = $mansion->secondary_school3_time;

            } elseif (!empty($item) && $item->property == 4 || $item->property == 5) {
                $photoIn = [];
                $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 2);
                foreach ($photosElmIn as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photoIn[] = $photo;
                }
                $item->photoIn = $photoIn;

                $photoOut = [];
                $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 1);
                foreach ($photosElmOut as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photoOut[] = $photo;
                }
                $item->photoOut = $photoOut;
            }
            // プロパティ名を取得
            if (!empty($item->mansion->property)) {
                $item->property_name = $this->getPropertyName($item->mansion->property, $item->mansion->property_sub);
            } elseif (!empty($lot->property)) {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            } else {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            }
            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            $item->shop_name = $shop;
            $item->shop = $shop;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';

            if ($item->primary_school_school) {
                $school = MstSchool::where('code', $item->primary_school_school)->first();
                $item->primary_school_name = $school->name;
            }
            if ($item->primary_school2_school) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if ($item->primary_school3_school) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }

            if ($item->secondary_school_school) {
                $school = MstSchool::where('code', $item->secondary_school_school)->first();
                $item->secondary_school_name = $school->name;
            }
            if ($item->secondary_school2_school) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if ($item->secondary_school3_school) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }
            $item->file_path2 = $item->getFilePath2();
            //$item->vertical = $item->getVertical();
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);
        if(isset($item)){
            $item->manner = $manner;
        }

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();
        $data['condition'] = $condition;
// dump($article_list);
        $data['items'] = $article_list;
        $user = Auth::user();
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['url'] = url('');
        $manner_name = '';
        if(isset($item)) {
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        }
        $data['manner_name'] = $manner_name;

        return view('business.pages.for-sale-print', $data);
    }


    public function forSaleSinglePrintVendor(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $data['hide_items'] = [];
        if (!empty($request->cookie('hide_items'))) {
            $hideItems = $request->cookie('hide_items');
            $hideItems = str_replace(['[', ']'], '', $hideItems);
            $hideItems = explode(',', $hideItems);
            $data['hide_items'] = $hideItems;
        }
        $data['set_count'] = 1;
        // 対象１件を取得
        $item = Article::getDataById($request->target_id);

        // 業者シート画像
        if(!empty($item->sheet_file_path2)){
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
        }

        $data['items'][0] = $item;

        return view('business.pages.for-sale-print-vendor', $data);
    }

    public function forSaleMultiPrintVendor(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $perPage = 30;
        $data['hide_items'] = [];
        if (!empty($request->cookie('hide_items'))) {
            $hideItems = $request->cookie('hide_items');
            $hideItems = str_replace(['[', ']'], '', $hideItems);
            $hideItems = explode(',', $hideItems);
            $data['hide_items'] = $hideItems;
            $request->hide_items = $hideItems;
        }
        if ($request->print == 'all') {
            $perPage = 9999;
        }

        $params = $request->all();
        unset($params['page']);
        unset($params['notcondition']);

        $article_list = [];
        $article_list = collect($article_list);
        if(isset($request->address1)) {
            for ($i = 0; $i < count($request->address1); $i++) {
                $newRequest = new \stdClass();
                foreach ($request->all() as $key => $value) {
                    if ($key == 'land_condition') {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                        }
                    } else {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i] ?? null];
                        }
                    }
                }
                $newRequest->image = $request->get("image");
                $newRequest->sort = $request->get("sort");
                $newRequest->hide_items = $request->hide_items;
                $lot = null;
                if(isset($newRequest->property) && $newRequest->property[0] == 99){
                    $lot = 1;
                }
                if (isset($request->close) && $request->close == 1) {
                    $art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, $lot);
                } else {
                    if(isset($request->room_lists)){
                        $newRequest = new \stdClass();
                        $newRequest->only_items = $request->room_lists;
                    }
                    $art_list = Article::getSearchDataByCondition($newRequest, null, null, 2, null, $lot);
                }
                $article_list = $article_list->merge($art_list)->unique();
                $data['total'] = $article_list->count();
            }
        }else{
            $buyer = session('buyer_active');
            $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
            if(!empty($onlyItems)){
                $buyer_article = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
            }else{
                $buyer_article = $buyer->articles()->get();
            }
            $newOnlyItems = [];
            foreach ($buyer_article as $key => $row) {
                $newOnlyItems[] = $row['article_id'];
            }
            $request->only_items = implode(',', $newOnlyItems);
            $art_list = Article::getSearchDataByCondition($request, null, null, 2, null, null);

            $article_list = $article_list->merge($art_list)->unique();
            $data['total'] = count($article_list);
        }

        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
        }

        $data['items'] = $article_list;

        return view('business.pages.for-sale-print-vendor', $data);
    }

    public function forSaleSinglePdf(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得

        $item = Article::getDataById($request->target_id);
        // 管理形態
        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        // ガス・オール電化
        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
        if ($item->land_condition == 1) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 2) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }


        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['shop'] = $shop;
        $data['manner_name'] = $manner_name;

        $data['items'][0] = $item;

        $data['url'] = url('');


        if ($request->summary == '1') {
            $pdf = \PDF::loadView('business.pages.for-sale-print-summary', $data);
        } else {
            $pdf = \PDF::loadView('business.pages.for-sale-pdf', $data)
                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');
        }

        // $pdf->setOptions(['orientation' => 'Landscape']);

        return $pdf->stream('title.pdf');

        // return view('business.pages.for-sale-pdf', $data);
    }


    public function forSaleMultiPdf(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);

        foreach ($article_list as $index => &$item) {

            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 1) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 2) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }


            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';


            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
            $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

            // プロパティ名を取得
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            //$item->shop = $shop;
            $item->shop_name = $shop;
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        // return view('business.pages.for-sale-pdf', $data);
    }

    public function forSaleSinglePdfVendor(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        $data['items'][0] = $item;
        $data['url'] = url('');

        // return view('business.pages.for-sale-pdf-vendor', $data);

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-vendor', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける
    }

    public function forSaleMultiPdfVendor(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
        }

        $data['items'] = $article_list;
        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-vendor', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける
    }

    public function forSaleMultiPrintSet1(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $data['hide_items'] = [];
        if (!empty($request->cookie('hide_items'))) {
            $hideItems = $request->cookie('hide_items');
            $hideItems = str_replace(['[', ']'], '', $hideItems);
            $hideItems = explode(',', $hideItems);
            $request->hide_items = $hideItems;
            $data['hide_items'] = $hideItems;
        }

        $params = $request->all();
        $article_list = [];
        $article_list = collect($article_list);
        if(isset($request->address1)){
            for ($i = 0; $i < count($request->address1); $i++) {
                $newRequest = new \stdClass();
                foreach ($request->all() as $key => $value) {
                    if ($key == 'land_condition') {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i * 2] ?? null, $value[$i * 2 + 1] ?? null];
                        }
                    } else {
                        if (is_array($value)) {
                            $newRequest->{$key} = [$value[$i] ?? null];
                        }
                    }
                }
                $newRequest->image = $request->get("image");
                $newRequest->sort = $request->get("sort");
                $newRequest->hide_items = $request->hide_items;
                $lot = null;
                if(isset($newRequest->property) && $newRequest->property[0] == 99){
                    $lot = 1;
                }
                if (isset($request->close) && $request->close == 1) {
                    $art_list = Article::getSearchDataByCondition($newRequest, null, null, 1, null, $lot);
                } else {
                    if(isset($request->room_lists)){
                        $newRequest = new \stdClass();
                        $newRequest->only_items = $request->room_lists;
                    }
                    $art_list = Article::getSearchDataByCondition($newRequest, null, null, 2, null, $lot);
                }
                $article_list = $article_list->merge($art_list)->unique();
            }
        }else{
            $buyer = session('buyer_active');
            $onlyItems = !empty($request->get('only_items')) ? $request->get('only_items') : [];
            if(!empty($onlyItems)){
                $buyer_article = $buyer->articles()->orderByRaw("FIELD(article_id, $onlyItems)")->get();
            }else{
                $buyer_article = $buyer->articles()->get();
            }
            $newOnlyItems = [];
            foreach ($buyer_article as $key => $row) {
                $newOnlyItems[] = $row['article_id'];
            }
            $request->only_items = implode(',', $newOnlyItems);
            $art_list = Article::getSearchDataByCondition($request, null, null, 2, null, null);

            $article_list = $article_list->merge($art_list)->unique();
        }

        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
//            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
//            if ($item->land_condition == 1) {
//                $item->land_condition_name = "条件なし";
//            } elseif ($item->land_condition == 2) {
//                $item->land_condition_name = "建築条件付き";
//            } else {
//                $item->land_condition_name = "-";
//            }

            if ($item->land_condition == 0) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 1) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }



            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';


            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
//            $photos = [];
            $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
//            dd($photos);
            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }
            $item->photos = $photos;
            $dataLand = $item;
            $mansion = $item->getMansion();
            if ($mansion) {
                $item->total_unit = $mansion->total_unit;
                $item->management_form = $mansion->management_form ?? null;
                $item->zip = $mansion->zip;

                $item->use_area = $mansion->use_area ?? null;
                $item->land_use = $mansion->land_use ?? null;
                $item->land_use2 = $mansion->land_use2 ?? null;

                $this->mapTrafficMansion($item, $mansion);

                $item->city_plan = $mansion->city_plan;
                $item->land_right = $mansion->land_right;
                $item->land_area_val = $mansion->land_area_val;
                $item->construction = $mansion->construction;
                $item->construction_sub = $mansion->construction_sub;

                $item->shared_building_id = $mansion->building_id;

                $item->pet = $mansion->pet;
                $item->pet_count = $mansion->pet_count;
                $item->primary_school_school = $mansion->primary_school_school;
                $item->primary_school2_school = $mansion->primary_school2_school;
                $item->primary_school3_school = $mansion->primary_school3_school;

                $item->secondary_school_school = $mansion->secondary_school_school;
                $item->secondary_school2_school = $mansion->secondary_school2_school;
                $item->secondary_school3_school = $mansion->secondary_school3_school;

                $item->primary_school_distance = $mansion->primary_school_distance;
                $item->primary_school_time = $mansion->primary_school_time;
                $item->primary_school2_distance = $mansion->primary_school2_distance;
                $item->primary_school2_time = $mansion->primary_school2_time;
                $item->primary_school3_distance = $mansion->primary_school3_distance;
                $item->primary_school3_time = $mansion->primary_school3_time;

                $item->secondary_school_distance = $mansion->secondary_school_distance;
                $item->secondary_school_time = $mansion->secondary_school_time;
                $item->secondary_school2_distance = $mansion->secondary_school2_distance;
                $item->secondary_school2_time = $mansion->secondary_school2_time;
                $item->secondary_school3_distance = $mansion->secondary_school3_distance;
                $item->secondary_school3_time = $mansion->secondary_school3_time;
                $dataLand = $mansion;
                $data['mansion'] = $mansion;
                $photosOutdoor = [];
                $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);

                foreach ($photosElm as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photosOutdoor[] = $photo;
                }
                $item->photosOutdoor = $photosOutdoor;
            } elseif (!empty($item) && $item->property == 4 || $item->property == 5) {
                $photoIn = [];
                $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 2);
                foreach ($photosElmIn as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photoIn[] = $photo;
                }
                $item->photoIn = $photoIn;

                $photoOut = [];
                $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 1);
                foreach ($photosElmOut as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    $photoOut[] = $photo;
                }
                $item->photoOut = $photoOut;
            }
            $item->dataLand = $dataLand;

            // プロパティ名を取得
            if (!empty($mansion->property)) {
                $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
            } elseif (!empty($lot->property)) {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            } else {
                $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
            }

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            $item->shop = $shop;

            if ($item->primary_school_school) {
                $school = MstSchool::where('code', $item->primary_school_school)->first();
                $item->primary_school_name = $school->name;
            }
            if ($item->primary_school2_school) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if ($item->primary_school3_school) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }

            if ($item->secondary_school_school) {
                $school = MstSchool::where('code', $item->secondary_school_school)->first();
                $item->secondary_school_name = $school->name;
            }
            if ($item->secondary_school2_school) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if ($item->secondary_school3_school) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
            $item->file_path2 = $item->getFilePath2();
            //$item->vertical = $item->getVertical();
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

// dump($article_list);
        $user = Auth::user();
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['items'] = $article_list;
        $data['url'] = url('');
        if (isset($item)) {
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        } else {
            $manner_name = "";
        }
        $data['manner_name'] = $manner_name;
        // $pdf = \PDF::loadView('business.pages.for-sale-pdf-set1', $data);
        // return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        return view('business.pages.for-sale-pdf-set1', $data);
    }

    public function forSaleSinglePrintSet1(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);
        // 管理形態
        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        $data['set_count'] = 1;
        // ガス・オール電化
//        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
//        if ($item->land_condition == 1) {
//            $item->land_condition_name = "条件なし";
//        } elseif ($item->land_condition == 2) {
//            $item->land_condition_name = "建築条件付き";
//        } else {
//            $item->land_condition_name = "-";
//        }
        if ($item->land_condition == 0) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 1) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }

        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
        $item->file_path2 = str_replace('public', '/storage', $item->file_path2);

        // 写真画像の取得

        $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }
        $item->photos = $photos;
        $dataLand = $item;
        $mansion = $item->getMansion();
        if ($mansion) {
            $dataLand = $mansion;
            $data['mansion'] = $mansion;
            $photosOutdoor = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($mansion->building_id);

            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photosOutdoor[] = $photo;
            }
            $item->photosOutdoor = $photosOutdoor;
//            dd($data['photosOutdoor']);

            $item->total_unit = $mansion->total_unit;
            $item->management_form = $mansion->management_form ?? null;
            $item->zip = $mansion->zip;

            $item->use_area = $mansion->use_area ?? null;
            $item->land_use = $mansion->land_use ?? null;
            $item->land_use2 = $mansion->land_use2 ?? null;

            $this->mapTrafficMansion($item, $mansion);

            $item->city_plan = $mansion->city_plan;
            $item->land_right = $mansion->land_right;
            $item->land_area_val = $mansion->land_area_val;
            $item->construction = $mansion->construction;
            $item->construction_sub = $mansion->construction_sub;

            $item->shared_building_id = $mansion->building_id;

            $item->pet = $mansion->pet;
            $item->pet_count = $mansion->pet_count;
            $item->primary_school_school = $mansion->primary_school_school;
            $item->primary_school2_school = $mansion->primary_school2_school;
            $item->primary_school3_school = $mansion->primary_school3_school;

            $item->secondary_school_school = $mansion->secondary_school_school;
            $item->secondary_school2_school = $mansion->secondary_school2_school;
            $item->secondary_school3_school = $mansion->secondary_school3_school;

            $item->primary_school_distance = $mansion->primary_school_distance;
            $item->primary_school_time = $mansion->primary_school_time;
            $item->primary_school2_distance = $mansion->primary_school2_distance;
            $item->primary_school2_time = $mansion->primary_school2_time;
            $item->primary_school3_distance = $mansion->primary_school3_distance;
            $item->primary_school3_time = $mansion->primary_school3_time;

            $item->secondary_school_distance = $mansion->secondary_school_distance;
            $item->secondary_school_time = $mansion->secondary_school_time;
            $item->secondary_school2_distance = $mansion->secondary_school2_distance;
            $item->secondary_school2_time = $mansion->secondary_school2_time;
            $item->secondary_school3_distance = $mansion->secondary_school3_distance;
            $item->secondary_school3_time = $mansion->secondary_school3_time;
        } elseif (!empty($item) && $item->property == 4 || $item->property == 5) {
            $photoIn = [];
            $photosElmIn = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 2);
            foreach ($photosElmIn as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoIn[] = $photo;
            }
            $data['photoIn'] = $photoIn;

            $photoOut = [];
            $photosElmOut = RelationArticlePhoto::getDispImgByArticleId($item->building_id, 1);
            foreach ($photosElmOut as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photoOut[] = $photo;
            }
            $data['photoOut'] = $photoOut;
        }
        $item->dataLand = $dataLand;

        if ($item->primary_school_school) {
            $school = MstSchool::where('code', $item->primary_school_school)->first();
            $item->primary_school_name = $school->name;
        }
        if ($item->primary_school2_school) {
            $school = MstSchool::where('code', $item->primary_school2_school)->first();
            $item->primary_school_name2 = $school->name;
        }
        if ($item->primary_school3_school) {
            $school = MstSchool::where('code', $item->primary_school3_school)->first();
            $item->primary_school_name3 = $school->name;
        }

        if ($item->secondary_school_school) {
            $school = MstSchool::where('code', $item->secondary_school_school)->first();
            $item->secondary_school_name = $school->name;
        }
        if ($item->secondary_school2_school) {
            $school = MstSchool::where('code', $item->secondary_school2_school)->first();
            $item->secondary_school_name2 = $school->name;
        }
        if ($item->secondary_school3_school) {
            $school = MstSchool::where('code', $item->secondary_school3_school)->first();
            $item->secondary_school_name3 = $school->name;
        }
        // プロパティ名を取得
        if (!empty($mansion->property)) {
            $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);
        } elseif (!empty($lot->property)) {
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
        } else {
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);
        }

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $item->manner = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $item->shop = $shop;

        $data['shop'] = $shop;
        $item->manner_name = $manner_name;
        $data['items'][0] = $item;
        $data['url'] = url('');

        return view('business.pages.for-sale-pdf-set1', $data);
    }

    public function forSaleMultiPdfSet1(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 1) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 2) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }


            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';


            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
            $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

            // プロパティ名を取得
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            $item->shop_name = $shop;

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set1', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        // return view('business.pages.for-sale-pdf', $data);
    }

    public function forSaleSinglePdfSet1(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);
        // 管理形態
        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        // ガス・オール電化
        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
        if ($item->land_condition == 1) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 2) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }


        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id); //$item->shop
        $data['shop'] = $shop;
        $data['manner_name'] = $manner_name;

        $data['items'][0] = $item;

        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set1', $data)
            ->setPaper('A4', 'landscape')
            ->setOption('user-style-sheet', base_path() . '/public/css/app.css');
        // $pdf->setOptions(['orientation' => 'Landscape']);

        return $pdf->stream('title.pdf');

        // return view('business.pages.for-sale-pdf', $data);
    }

    public function forSaleMultiPrintSet2(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 対象の一覧取得
            $item->room_list = Article::getDataByRoom($item->building_id, 1);
            $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
            foreach ($item->sold_room_list as &$room) {
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = "-";
                }
            }
        }

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');
        $user = Auth::user();
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        $data['manner_name'] = $manner_name;

        // $pdf = \PDF::loadView('business.pages.for-sale-pdf-set1', $data);
        // return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        return view('business.pages.for-sale-pdf-set2', $data);
    }

    public function forSaleSinglePrintSet2(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 対象の一覧取得
        $item->room_list = Article::getDataByRoom($item->building_id, 1);
        $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
        foreach ($item->sold_room_list as &$room) {
            if ($room->land_area_val != null && $room->land_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
            } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
            } else {
                $room->tsubo = "-";
            }
        }

        $data['items'][0] = $item;
        $user = Auth::user();
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        $data['manner_name'] = $manner_name;

        $data['url'] = url('');

        return view('business.pages.for-sale-pdf-set2', $data);
    }

    public function forSaleMultiPdfSet2(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 対象の一覧取得
            $item->room_list = Article::getDataByRoom($item->building_id, 1);
            $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
            foreach ($item->sold_room_list as &$room) {
                // 坪単価
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = 0;
                }
            }
        }

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set2', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        // return view('business.pages.for-sale-pdf', $data);
    }

    public function forSaleSinglePdfSet2(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);

        // 対象の一覧取得
        $item->room_list = Article::getDataByRoom($item->building_id, 1);
        $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
        foreach ($item->sold_room_list as &$room) {
            if ($room->land_area_val != null && $room->land_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
            } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
            } else {
                $room->tsubo = "-";
            }
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        $data['items'][0] = $item;

        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set2', $data)
            ->setPaper('A4', 'landscape')
            ->setOption('user-style-sheet', base_path() . '/public/css/app.css');
        // $pdf->setOptions(['orientation' => 'Landscape']);

        return $pdf->stream('title.pdf');

        // return view('business.pages.for-sale-pdf', $data);
    }


    public function forSaleMultiPrintSet3(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 1) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 2) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }


            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';


            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
            $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

            // プロパティ名を取得
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            $item->shop_name = $shop;

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 対象の一覧取得
            $item->room_list = Article::getDataByRoom($item->building_id, 1);
            $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
            foreach ($item->sold_room_list as &$room) {
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = "-";
                }
            }
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');
        $user = Auth::user();
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);
        $data['shop'] = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        $data['manner_name'] = $manner_name;
        // $pdf = \PDF::loadView('business.pages.for-sale-pdf-set1', $data);
        // return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        return view('business.pages.for-sale-pdf-set3', $data);
    }

    public function forSaleSinglePrintSet3(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);
        // 管理形態
        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        // ガス・オール電化
        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
        if ($item->land_condition == 1) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 2) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }


        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 対象の一覧取得
        $item->room_list = Article::getDataByRoom($item->building_id, 1);
        $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
        foreach ($item->sold_room_list as &$room) {
            if ($room->land_area_val != null && $room->land_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
            } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
            } else {
                $room->tsubo = "-";
            }
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $item->shop = $shop;
        $data['shop'] = $shop;
        $data['manner_name'] = $manner_name;

        $data['items'][0] = $item;

        $data['url'] = url('');

        return view('business.pages.for-sale-pdf-set3', $data);
    }

    public function forSaleMultiPdfSet3(Request $request)
    {
        $user = Auth::user();
        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);
        foreach ($article_list as &$item) {
            $item->article_name = $item->name;
            // 管理形態
            $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 水道
            $item->sewerage_name = "";
            if ($item->sewerage == 1) {
                $item->sewerage_name = "本下水";
            } elseif ($item->sewerage == 2) {
                $item->sewerage_name = "集中浄化槽";
            } elseif ($item->sewerage == 3) {
                $item->sewerage_name = "個別浄化槽";
            } else {
                $item->sewerage_name = "-";
            }

            // ガス・オール電化
            $item->gas = "";
            if ($item->gas == 1) {
                $item->gas_name = "都市ガス";
            } elseif ($item->gas == 2) {
                $item->gas_name = "集中ＬＰＧ";
            } elseif ($item->gas == 3) {
                $item->gas_name = "個別ＬＰＧ";
            } elseif ($item->gas == 4) {
                $item->gas_name = "オール電化";
            } else {
                $item->gas_name = "-";
            }

            // 車庫
            $garage = [
                ['code' => 1, 'name' => '掘込車庫'],
                ['code' => 0, 'name' => '未選択'],
            ];

            // 地目
            if ($item->ground == 1) {
                $item->ground_name = "宅地";
            } elseif ($item->ground == 2) {
                $item->ground_name = "田";
            } elseif ($item->ground == 3) {
                $item->ground_name = "畑";
            } elseif ($item->ground == 4) {
                $item->ground_name = "山林";
            } elseif ($item->ground == 5) {
                $item->ground_name = "雑種地";
            } elseif ($item->ground == 6) {
                $item->ground_name = "原野";
            } elseif ($item->ground == 7) {
                $item->ground_name = "その他";
            } else {
                $item->ground_name = "-";
            }

            // 建築条件
            if ($item->land_condition == 1) {
                $item->land_condition_name = "条件なし";
            } elseif ($item->land_condition == 2) {
                $item->land_condition_name = "建築条件付き";
            } else {
                $item->land_condition_name = "-";
            }


            // 開発許可番号
            $lot = Article::getDataById($item->sale_no);
            $item->develop_num = $lot->develop_num ?? null;

            // 用途地域
            $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
            $item->use_area_name = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
            $item->use_area_name_sub = $use_info->fomalname ?? '';
            $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
            $item->use_area_name_sub2 = $use_info->fomalname ?? '';


            // 画像の設定
            if ($item->sheet_file_type == 1) {
                $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
            } else {
                $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
            }

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 写真画像の取得
            $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
            foreach ($photos as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
            }

            // プロパティ名を取得
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

            // 周辺物件を取得
            $tmp_facilities = RelationArticleFacility::getData($request->id);
            $facility_count = ceil(count($tmp_facilities) / 2);
            $facilities = [];
            foreach ($tmp_facilities as $tmp_facility) {
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $item->file_path);
                $facilities[] = $facility;
            }

            // 取引様態の取得
            $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
            $item->manner_name = $manner_name;

            // 取扱店舗を取得
//            $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
            $item->shop_name = $shop;

            // 業者シート画像
            $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

            // 対象の一覧取得
            $item->room_list = Article::getDataByRoom($item->building_id, 1);
            $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
            foreach ($item->sold_room_list as &$room) {
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = "-";
                }
            }
        }

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

// dump($article_list);
        $data['items'] = $article_list;
        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set3', $data);
        return $pdf->inline('thisis.pdf');  //ブラウザ上で開ける

        // return view('business.pages.for-sale-pdf', $data);
    }

    public function forSaleSinglePdfSet3(Request $request)
    {
        $user = Auth::user();
        $data = [];

        // 対象１件を取得
        $item = Article::getDataById($request->target_id);
        // 管理形態
        $item->management_name = MstManagementForm::find($item->management_form)->name ?? '';

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 水道
        $item->sewerage_name = "";
        if ($item->sewerage == 1) {
            $item->sewerage_name = "本下水";
        } elseif ($item->sewerage == 2) {
            $item->sewerage_name = "集中浄化槽";
        } elseif ($item->sewerage == 3) {
            $item->sewerage_name = "個別浄化槽";
        } else {
            $item->sewerage_name = "-";
        }

        // ガス・オール電化
        $item->gas = "";
        if ($item->gas == 1) {
            $item->gas_name = "都市ガス";
        } elseif ($item->gas == 2) {
            $item->gas_name = "集中ＬＰＧ";
        } elseif ($item->gas == 3) {
            $item->gas_name = "個別ＬＰＧ";
        } elseif ($item->gas == 4) {
            $item->gas_name = "オール電化";
        } else {
            $item->gas_name = "-";
        }

        // 車庫
        $garage = [
            ['code' => 1, 'name' => '掘込車庫'],
            ['code' => 0, 'name' => '未選択'],
        ];

        // 地目
        if ($item->ground == 1) {
            $item->ground_name = "宅地";
        } elseif ($item->ground == 2) {
            $item->ground_name = "田";
        } elseif ($item->ground == 3) {
            $item->ground_name = "畑";
        } elseif ($item->ground == 4) {
            $item->ground_name = "山林";
        } elseif ($item->ground == 5) {
            $item->ground_name = "雑種地";
        } elseif ($item->ground == 6) {
            $item->ground_name = "原野";
        } elseif ($item->ground == 7) {
            $item->ground_name = "その他";
        } else {
            $item->ground_name = "-";
        }

        // 建築条件
        if ($item->land_condition == 1) {
            $item->land_condition_name = "条件なし";
        } elseif ($item->land_condition == 2) {
            $item->land_condition_name = "建築条件付き";
        } else {
            $item->land_condition_name = "-";
        }


        // 開発許可番号
        $lot = Article::getDataById($item->sale_no);
        $item->develop_num = $lot->develop_num ?? null;

        // 用途地域
        $use_info = MstUseArea::where('code', '=', $item->use_area)->first();
        $item->use_area_name = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use)->first();
        $item->use_area_name_sub = $use_info->fomalname ?? '';
        $use_info = MstUseArea::where('code', '=', $item->land_use2)->first();
        $item->use_area_name_sub2 = $use_info->fomalname ?? '';


        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }

        // 業者シート画像
        $item->disp_sheet_file = str_replace('public', '/storage', $item->sheet_file_path2);

        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 対象の一覧取得
        $item->room_list = Article::getDataByRoom($item->building_id, 1);
        $item->sold_room_list = Article::getDataByRoom($item->building_id, 2);
        foreach ($item->sold_room_list as &$room) {
            if ($room->land_area_val != null && $room->land_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
            } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                // 坪単価
                $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
            } else {
                $room->tsubo = "-";
            }
        }

        // 取引様態の取得
        $manner_name = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取引様態
        $manner = MstManner::getData();
        array_unshift($manner, ['code' => '', 'name' => '']);

        $data['manner'] = $manner;

        $data['ad_conf'] = [
            ['code' => '', 'name' => ''],
            ['code' => 1, 'name' => 'TEL'],
            ['code' => 2, 'name' => 'FAX'],
        ];

        // ステータス
        $data['statuses'] = MstStatus::getData();

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop
        $data['shop'] = $shop;
        $data['manner_name'] = $manner_name;

        $data['items'][0] = $item;

        $data['url'] = url('');

        $pdf = \PDF::loadView('business.pages.for-sale-pdf-set3', $data)
            ->setPaper('A4', 'landscape')
            ->setOption('user-style-sheet', base_path() . '/public/css/app.css');
        // $pdf->setOptions(['orientation' => 'Landscape']);

        return $pdf->stream('title.pdf');

        // return view('business.pages.for-sale-pdf', $data);
    }


    public function closeDetail(Request $request)
    {
        $request['is_close'] = true;
        return $this->forSaleDetail($request);
    }

    public function closeDetails(Request $request)
    {
        $request['is_close'] = true;
        return $this->forSaleDetails($request);
    }

    public function mansionParentDetail(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $data['total_article'] = $request->get('total_article');
        $page = 0;
        if (isset($request->page)) {
            $page = $request->page - 1;
        }
        $params = $request->all();
        $data['totalArt'] = $params['totalArt'] ?? 1;
        unset($params['_token']);
        $de_id = $request->get('only_items');

        if ($de_id !== '' && isset($de_id)) {
            $de_id = explode(',', $de_id);
        } else {

            $de_id = explode(',', $request->detail_id);
        }
        $result = [];

        if ($request->print == 'all') {
            $de_id = Article::getMansionDataByCondition($request, null, null, null)->pluck('building_id');
        }

        foreach ($de_id as $key => $val) {
            // 対象１件を取得
            $item = Article::getDataById($val);
            // プロパティ名を取得
            $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

            // 間取り
            $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

            // 構造
            $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
            $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

            // 土地権利
            $item->land_right_name = '';
            if ($item->land_right == 1) {
                $item->land_right_name = '所有権';

            } elseif ($item->land_right == 2) {
                $item->land_right_name = '借地権のみ';

            } elseif ($item->land_right == 3) {
                $item->land_right_name = '所有権・借地権混在';

            }

            // 用途
            $item->use_area_name = MstUseArea::where('code', $item->use_area)->first()->fomalname ?? '';
            $item->use_area_name_sub = MstUseArea::where('code', $item->land_use)->first()->fomalname ?? '';
            $item->use_area_name_sub2 = MstUseArea::where('code', $item->land_use2)->first()->fomalname ?? '';

            $item->shared = RelationShared::where('article_id', $item->building_id)->get();

            // 共用部

            // 管理形態
            $item->management_form_name = MstManagementForm::find($item->management_form)->name ?? '';

            // 学校区
            if (isset($item->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($item->primary_school_school);
                if (isset($primary->name)) {
                    $item->primary_school_name = str_replace('学校', '', $primary->name);
                }
            }
            if (isset($item->primary_school2_school)) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = str_replace('学校', '', $school->name);
            }
            if (isset($item->primary_school3_school)) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = str_replace('学校', '', $school->name);
            }
            if (isset($item->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($item->secondary_school_school);
                if (isset($secondary->name)) {
                    $item->secondary_school_name = str_replace('学校', '', $secondary->name);
                }
            }
            if (isset($item->secondary_school2_school)) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = str_replace('学校', '', $school->name);
            }
            if (isset($item->secondary_school3_school)) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = str_replace('学校', '', $school->name);
            }

            // 対象の一覧取得
            $room_list = Article::getDataByRoom($item->building_id, 1);
// dump($room_list);
            $facilities = [];
            $facilities_few = [];
            $photos = [];
            $photosOutdoor = [];
            $photosElm = RelationArticlePhoto::getDispImgByArticleId($item->building_id);

            foreach ($photosElm as $photo) {
                $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                $photos[] = $photo;
            }

            $tmp_facilities = [];

            foreach ($room_list as &$room) {
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = 0;
                }

                // 画像の設定
                if ($room->sheet_file_type == 1) {
                    $room->disp_file = str_replace('public', '/storage', $room->sheet_file_path3);
                } else {
                    $room->disp_file = str_replace('public', '/storage', $room->floor_file_path);
                }
                // 写真画像の取得
                /*$photosElm = RelationArticlePhoto::getDispImgByArticleId($room->building_id);

                foreach($photosElm as $photo) {
    //                $photo->disp_file_path = 'https://assets.bwbx.io/images/users/iqjWHBFdfxIU/iY9hlVg7iub8/v0/1000x-1.jpg';
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                    if($photo->property_sub == 1) {
                        $photosOutdoor[] = $photo;
                    } else {
                        $photos[] = $photo;
                    }
                }*/

            }

            // 周辺物件を取得
            $list_facilities = RelationArticleFacility::getData($item->building_id);
            foreach ($list_facilities as $tmp_facility) {
                $tmp_facilities[] = $tmp_facility;
                $facility = Facility::getDataById($tmp_facility->facility_id);
                $facility->disp_file = str_replace('public', '/storage', $facility->file_path);
                $facility->kind_name = MstFacilityType::find($facility->kind)->name ?? '';
                $facility->access = '徒歩' . $tmp_facility->time . '分(約' . $tmp_facility->distance . 'm)';
                $facility->distance = $tmp_facility->distance;
                $facility->time = $tmp_facility->time;
                $facilities[] = $facility;
                if (count($facilities_few) < 6) {
                    $facilities_few[] = $facility;
                }
            }

            $sold_room_list = Article::getDataByRoom($item->building_id, 2);

//        dd($sold_room_list);
            foreach ($sold_room_list as $room) {
                if ($room->land_area_val != null && $room->land_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->land_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->land_area_val * 0.3025));
                } elseif ($room->total_area_val != null && $room->total_area_val != 0) {
                    // 坪単価
                    $room->tsubo = ceil($room->price / ($room->total_area_val * 0.3025));
                    $room->tsubo_closing = ceil($room->price_closing / ($room->total_area_val * 0.3025));
                } else {
                    $room->tsubo = 0;
                }
            }
            $item->facilirty_few = $facilities_few;
            $facility_count = ceil(count($facilities) / 2);


            // 取引様態の取得
            $manner = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

            // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
            $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop

            // // 表示中のページの最初のアイテムのインデックス番号
            // $first = $article_list->firstItem();

            // // 表示中のページの最後のアイテムのインデックス番号
            // $last = $article_list->lastItem();

            $count = count($room_list);

            $data['article_list'] = $room_list;
            $data['sold_article_list'] = $sold_room_list;
            $data['item'] = $item;
            $data['citem'] = $item;
            $data['photos'] = $photos;
            //$data['photosOutdoor'] = $photosOutdoor;
            $data['facilities'] = $facilities;
            $data['tmp_facilities'] = $tmp_facilities;
            $data['facility_count'] = $facility_count;
            $data['manner'] = $manner;
            $data['shop'] = $shop;
            $data['params'] = $params;
            $data['params_single'] = ['target_id' => $item->building_id];

            $data['count'] = $count;
            // $data['first'] = $first;
            // $data['last'] = $last;
            // show adrr
            $store = auth()->user()->store;
            $data['pref'] = MstPrefecture::getData();
            $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
            $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);


            $result[] = $data;
        }
        if (!$request->action) {
            return view('business.pages.mansion-parent-detail', $result[0]);
        }
        $printContentBlade = 'business.pages.print.mansion.main-sheet';
        if ($request->tab == 'summary') {
            $printContentBlade = 'business.pages.print.mansion.summary';
        } else if ($request->tab == 'pictures') {
            $printContentBlade = 'business.pages.print.mansion.pictures';
        } else if ($request->tab == 'photo_tab') {
            $printContentBlade = 'business.pages.print.mansion.pictures';
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView($printContentBlade, $result)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('mansion_list.pdf');
        }

        return view($printContentBlade)->with(['result' => $result, 'totalArt' => $data['totalArt']]);

    }

    public function mansionDetail(Request $request)
    {
        $user = Auth::user();
        $data = [];

        $page = 0;
        if (isset($request->page)) {
            $page = $request->page - 1;
        }
        $params = $request->all();
        unset($params['_token']);

        // 対象１件を取得
        $mansion = Article::getDataById($request->id);

        // 対象の一覧取得
        $room_list = Article::getDataByRoom($mansion->building_id);

        $item = $room_list[$page];

        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $mansion->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $mansion->construction_sub)->first()->name ?? '';

        // 管理形態
        $item->construction_name = MstStructure::where("code", $mansion->management_form)->first()->name ?? '';

        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }
        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($item->building_id);
        foreach ($photos as $index => $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($mansion->property, $mansion->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop

        // // 表示中のページの最初のアイテムのインデックス番号
        // $first = $article_list->firstItem();

        // // 表示中のページの最後のアイテムのインデックス番号
        // $last = $article_list->lastItem();

        $count = count($room_list);

        $data['article_list'] = $room_list;
        $data['mansion'] = $mansion;
        $data['item'] = $item;
        $data['photos'] = $photos;
        $data['facilities'] = $facilities;
        $data['facility_count'] = $facility_count;
        $data['manner'] = $manner;
        $data['shop'] = $shop;
        $data['params'] = $params;
        $data['params_single'] = ['target_id' => $request->id];

        $data['count'] = $count;
        // $data['first'] = $first;
        // $data['last'] = $last;

        return view('business.pages.mansion-detail', $data);
    }

    public function lotParentDetail(Request $request)
    {
        $user = Auth::user();
        $data = [];
        $data['total_article'] = $request->get('total_article');
        $params = $request->all();
        unset($params['_token']);
        if($request->print == 'all') {
            $data['totalArt'] = $params['totalArt'] ?? 1;
        }else{
            $data['totalArt'] = 1;
        }

        // 対象１件を取得
        $listOfArticle = [];
        if ($request->action && $request->print_type != 'single') {
            if ($request->print == 'all') {
                $artListId = Article::getSearchDataByCondition($request, null, null, null, '', 1)->pluck('building_id');
                foreach ($artListId as $id) {
                    $listOfArticle[] = Article::getDataById($id);
                }
            } else {
                $items = explode(',', $request->only_items);
                foreach ($items as $id) {
                    $listOfArticle[] = Article::getDataById($id);
                }
                if (empty($listOfArticle[0])) {
                    $listOfArticle = [];
                    $listOfArticle[] = Article::getDataById($request->id);
                }
            }

        } else {
            $listOfArticle[] = Article::getDataById($request->id);
        }

        foreach ($listOfArticle as &$item) {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);

            // 土地権利
            $item->land_right_name = '';
            if ($item->land_right == 1) {
                $item->land_right_name = '所有権';
            } elseif ($item->land_right == 2) {
                $item->land_right_name = '借地権のみ';
            } elseif ($item->land_right == 3) {
                $item->land_right_name = '所有権・借地権混在';
            }

            // 地目
            $item->ground_name = '';
            if ($item->ground == 1) {
                $item->ground_name = '宅地';

            } elseif ($item->ground == 2) {
                $item->ground_name = '田';

            } elseif ($item->ground == 3) {
                $item->ground_name = '畑';

            } elseif ($item->ground == 4) {
                $item->ground_name = '山林';

            } elseif ($item->ground == 5) {
                $item->ground_name = '雑種地';

            } elseif ($item->ground == 6) {
                $item->ground_name = '原野';

            } elseif ($item->ground == 7) {
                $item->ground_name = 'その他';

            }

            // 地役権
            $item->easement_name = '';
            if ($item->easement == 1) {
                $item->easement_name = '無';

            } elseif ($item->easement == 2) {
                $item->easement_name = '地役権';

            } elseif ($item->easement == 3) {
                $item->easement_name = '通行地役権';

            } elseif ($item->easement == 4) {
                $item->easement_name = '引水地役権';

            } elseif ($item->easement == 5) {
                $item->easement_name = '眺望地役権';

            } elseif ($item->easement == 6) {
                $item->easement_name = '通路賃借権';

            }

            // 都市計画
            if ($item->city_plan != 0) {
                $cp = MstCityPlan::where('code', '=', $item->city_plan)->first();
                if ($cp != null) {
                    $item->city_plan_name = $cp->name ?? '';
                }

            } else {
                $item->city_plan_name = '-';
            }

            // 用途
//         $item->use_area_name = MstUseArea::find($item->use_area)->name??'';
//         $item->use_area_name_sub = MstUseArea::find($item->land_use)->name??'';
//         $item->use_area_name_sub2 = MstUseArea::find($item->land_use2)->name??'';

            $item->use_area_name = MstUseArea::where('code', $item->use_area)->first()->fomalname ?? '';
            $item->use_area_name_sub = MstUseArea::where('code', $item->land_use)->first()->fomalname ?? '';
            $item->use_area_name_sub2 = MstUseArea::where('code', $item->land_use2)->first()->fomalname ?? '';

            // 共用部

            // 管理形態
            $item->management_form_name = MstManagementForm::find($item->management_form)->name ?? '';

            $item->city_plan_reason_name = optional(MstCityPlanReason::where("code", $item->city_plan_reason)->first())->name;

            // 学校区
            if (isset($item->primary_school_school)) {
                $primary = MstSchool::getSchoolByCode($item->primary_school_school);
                if (isset($primary->name)) {
                    $item->primary_school_name = $primary->name;
                }
            }
            if (isset($item->primary_school2_school)) {
                $school = MstSchool::where('code', $item->primary_school2_school)->first();
                $item->primary_school_name2 = $school->name;
            }
            if (isset($item->primary_school3_school)) {
                $school = MstSchool::where('code', $item->primary_school3_school)->first();
                $item->primary_school_name3 = $school->name;
            }
            if (isset($item->secondary_school_school)) {
                $secondary = MstSchool::getSchoolByCode($item->secondary_school_school);
                if (isset($secondary->name)) {
                    $item->secondary_school_name = $secondary->name;
                }
            }
            if (isset($item->secondary_school2_school)) {
                $school = MstSchool::where('code', $item->secondary_school2_school)->first();
                $item->secondary_school_name2 = $school->name;
            }
            if (isset($item->secondary_school3_school)) {
                $school = MstSchool::where('code', $item->secondary_school3_school)->first();
                $item->secondary_school_name3 = $school->name;
            }

            // 対象の一覧取得
            $lot_list = Article::getLotList($item->building_id);
            $article_list = Article::getArticleList($item->building_id, 1);
            foreach ($article_list as &$row) {
                if ($row->land_area_val != null && $row->land_area_val != 0) {
                    // 坪単価
                    $row->tsubo = ceil($row->price / ($row->land_area_val * 0.3025));
                    $row->tsubo_closing = ceil($row->price_closing / ($row->land_area_val * 0.3025));
                } else {
                    $row->tsubo = "-";
                }
            }
            $sold_lot_list = Article::getArticleList($item->building_id, 2);

            foreach ($sold_lot_list as $row) {
                if ($row->land_area_val != null && $row->land_area_val != 0) {
                    // 坪単価
                    $row->tsubo = ceil($row->price / ($row->land_area_val * 0.3025));
                } else {
                    $row->tsubo = "-";
                }
            }

            $facilities = [];
            $facilities_few = [];
            $photos = [];
            foreach ($lot_list as $lot) {
                // 画像の設定
                if ($lot->sheet_file_type == 1) {
                    $lot->disp_file = str_replace('public', '/storage', $lot->sheet_file_path3);
                } else {
                    $lot->disp_file = str_replace('public', '/storage', $lot->floor_file_path);
                }
                // 写真画像の取得
                $photos = RelationArticlePhoto::getDispImgByArticleId($lot->building_id);
                foreach ($photos as $photo) {
                    $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
                }

                // 周辺物件を取得
                $tmp_facilities = RelationArticleFacility::getData($lot->building_id);


                foreach ($tmp_facilities as $tmp_facility) {
                    $facility = Facility::getDataById($tmp_facility->facility_id);
                    $facility->disp_file = str_replace('public', '/storage', $facility->file_path);
                    $facility->kind_name = MstFacilityType::find($facility->kind)->name ?? '';
                    $facility->access = '徒歩' . $tmp_facility->time . '分(約' . $tmp_facility->distance . 'm)';
                    $facility->distance = $tmp_facility->distance;
                    $facility->time = $tmp_facility->time;
                    $facilities[] = $facility;
                    if (count($facilities_few) < 6) {
                        $facilities_few[] = $facility;
                    }
                }
            }
            $item->facilirty_few = $facilities_few;
            $facility_count = ceil(count($facilities) / 2);

            $item['article_list'] = $article_list;
            $item['sold_lot_list'] = $sold_lot_list;
            $item['photos'] = $photos;
            $item['facilities'] = $facilities;
            $item['facility_count'] = $facility_count;
        }


        // 取引様態の取得
        $manner = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';
        // 取扱店舗を取得
        $company = MstCompany::getDataById($item->company_id);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop

        $data['manner'] = $manner;
        $data['shop'] = $shop;
        $data['company'] = $company;
        $data['params'] = $params;
        $data['item'] = $listOfArticle[0];
        $data['listOfArticle'] = $listOfArticle;
        $data['params_single'] = ['target_id' => $request->id] + $params;
        // show addr
        $store = auth()->user()->store;
        $data['pref'] = MstPrefecture::getData();
        $data['city'] = MstCity::getData($store['address1'], $store['address2'], $user->company_id);
        $data['town'] = MstTown::getData($store['address1'], $store['address2'], $store['address3']);

        if ($request->action == null) {
            return view('business.pages.lot-parent-detail', $data);
        }

        $printContentBlade = 'business.pages.print.lot.parent-detail-main-sheet';
        if ($request->tab == 'summary') {
            $printContentBlade = 'business.pages.print.lot.summary';
        }

        if ($request->action == 'pdf') {
            $pdf = \PDF::loadView($printContentBlade, $data)
//                ->setPaper('A4', 'landscape')
                ->setOption('user-style-sheet', base_path() . '/public/css/app.css');

            return $pdf->stream('forsale_list.pdf');
        }

        return view($printContentBlade, $data);
    }

    public function lotDetail(Request $request)
    {
        $user = Auth::user();
        $data = [];

        $params = $request->all();
        unset($params['_token']);

        // 対象の一覧取得
        $article_list = Article::getSearchDataByCondition($request);

        // 対象１件を取得
        $item = Article::getDataById($request->id);
        // 間取り
        $item->floor_plan_name = MstFloorType::find($item->floor_plan_type)->name ?? '';

        // 構造
        $item->construction_name = MstStructure::where("code", $item->construction)->first()->name ?? '';
        $item->construction_sub_name = MstStructureSub::where("code", $item->construction_sub)->first()->name ?? '';

        // 管理形態
        $item->construction_name = MstStructure::where("code", $item->management_form)->first()->name ?? '';

        // 画像の設定
        if ($item->sheet_file_type == 1) {
            $item->disp_file = str_replace('public', '/storage', $item->sheet_file_path3);
        } else {
            $item->disp_file = str_replace('public', '/storage', $item->floor_file_path);
        }
        // 写真画像の取得
        $photos = RelationArticlePhoto::getDispImgByArticleId($request->id);
        foreach ($photos as $photo) {
            $photo->disp_file_path = str_replace('public', '/storage', $photo->file_path);
        }

        // プロパティ名を取得
        $item->property_name = $this->getPropertyName($item->property, $item->property_sub);

        // 周辺物件を取得
        $tmp_facilities = RelationArticleFacility::getData($request->id);
        $facility_count = ceil(count($tmp_facilities) / 2);
        $facilities = [];
        foreach ($tmp_facilities as $tmp_facility) {
            $facility = Facility::getDataById($tmp_facility->facility_id);
            $facility->disp_file = str_replace('public', '/storage', $item->file_path);
            $facilities[] = $facility;
        }

        // 取引様態の取得
        $manner = MstManner::where('code', '=', $item->company_manner)->first()->name ?? '';

        // 取扱店舗を取得
//        $shop = MstStore::getDataByCode($user->company_id, $item->shop);
        $shop = MstStore::getDataByCode($user->company_id, auth()->user()->store_id);//$item->shop

        // // 表示中のページの最初のアイテムのインデックス番号
        // $first = $article_list->firstItem();

        // // 表示中のページの最後のアイテムのインデックス番号
        // $last = $article_list->lastItem();

        $count = count($article_list);

        $data['article_list'] = $article_list;
        $data['item'] = $item;
        $data['photos'] = $photos;
        $data['facilities'] = $facilities;
        $data['facility_count'] = $facility_count;
        $data['manner'] = $manner;
        $data['shop'] = $shop;
        $data['params'] = $params;
        $data['params_single'] = ['target_id' => $request->id];

        $data['count'] = $count;
        // $data['first'] = $first;
        // $data['last'] = $last;

        return view('business.pages.lot-detail', $data);
    }

    //TODO delete
//    public function sample(Request $request)
//    {
//        return view('business.objects.projects.modal-print');
//    }
//
//    public function sample2(Request $request)
//    {
//        return view('business.objects.projects.modal-select-layout');
//    }

    public static function getPropertyName($property, $property_sub)
    {
        $res = '';
        if ($property > 3) {
            $res .= MstPropertyTypeSub::find($property_sub)->name ?? '';
        }

        if ($property != null) {
            $res .= MstPropertyType::find($property)->name ?? '';
        } else {
//            $res .= 'マンション（部屋）';
            $res .= 'マンション';
        }


        return $res;
    }

    public static function createConditionList($con)
    {
        // dump($con);
        $user = Auth::user();
        $res = [];
        $property_name = "";
        $add = "";
        $pref = 0;
        $city = 0;
        $town = 0;
        $price = "";
        $years = "";
        $months = "";
        $construction = "";
        $floor_area = "";
        $land = "";
        $floor = "";
        $school = "";
        $parking = "";
        $land_status = "";
        $land_kind = "";
        $shop = "";
        $balcony_direction = "";
        $balcony_area = "";
        $pet = "";
        $pet_num = "";
        $use_area = "";
        $management_company = "";
        $construction_name = "";
        foreach ($con as $key => $val) {
            if ($key == 'property') {
                $property_name = "";
                foreach ($val as $row) {
                    $tmp = "";
                    $tmp_sub = "";
                    if ($row == 41) {
                        $property = 4;
                        $property_sub = 1;
                        $tmp_sub = MstPropertyTypeSub::find($property_sub)->name ?? '';
                    } elseif ($row == 42) {
                        $property = 4;
                        $property_sub = 2;
                        $tmp_sub = MstPropertyTypeSub::find($property_sub)->name ?? '';
                    } else {
                        $property = $row;
                    }
                    $tmp = MstPropertyType::find($property)->name ?? '';
                    $property_name .= $tmp_sub . $tmp;
                }

            }
            if ($key == 'address1') {
                foreach ($val as $row) {
                    $add .= MstPrefecture::where('code', '=', $row)->first()->name ?? '';
                    $pref .= $row;
                }

            }
            if ($key == 'address2') {
                foreach ($val as $row) {
                    $add .= MstCity::where('pref_code', '=', $pref)->where('code', '=', $val)->first()->name ?? '';
                    $city .= $row;
                }
            }
            if ($key == 'address3') {
                foreach ($val as $row) {
                    $add .= MstTown::where('pref_code', '=', $pref)->where('city_code', '=', $city)->where('code', '=', $val)->first()->name ?? '';
                    $add .= $row;
                }
            }
            if ($key == 'address4') {
                foreach ($val as $row) {
                    $add .= $row;
                }
            }
            if ($key == 'building_id') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $res['物件ID'] = $row;
                    }
                }
            }
            if ($key == 'price_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $price .= $row . "万円";
                    }
                }
            }
            if ($key == 'price_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($price != "") {
                            $price .= "〜";
                        }
                        $price .= $row . "万円";
                    } else {
                        if ($price != "") {
                            $price .= "〜";
                        }
                    }
                }
            }
            if ($key == 'access1') {

            }
            if ($key == 'access2') {

            }
            if ($key == 'access3') {

            }
            if ($key == 'bus') {

            }
            if ($key == 'walk') {

            }
            if ($key == 'primary_school') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $school .= MstSchool::where('code', '=', $row)->first()->name ?? '';
                    }
                }
            }
            if ($key == 'secondary_school') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($school != "") {
                            $school .= " ";
                        }
                        $school .= MstSchool::where('code', '=', $row)->first()->name ?? '';
                    }
                }
            }

            if ($key == 'land_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $land .= $row . "㎡";
                    }
                }
            }
            if ($key == 'land_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($land != "") {
                            $land .= "〜";
                        }
                        $land .= $row . "㎡";
                    } else {
                        if ($land != "") {
                            $land .= "〜";
                        }
                    }
                }
            }
            if ($key == 'floor_area_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $floor_area .= $row . "㎡";
                    }
                }
            }
            if ($key == 'floor_area_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($floor_area != "") {
                            $floor_area .= "〜";
                        }
                        $floor_area .= $row . "㎡";
                    } else {
                        if ($floor_area != "") {
                            $floor_area .= "〜";
                        }
                    }
                }
            }
            if ($key == 'floor_plan') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $res['間取り'] = '1R - 1SLDK';
                                break;
                            case 2:
                                $res['間取り'] = '2K - 2SLDK';
                                break;
                            case 3:
                                $res['間取り'] = '3K - 3SLDK';
                                break;
                            case 4:
                                $res['間取り'] = '4K - 4SLDK';
                                break;
                            case 5:
                                $res['間取り'] = '5K〜';
                                break;

                        }
                    }
                }


            }
            if ($key == 'name') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $res['物件名'] = $row;
                    }
                }
            }
            if ($key == 'mansion_name') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $res['マンション名'] = $row;
                    }
                }
            }
            if ($key == 'years_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $years .= $row . "年";
                    }
                }
            }
            if ($key == 'years_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($years != "") {
                            $years .= "〜";
                        }
                        $years .= $row . "年";
                    } else {
                        if ($years != "") {
                            $years .= "〜";
                        }
                    }
                }
            }

            if ($key == 'year_month_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $months .= $row;
                    }
                }
            }
            if ($key == 'year_month_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($months != "") {
                            $months .= "〜";
                        }
                        $months .= $row;
                    } else {
                        if ($months != "") {
                            $months .= "〜";
                        }
                    }
                }
            }
            if ($key == 'construction') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $construction .= MstStructure::where('code', '=', $row)->first()->name ?? '';
                    }
                }
            }
            if ($key == 'parking') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $parking .= MstParking::where('code', '=', $row)->first()->name ?? '';
                    }
                }
            }
            if ($key == 'land_status') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $land_status .= MstGenkyou::where('code', '=', $row)->first()->name ?? '';
                    }
                }
            }
            if ($key == 'use_area') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $use_area .= MstUseArea::where('code', '=', $row)->first()->fomalname ?? '';
                    }
                }
            }
            if ($key == 'floor_from') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $floor .= $row . "㎡";
                    }
                }
            }
            if ($key == 'floor_to') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        if ($floor != "") {
                            $floor .= "〜";
                        }
                        $floor .= $row . "㎡";
                    } else {
                        if ($floor != "") {
                            $floor .= "〜";
                        }
                    }
                }
            }
            if ($key == 'land_kind') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $land_kind = "北";
                                break;
                            case 2:
                                $land_kind = "北東";
                                break;
                            case 3:
                                $land_kind = "東";
                                break;
                            case 4:
                                $land_kind = "南東";
                                break;
                            case 5:
                                $land_kind = "南";
                                break;
                            case 6:
                                $land_kind = "南西";
                                break;
                            case 7:
                                $land_kind = "西";
                                break;
                            case 8:
                                $land_kind = "北西";
                                break;
                        }
                    }
                }
            }

            if ($key == 'search_shop') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $shop .= MstStore::where('company_id', '=', $user->company_id)->where('code', $row)->first()->name ?? '';
                    }
                }
            }
            if ($key == 'balcony_direction') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $balcony_direction = "北";
                                break;
                            case 2:
                                $balcony_direction = "北東";
                                break;
                            case 3:
                                $balcony_direction = "東";
                                break;
                            case 4:
                                $balcony_direction = "南東";
                                break;
                            case 5:
                                $balcony_direction = "南";
                                break;
                            case 6:
                                $balcony_direction = "南西";
                                break;
                            case 7:
                                $balcony_direction = "西";
                                break;
                            case 8:
                                $balcony_direction = "北西";
                                break;
                        }
                    }
                }
            }

            if ($key == 'balcony_area') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $balcony_area .= $row . "㎡";
                    }
                }
            }
            if ($key == 'is_pet') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        switch ($row) {
                            case 2:
                                $pet = "飼育可能（制限有）";
                                break;
                            case 1:
                                $pet = "飼育不可";
                                break;
                        }
                    }
                }
            }
            if ($key == 'pet_num') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $pet_num .= $row . "匹";
                    }
                }
            }

            if ($key == 'management_company') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $management_company .= $row;
                    }
                }
            }

            if ($key == 'construction_name') {
                foreach ($val as $row) {
                    if (!empty($row)) {
                        $construction_name .= $row;
                    }
                }
            }
            if ($key == 'owner') {

            }
            if ($key == 'registed_from') {

            }
            if ($key == 'registed_to') {

            }
            if ($key == 'checked_from') {

            }
            if ($key == 'checked_to') {

            }
            if ($key == 'changed_price_from') {

            }
            if ($key == 'changed_price_to') {

            }
            if ($key == 'ad_checked_from') {

            }
            if ($key == 'ad_checked_to') {

            }
            if ($key == 'flyer') {

            }
            if ($key == 'vender_name') {

            }
            if ($key == 'vender_kind') {

            }
            if ($key == 'vender_tel') {

            }
        }

        if ($property_name != "") {
            $res['物件種別'] = $property_name;
        }

        if ($add != "") {
            $res['所在地'] = $add;
        }

        if ($price != "") {
            $res['価格'] = $price;
        }
        if ($years != "") {
            $res['築年数'] = $years;
        }
        if ($months != "") {
            $res['築年月'] = $months;
        }
        if ($construction != "") {
            $res['構造'] = $construction;
        }
        if ($parking != "") {
            $res['駐車場'] = $parking;
        }
        if ($land_status != "") {
            $res['現況'] = $land_status;
        }
        if ($use_area != "") {
            $res['用途地域'] = $use_area;
        }
        if ($land_kind != "") {
            $res['接道方向'] = $land_kind;
        }
        if ($shop != "") {
            $res['取扱店舗'] = $shop;
        }
        if ($balcony_direction != "") {
            $res['バルコニー'] = $balcony_direction . " " . $balcony_area;
        }

        if ($pet != "") {
            $res['ペット'] = $pet . " " . $pet_num;
        }
        if ($management_company != "") {
            $res['事業主'] = $management_company;
        }
        if ($construction_name != "") {
            $res['施工'] = $construction_name;
        }

        if ($school != "") {
            $res['学校区'] = $school;
        }

        if ($land != "") {
            $res['土地面積'] = $land;
        }

        if ($floor_area != "") {
            $res['延床面積'] = $floor_area;
        }

        if ($floor != "") {
            $res['階建'] = $floor;
        }

        return $res;
    }

    public static function createConditionSearch($con)
    {
        $user = Auth::user();
        $res = [];

        foreach ($con as $key => $val) {
            if ($key == 'property') {
                foreach ($val as $k => $row) {
                    $property_name = "";
                    $tmp_sub = "";
                    if ($row == 41) {
                        $property = 4;
                        $property_sub = 1;
                        $tmp_sub = MstPropertyTypeSub::find($property_sub)->name ?? '';
                    } elseif ($row == 42) {
                        $property = 4;
                        $property_sub = 2;
                        $tmp_sub = MstPropertyTypeSub::find($property_sub)->name ?? '';
                    } elseif ($row == 61) {
                        $property = 6;
                    } else {
                        $property = $row;
                    }
                    $tmp = MstPropertyType::find($property)->name ?? '';
                    $property_name .= $tmp_sub . $tmp;
                    if ($property_name != "") {
                        $res['物件種別'][$k] = $property_name;
                    }
                }


            }

            if ($key == 'address1') {
                foreach ($val as $k => $row) {
                    $add = "";
                    $pref = 0;
                    $city = 0;

                    $add .= MstPrefecture::where('code', '=', $row)->first()->name ?? '';
                    $pref .= $row;

                    if (isset($con['address2'][$k])) {
                        $add .= MstCity::where('pref_code', '=', $pref)->where('code', '=', $con['address2'][$k])->first()->name ?? '';
                        $city .= $con['address2'][$k];
                    }
                    if (isset($con['address3'][$k])) {
                        $add .= MstTown::where('pref_code', '=', $pref)->where('city_code', '=', $city)->where('code', '=', $con['address3'][$k])->first()->name ?? '';
                        $city .= $con['address3'][$k];
                    }

                    if (isset($con['address4'][$k])) {
                        $add .= $con['address4'][$k];
                    }

                    if ($add != "") {
                        $res['所在地'][$k] = $add;
                    }
                }

            }

            if ($key == 'building_id') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['物件ID'][$k] = $row;
                    }
                }
            }

            if ($key == 'price_from') {
                foreach ($val as $k => $row) {
                    $price = "";

                    if (!empty($row)) {
                        $price .= $row . "万円";
                    }

                    if (isset($con['price_to'][$k])) {
                        if (!empty($con['price_to'][$k])) {
                            if ($price != "") {
                                $price .= "〜";
                            }
                            $price .= $con['price_to'][$k] . "万円";
                        } else {
                            if ($price != "") {
                                $price .= "〜";
                            }
                        }
                    }

                    if ($price != "") {
                        $res['価格'][$k] = $price;
                    }
                }
            }

            if ($key == 'access1') {
                foreach ($val as $k => $row) {
                    $access = "";
                    $line_id = 0;
                    $access .= MstLine::where('id', '=', $row)->first()->name ?? '';
                    $line_id = $row;

                    if (isset($con['access2'][$k])) {
                        $access .= MstStation::where('line_id', '=', $line_id)->where('id', '=', $con['access2'][$k])->first()->name ?? '';
                    }

                    if (isset($con['access3'][$k])) {
                        if ($con['access3'][$k] == 1) {
                            $access .= '徒歩';
                        }
                        if ($con['access3'][$k] == 2) {
                            $access .= 'バス';
                        }
                    }

                    if ($access != "") {
                        $res['交通'][$k] = $access;
                    }
                }
            }

            if ($key == 'bus') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['バス'][$k] = $row;
                    }
                }
            }

            if ($key == 'walk') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['徒歩'][$k] = $row;
                    }
                }
            }

            if ($key == 'primary_school') {
                foreach ($val as $k => $row) {
                    $school = "";

                    if (!empty($row)) {
                        $school .= MstSchool::where('code', '=', $row)->first()->name ?? '';
                    }
                    if (isset($con['primary_school_time'][$k])) {
                        if (!empty($con['primary_school_time'][$k])) {
                            $school .= " 徒歩 ";
                            $school .= $con['primary_school_time'][$k] . ' 分以内';
                        }
                    }

                    if (isset($con['secondary_school'][$k])) {
                        if (!empty($con['secondary_school'][$k])) {
                            if ($school != "") {
                                $school .= " ";
                            }
                            $school .= MstSchool::where('code', '=', $con['secondary_school'][$k])->first()->name ?? '';
                        }
                    }

                    if (isset($con['secondary_school_time'][$k])) {
                        if (!empty($con['secondary_school_time'][$k])) {
                            $school .= " 徒歩 ";
                            $school .= $con['secondary_school_time'][$k] . ' 分以内';
                        }
                    }

                    if ($school != "") {
                        $res['学校区'][$k] = $school;
                    }
                }
            }

            if ($key == 'land_from') {
                foreach ($val as $k => $row) {
                    $land = "";

                    if (!empty($row)) {
                        $land .= $row . "㎡";
                    }

                    if (isset($con['land_to'][$k])) {
                        if (!empty($con['land_to'][$k])) {
                            if ($land != "") {
                                $land .= "〜";
                            }
                            $land .= $con['land_to'][$k] . "㎡";
                        } else {
                            if ($land != "") {
                                $land .= "〜";
                            }
                        }
                    }

                    if ($land != "") {
                        $res['土地面積'][$k] = $land;
                    }
                }
            }

            if ($key == 'floor_area_from') {
                foreach ($val as $k => $row) {
                    $floor_area = "";

                    if (!empty($row)) {
                        $floor_area .= $row . "㎡";
                    }

                    if (isset($con['floor_area_to'][$k])) {
                        if (!empty($con['floor_area_to'][$k])) {
                            if ($floor_area != "") {
                                $floor_area .= "〜";
                            }
                            $floor_area .= $con['floor_area_to'][$k] . "㎡";
                        } else {
                            if ($floor_area != "") {
                                $floor_area .= "〜";
                            }
                        }
                    }

                    if ($floor_area != "") {
                        $res['延床面積'][$k] = $floor_area;
                    }
                }
            }

            if ($key == 'floor_plan') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $res['間取り'][$k] = '1R - 1SLDK';
                                break;
                            case 2:
                                $res['間取り'][$k] = '2K - 2SLDK';
                                break;
                            case 3:
                                $res['間取り'][$k] = '3K - 3SLDK';
                                break;
                            case 4:
                                $res['間取り'][$k] = '4K - 4SLDK';
                                break;
                            case 5:
                                $res['間取り'][$k] = '5K〜';
                                break;

                        }
                    }
                }


            }

            if ($key == 'floor_plan_from') {
                foreach ($val as $k => $row) {
                    $floor_plan = "";

                    if (!empty($row)) {
                        $floor_plan .= $row . "R・SLDK";
                    }

                    if (isset($con['floor_plan_to'][$k])) {
                        if (!empty($con['floor_plan_to'][$k])) {
                            if ($floor_plan != "") {
                                $floor_plan .= "〜";
                            }
                            $floor_plan .= $con['floor_plan_to'][$k] . "R・SLDK";
                        } else {
                            if ($floor_plan != "") {
                                $floor_plan .= "〜";
                            }
                        }
                    }

                    if ($floor_plan != "") {
                        $res['間取り'][$k] = $floor_plan;
                    }
                }
            }

            if ($key == 'whereabouts_from') {
                foreach ($val as $k => $row) {
                    $whereabouts = "";

                    if (!empty($row)) {
                        $whereabouts .= $row . "階";
                    }

                    if (isset($con['whereabouts_to'][$k])) {
                        if (!empty($con['whereabouts_to'][$k])) {
                            if ($whereabouts != "") {
                                $whereabouts .= "〜";
                            }
                            $whereabouts .= $con['whereabouts_to'][$k] . "階";
                        } else {
                            if ($whereabouts != "") {
                                $whereabouts .= "〜";
                            }
                        }
                    }

                    if ($whereabouts != "") {
                        $res['マンション所在階'][$k] = $whereabouts;
                    }
                }
            }

            if ($key == 'name') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['物件名'][$k] = $row;
                    }
                }
            }

            if ($key == 'mansion_name') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['マンション名'][$k] = $row;
                    }
                }
            }

            if ($key == 'years_from') {
                foreach ($val as $k => $row) {
                    $years = "";

                    if (!empty($row)) {
                        $years .= $row . "年";
                    }

                    if (isset($con['years_to'][$k])) {
                        if (!empty($con['years_to'][$k])) {
                            if ($years != "") {
                                $years .= "〜";
                            }
                            $years .= $con['years_to'][$k] . "年";
                        } else {
                            if ($years != "") {
                                $years .= "〜";
                            }
                        }
                    }

                    if ($years != "") {
                        $res['築年数'][$k] = $years;
                    }
                }
            }

            if ($key == 'year_month_from') {
                foreach ($val as $k => $row) {
                    $months = "";

                    if (!empty($row)) {
                        $months .= $row;
                    }

                    if (isset($con['year_month_to'][$k])) {
                        if (!empty($con['year_month_to'][$k])) {
                            if ($months != "") {
                                $months .= "〜";
                            }
                            $months .= $con['year_month_to'][$k];
                        } else {
                            if ($months != "") {
                                $months .= "〜";
                            }
                        }
                    }

                    if ($months != "") {
                        $res['築年月'][$k] = $months;
                    }
                }
            }

            if ($key == 'construction') {
                foreach ($val as $k => $row) {
                    $construction = "";

                    if (!empty($row)) {
                        $construction .= MstStructure::where('code', '=', $row)->first()->name ?? '';
                    }

                    if ($construction != "") {
                        $res['構造'][$k] = $construction;
                    }
                }
            }

            if ($key == 'parking') {
                foreach ($val as $k => $row) {
                    $parking = "";

                    if (!empty($row)) {
                        $parking .= MstParking::where('code', '=', $row)->first()->name ?? '';
                    }

                    if ($parking != "") {
                        $res['駐車場'][$k] = $parking;
                    }
                }
            }

            if ($key == 'current_status') {
                foreach ($val as $k => $row) {
                    $current_status = "";

                    if (!empty($row)) {
                        $current_status .= MstGenkyou::where('code', '=', $row)->first()->name ?? '';
                    }

                    if ($current_status != "") {
                        $res['現況'][$k] = $current_status;
                    }
                }
            }

            if ($key == 'use_area') {
                foreach ($val as $k => $row) {
                    $use_area = "";

                    if (!empty($row)) {
                        $use_area .= MstUseArea::where('code', '=', $row)->first()->fomalname ?? '';
                    }

                    if ($use_area != "") {
                        $res['用途地域'][$k] = $use_area;
                    }
                }
            }

            if ($key == 'floor_from') {
                foreach ($val as $k => $row) {
                    $floor = "";

                    if (!empty($row)) {
                        $floor .= $row . "階建";
                    }

                    if (isset($con['floor_to'][$k])) {
                        if (!empty($con['floor_to'][$k])) {
                            if ($floor != "") {
                                $floor .= "〜";
                            }
                            $floor .= $con['floor_to'][$k] . "階建";
                        } else {
                            if ($floor != "") {
                                $floor .= "〜";
                            }
                        }
                    }

                    if ($floor != "") {
                        $res['階建'][$k] = $floor;
                    }

                }
            }

            if ($key == 'land_kind') {
                foreach ($val as $k => $row) {
                    $land_kind = "";

                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $land_kind = "北";
                                break;
                            case 2:
                                $land_kind = "北東";
                                break;
                            case 3:
                                $land_kind = "東";
                                break;
                            case 4:
                                $land_kind = "南東";
                                break;
                            case 5:
                                $land_kind = "南";
                                break;
                            case 6:
                                $land_kind = "南西";
                                break;
                            case 7:
                                $land_kind = "西";
                                break;
                            case 8:
                                $land_kind = "北西";
                                break;
                        }
                    }

                    if ($land_kind != "") {
                        $res['接道方向'][$k] = $land_kind;
                    }
                }
            }

            if ($key == 'search_shop') {
                foreach ($val as $k => $row) {
                    $shop = "";

                    if (!empty($row)) {
                        $shop .= MstStore::where('company_id', '=', $user->company_id)->where('code', $row)->first()->name ?? '';
                    }
                    if ($shop != "") {
                        $res['取扱店舗'][$k] = $shop;
                    }
                }
            }

            if ($key == 'balcony_direction') {
                foreach ($val as $k => $row) {
                    $balcony_direction = "";
                    $balcony_area = "";

                    if (!empty($row)) {
                        switch ($row) {
                            case 1:
                                $balcony_direction = "北";
                                break;
                            case 2:
                                $balcony_direction = "北東";
                                break;
                            case 3:
                                $balcony_direction = "東";
                                break;
                            case 4:
                                $balcony_direction = "南東";
                                break;
                            case 5:
                                $balcony_direction = "南";
                                break;
                            case 6:
                                $balcony_direction = "南西";
                                break;
                            case 7:
                                $balcony_direction = "西";
                                break;
                            case 8:
                                $balcony_direction = "北西";
                                break;
                        }
                    }

                    if (isset($con['balcony_area'][$k])) {
                        if (!empty($con['balcony_area'][$k])) {
                            $balcony_area .= $con['balcony_area'][$k] . "㎡";
                        }
                    }
                    if ($balcony_direction != "") {
                        $res['バルコニー'][$k] = $balcony_direction . " " . $balcony_area;
                    }
                }
            }

            if ($key == 'is_pet') {
                foreach ($val as $k => $row) {
                    $pet = "";
                    $pet_num = "";

                    if (!empty($row)) {
                        switch ($row) {
                            case 2:
                                $pet = "飼育可能（制限有）";
                                break;
                            case 1:
                                $pet = "飼育不可";
                                break;
                        }
                    }

                    if (isset($con['pet_num'][$k])) {
                        foreach ($val as $row) {
                            if (!empty($con['pet_num'][$k])) {
                                $pet_num .= $con['pet_num'][$k] . "匹";
                            }
                        }
                    }

                    if ($pet != "") {
                        $res['ペット'][$k] = $pet . " " . $pet_num;
                    }
                }
            }

            if ($key == 'management_company') {
                foreach ($val as $k => $row) {
                    $management_company = "";

                    if (!empty($row)) {
                        $management_company .= $row;
                    }
                }
            }

            if ($key == 'sales_company') {
                foreach ($val as $k => $row) {
                    $sales_company = "";

                    if (!empty($row)) {
                        $sales_company .= $row;
                    }

                    if ($sales_company != "") {
                        $res['分譲会社'][$k] = $sales_company;
                    }
                }
            }

            if ($key == 'construction_name') {
                foreach ($val as $k => $row) {
                    $construction_name = "";

                    if (!empty($row)) {
                        $construction_name .= $row;
                    }

                    if ($construction_name != "") {
                        $res['施工会社'][$k] = $construction_name;
                    }
                }
            }

            if ($key == 'owner') {

            }

            if ($key == 'registed_from') {
                foreach ($val as $k => $row) {
                    $registed = "";

                    if (!empty($row)) {
                        $registed .= $row;
                    }

                    if (isset($con['registed_to'][$k])) {
                        if (!empty($con['registed_to'][$k])) {
                            if ($registed != "") {
                                $registed .= "〜";
                            }
                            $registed .= $con['registed_to'][$k];
                        } else {
                            if ($registed != "") {
                                $registed .= "〜";
                            }
                        }
                    }

                    if ($registed != "") {
                        $res['物件登録日'][$k] = $registed;
                    }
                }
            }

            if ($key == 'checked_from') {
                foreach ($val as $k => $row) {
                    $checked = "";

                    if (!empty($row)) {
                        $checked .= $row;
                    }

                    if (isset($con['checked_to'][$k])) {
                        if (!empty($con['checked_to'][$k])) {
                            if ($checked != "") {
                                $checked .= "〜";
                            }
                            $checked .= $con['checked_to'][$k];
                        } else {
                            if ($checked != "") {
                                $checked .= "〜";
                            }
                        }
                    }

                    if ($checked != "") {
                        $res['物件確認日'][$k] = $checked;
                    }
                }
            }


            if ($key == 'changed_price_from') {
                foreach ($val as $k => $row) {
                    $changed_price = "";

                    if (!empty($row)) {
                        $changed_price .= $row;
                    }

                    if (isset($con['changed_price_to'][$k])) {
                        if (!empty($con['changed_price_to'][$k])) {
                            if ($changed_price != "") {
                                $changed_price .= "〜";
                            }
                            $changed_price .= $con['changed_price_to'][$k];
                        } else {
                            if ($changed_price != "") {
                                $changed_price .= "〜";
                            }
                        }
                    }

                    if ($changed_price != "") {
                        if(isset($con['is_close'])){
                            $res['	成約日'][$k] = $changed_price;
                        }else{
                            $res['価格変更日'][$k] = $changed_price;
                        }
                    }
                }
            }

            if ($key == 'ad_checked_from') {
                foreach ($val as $k => $row) {
                    $ad_checked = "";

                    if (!empty($row)) {
                        $ad_checked .= $row;
                    }

                    if (isset($con['ad_checked_to'][$k])) {
                        if (!empty($con['ad_checked_to'][$k])) {
                            if ($ad_checked != "") {
                                $ad_checked .= "〜";
                            }
                            $ad_checked .= $con['ad_checked_to'][$k];
                        } else {
                            if ($ad_checked != "") {
                                $ad_checked .= "〜";
                            }
                        }
                    }

                    if ($ad_checked != "") {
                        $res['広告確認日'][$k] = $ad_checked;
                    }
                }
            }

            if ($key == 'flyer') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['チラシ'][$k] = Config::get('const.TARGET')[$row];
                    }
                }
            }

            if ($key == 'freepaper') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['情報誌'][$k] = Config::get('const.TARGET')[$row];
                    }
                }
            }

            if ($key == 'house_hp') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['広告確認日-自社HP'][$k] = Config::get('const.TARGET')[$row];
                    }
                }
            }

            if ($key == 'portal') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['ポータル'][$k] = Config::get('const.TARGET')[$row];
                    }
                }
            }

            if ($key == 'signboard') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['現地看板'][$k] = Config::get('const.TARGET')[$row];
                    }
                }
            }

            if ($key == 'own_company') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['広告掲載指示-自社HP'][$k] = Config::get('const.PORT')[$row];
                    }
                }
            }

            if ($key == 'suumo') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['SUUMO'][$k] = Config::get('const.PORT')[$row];
                    }
                }
            }

            if ($key == 'homes') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['HOMES'][$k] = Config::get('const.PORT')[$row];
                    }
                }
            }

            if ($key == 'athome') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['athome'][$k] = Config::get('const.PORT')[$row];
                    }
                }
            }

            if ($key == 'revenue') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['収益'][$k] = '有';
                    }
                }
            }

            if ($key == 'is_agreement') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['契約済'][$k] = '有';
                    }
                }
            }

            if ($key == 'total_unit_from') {
                foreach ($val as $k => $row) {
                    $total_unit = "";

                    if (!empty($row)) {
                        $total_unit .= $row . "戸";
                    }

                    if (isset($con['total_unit_to'][$k])) {
                        if (!empty($con['total_unit_to'][$k])) {
                            if ($total_unit != "") {
                                $total_unit .= "〜";
                            }
                            $total_unit .= $con['total_unit_to'][$k] . "戸";
                        } else {
                            if ($total_unit != "") {
                                $total_unit .= "〜";
                            }
                        }
                    }

                    if ($total_unit != "") {
                        $res['階建'][$k] = $total_unit;
                    }

                }
            }

            if ($key == 'shared') {
                $shared_arr = array_chunk($val, 8, true);
                foreach ($shared_arr as $key => $value) {
                    $shared = "";
                    $check_shared = true;
                    foreach ($value as $k => $v) {
                        if (!empty($v)) {
                            if ($check_shared == true) {
                                $shared .= Config::get('const.SHARED')[$v];
                                $check_shared = false;
                            } else {
                                $shared .= ', ' . Config::get('const.SHARED')[$v];
                            }
                        }
                    }
                    if (!empty($shared)) {
                        $res['共用部'][$key] = $shared;
                    }
                }
            }

            if ($key === 'land_condition') {
                $land_condition_arr = array_chunk($val, 2, true);
                foreach ($land_condition_arr as $k => $value) {
                    $land_condition = "";
                    $check_land_condition = true;
                    foreach ($value as $v) {
                        if ($v !== null) {
                            if ($check_land_condition == true) {
                                $land_condition .= Config::get('const.LAND_CONDITION')[$v];
                                $check_land_condition = false;
                            } else {
                                $land_condition .= ', ' . Config::get('const.LAND_CONDITION')[$v];
                            }
                        }
                    }
                    if (!empty($land_condition)) {
                        $res['建築条件'][$k] = $land_condition;
                    }
                }
            }

            if ($key == 'remove_condition') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['条件外し'][$k] = '条件外し可';
                    }
                }
            }

            if ($key == 'vender_name' || $key == 'vender_kind' || $key == 'vender_tel') {
                foreach ($val as $k => $row) {
                    $vender = "";

                    if (isset($con['vender_name'][$k])) {
                        if (!empty($con['vender_name'][$k])) {
                            $vender .= $con['vender_name'][$k];
                        }
                    }

                    if (isset($con['vender_kind'][$k])) {
                        if (!empty($con['vender_kind'][$k])) {
                            $vender .= ' ' . MstManner::getData($con['vender_kind'][$k])[0]['name'] ?? "";
                        }
                    }

                    if (isset($con['vender_tel'][$k])) {
                        if (!empty($con['vender_tel'][$k])) {
                            $vender .= ' ' . $con['vender_tel'][$k];
                        }
                    }

                    if ($vender != "") {
                        $res['業者'][$k] = $vender;
                    }
                }
            }

            if ($key === 'name_apartment') {
                foreach ($val as $k => $row) {
                    if (!empty($row)) {
                        $res['マンション名'][$k] = $row;
                    }
                }
            }

            if ($key === 'section_from' || $key === 'section_to') {
                foreach ($val as $k => $row) {
                    $section = "";

                    if (isset($con['section_from'][$k])) {
                        if (!empty($con['section_from'][$k])) {
                            $section .= $con['section_from'][$k] . '区画';
                        }
                    }

                    if (isset($con['section_to'][$k])) {
                        if (!empty($con['section_to'][$k])) {
                            if ($section != "") {
                                $section .= "〜";
                            }
                            $section .= $con['section_to'][$k] . "区画";
                        }
                    }
                    if ($section != "") {
                        $res['総区画数'][$k] = $section;
                    }
                }
            }
        }


        $res['count_search'] = isset($res['所在地']) ? count($res['所在地']) : 1;
        return $res;
    }
}
