<?php

namespace App\Models\traits;

use App\Models\MstCity;
use App\Models\MstFloorType;
use App\Models\MstPrefecture;
use App\Models\MstPropertyType;
use App\Models\MstTown;
use App\Models\RelationArticlePhoto;

trait ArticleUnit
{
    function getIsLandAttribute()
    {
        return in_array($this->property, [1, 2, 3]);
    }

    public function getIsLandNoBuildingConditionAttribute(){
        return $this->isLand && $this->land_condition === 0;
    }

    public function getIsLandHasBuildingConditionAttribute(){
        return $this->isLand && $this->land_condition === 1;
    }

    function getIsUsedHouseAttribute(){
        return $this->isHome && $this->property_sub === 2;
    }

    function getIsNewHouseAttribute(){
        return $this->isHome && $this->property_sub === 1;
    }

    function getIsHomeAttribute()
    {
        return in_array($this->property, [4, 5]);
    }

    function getIsNewPropertyAttribute()
    {
        return $this->property_sub === MstPropertyType::NEW;
    }

    function getIsOldPropertyAttribute()
    {
        return $this->property_sub === MstPropertyType::OLD;
    }


    function getIsMansionAttribute()
    {
        return in_array($this->property, [6, 7]);
    }

    function getIsRoomAttribute()
    {
        return in_array($this->property, [null, 0, '']) && !in_array($this->mansion_id, [0, '', null]);
    }

    function getTrafficInfoAttribute(){
        $list = [];
        if(!empty($this->main_traffic_line) && !empty($this->main_traffic_station) &&
            ($this->main_traffic == 1 and !empty($this->main_traffic_time) or $this->main_traffic==2 and (!empty($this->main_traffic_bus_time) or !empty($this->main_traffic_bus_walk)))){
            $list[] = ($this->main_traffic_line?? "-") .'「'. ($this->main_traffic_station?? "-") .'」駅';
        }
        if($this->main_traffic == 2){
            if ($this->main_traffic_bus_time){
                $list [] = 'バス'.($this->main_traffic_bus_time?? "-").'分';
            }
            if ($this->main_traffic_bus){
                $list [] = $this->main_traffic_bus;
            }
            if($this->main_traffic_bus_walk){
                $list [] = $this->main_traffic_bus_walk;
            }
        }else {
            $list[] = '徒歩'.($this->main_traffic_time?? "-").'分';
        }

        if(isset($this->sub_traffic1_station) && !empty($this->sub_traffic1_line) and !empty($this->sub_traffic1_station) and ($this->sub_traffic1==1 and !empty($this->sub_traffic1_time) or $this->sub_traffic1==2 and (!empty($this->sub_traffic1_bus_time) or !empty($this->sub_traffic1_bus_walk)))){
            $list[] = '<br/>'.$this->sub_traffic1_line.'「'. $this->sub_traffic1_station.'」駅';
            if($this->sub_traffic1 == 2){
                if($this->sub_traffic1_bus_time)
                    $list[] = 'バス'.($this->sub_traffic1_bus_time?? "-") .'分';
                if ($this->sub_traffic1_bus)
                    $list[] ='「'.$this->sub_traffic1_bus .'」';

                if($this->sub_traffic1_bus_walk)
                    $list[] = '停歩'. ($this->sub_traffic1_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->sub_traffic1_time?? "-").'分';
            }
        }

        if(isset($this->sub_traffic2_station) && !empty($this->sub_traffic2_line) and !empty($this->sub_traffic2_station) and ($this->sub_traffic2==1 and !empty($this->sub_traffic2_time) or $this->sub_traffic2==2 and (!empty($this->sub_traffic2_bus_time) or !empty($this->sub_traffic2_bus_walk)))){
            $list[] = '<br/>'.$this->sub_traffic2_line.'「'. $this->sub_traffic2_station.'」駅';
            if($this->sub_traffic2 == 2){
                if($this->sub_traffic2_bus_time)
                    $list[] = 'バス'.($this->sub_traffic2_bus_time?? "-") .'分';
                if ($this->sub_traffic2_bus)
                    $list[] ='「'.$this->sub_traffic2_bus .'」';

                if($this->sub_traffic2_bus_walk)
                    $list[] = '停歩'. ($this->sub_traffic2_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->sub_traffic2_time?? "-").'分';
            }
        }

        if(isset($this->sub_traffic3_station) && !empty($this->sub_traffic3_line) and !empty($this->sub_traffic3_station) and ($this->sub_traffic3==1 and !empty($this->sub_traffic3_time) or $this->sub_traffic3==2 and (!empty($this->sub_traffic3_bus_time) or !empty($this->sub_traffic3_bus_walk)))){
            $list[] = '<br/>'.$this->sub_traffic3_line.'「'. $this->sub_traffic3_station.'」駅';
            if($this->sub_traffic3 == 2){
                if($this->sub_traffic3_bus_time)
                    $list[] = 'バス'.($this->sub_traffic3_bus_time?? "-") .'分';
                if ($this->sub_traffic3_bus)
                    $list[] ='「'.$this->sub_traffic3_bus .'」';

                if($this->sub_traffic3_bus_walk)
                    $list[] = '停歩'. ($this->sub_traffic3_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->sub_traffic3_time?? "-").'分';
            }
        }
        if(isset($this->sub_traffic4_station) && !empty($this->sub_traffic4_line) and !empty($this->sub_traffic4_station) and ($this->sub_traffic4==1 and !empty($this->sub_traffic4_time) or $this->sub_traffic4==2 and (!empty($this->sub_traffic4_bus_time) or !empty($this->sub_traffic4_bus_walk)))){
            $list[] = '<br/>'.$this->sub_traffic4_line.'「'. $this->sub_traffic4_station.'」駅';
            if($this->sub_traffic4 == 2){
                if($this->sub_traffic4_bus_time)
                    $list[] = 'バス'.($this->sub_traffic4_bus_time?? "-") .'分';
                if ($this->sub_traffic4_bus)
                    $list[] ='「'.$this->sub_traffic4_bus .'」';

                if($this->sub_traffic4_bus_walk)
                    $list[] = '停歩'. ($this->sub_traffic4_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->sub_traffic4_time?? "-").'分';
            }
        }
        if(isset($this->sub_traffic5_station) && !empty($this->sub_traffic5_line) and !empty($this->sub_traffic5_station) and ($this->sub_traffic5==1 and !empty($this->sub_traffic5_time) or $this->sub_traffic5==2 and (!empty($this->sub_traffic5_bus_time) or !empty($this->sub_traffic5_bus_walk)))){
            $list[] = '<br/>'.$this->sub_traffic5_line.'「'. $this->sub_traffic5_station.'」駅';
            if($this->sub_traffic5 == 2){
                if($this->sub_traffic5_bus_time)
                    $list[] = 'バス'.($this->sub_traffic5_bus_time?? "-") .'分';
                if ($this->sub_traffic5_bus)
                    $list[] ='「'.$this->sub_traffic5_bus .'」';

                if($this->sub_traffic5_bus_walk)
                    $list[] = '停歩'. ($this->sub_traffic5_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->sub_traffic5_time?? "-").'分';
            }
        }

        if(isset($this->else_traffic_station) && !empty($this->else_traffic_line) and !empty($this->else_traffic_station) and ($this->else_traffic==1 and !empty($this->else_traffic_time) or $this->else_traffic==2 and (!empty($this->else_traffic_bus_time) or !empty($this->else_traffic_bus_walk)))){
            $list[] = '<br/>'.$this->else_traffic_line.'「'. $this->else_traffic_station.'」駅';
            if($this->else_traffic == 2){
                if($this->else_traffic_bus_time)
                    $list[] = 'バス'.($this->else_traffic_bus_time?? "-") .'分';
                if ($this->else_traffic_bus)
                    $list[] ='「'.$this->else_traffic_bus .'」';

                if($this->else_traffic_bus_walk)
                    $list[] = '停歩'. ($this->else_traffic_bus_walk?? "-") .'分';
            }else {
                $list[] = '徒歩'.($this->else_traffic_time?? "-").'分';
            }
        }
        return implode('', $list);
    }


    public function getAddressInfoAttribute(){
        $address = "";

        if (isset($this->other_address)){
            $address .= $this->other_address;
        } else {
            if (isset($this->address1)) {
                $add = MstPrefecture::getData($this->address1);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($this->address2)) {
                $add = MstCity::getData($this->address1, $this->address2);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($this->address3)) {
                $add = MstTown::getData($this->address1, $this->address2, $this->address3);
                if (!empty($add)) {
                    $address .= $add[0]["name"];
                }
            }
            if (isset($this->address4)) {
                $address .= $this->address4;
            }
        }
        if ($address === ''){
            $address = '-/-';
        }
       return $address;
    }

    public function getFloorPlanInfoAttribute(){
        $floor_type_list = MstFloorType::getData();
        $floor_type = [];
        foreach ($floor_type_list as $row) {
            $floor_type[$row['code']] = $row['name'];
        }
        if (isset($this->floor_plan_type)) {
            return $this->floor_plan . $floor_type[$this->floor_plan_type];
        }
        return '-';
    }


    public function getAgeInfoAttribute(){
        if ($this->age_year == null && $this->age_month == null && $this->completed_contract_month != null){
            return '契約後'.$this->completed_contract_month .'ヶ月';
        }elseif($this->age_year > date("Y") || ($this->age_year == date("Y") && $this->age_month >= date("m") )){
            return ($this->age_year?? "-").'年' .($this->age_month?? "-").'月';
        }else {
            return ($this->age_year?? "-").'年' .($this->age_month?? "-").'月';
        }
    }

    public function getFloorDetailInfo(){
        $info = '';
        if($this->whereabouts or $this->maisonette==1){
            if($this->maisonette==1){
                $info .= $this->maisonette_from ? $this->maisonette_from : '~' .($this->maisonette_to ? $this->maisonette_to : ''). '階';
            }elseif($this->whereabouts){
                $info .= ($this->whereabouts?? "-").'階';
            }else{
                $info .= '-';
            }
        }
        if($this->floor){
            if(!empty($this->mansion_id)){
                if(optional($this->getMansion())->underground){
                    $info .= '/地上'.optional($this->getMansion())->floor .'階 地下'. optional($this->getMansion())->underground. '階建';
                }elseif(optional($this->getMansion())->floor){
                    $info .= '/'.optional($this->getMansion())->floor .'階建';
                }
            }else {
                if(optional($this)->underground){
                    $info .= '/地上'.optional($this)->floor .'階 地下'.optional($this)->underground . '階建';
                }elseif(optional($this)->floor)
                    $info .= '/'.optional($this)->floor . '階建';
            }
        }
        if(!$this->whereabouts and $this->maisonette!=1 and !$this->floor){
            $info .= '-';
        }

//        $first = mb_substr($info, 0, 1);
//
//        if ($first == '/'){
//            return substr($info, 1);
//        }
        return $info;
    }

    public function getFloorInfoAttribute(){
        $info = ($this->floor_plan_text?? "-"). '・';
        $info .= $this->getFloorDetailInfo();
        return $info;
    }

    public function getFileCountInfoAttribute(){
        $isMansion = [6,7];
        $isHouse = [4,5];
        if(empty($this->property) && is_numeric($this->mansion_file_count))
            $res = '外'.$this->mansion_file_count .'/内'.$this->file_count;
        elseif (in_array($this->property, $isMansion))
            $res =  '外'.$this->mansion_file_count;
        elseif (in_array($this->property, $isHouse))
            $res = '外'.$this->mansion_file_count.'/内'.$this->file_count;
        else
            $res = $this->file_count;
        return $res;
    }


    public function getPropertyLabelAttribute(){
        if($this->property==1 or $this->property==2 or $this->property==3){
            return 'label-land';
        }elseif($this->property==4 or $this->property==5){
            if ($this->propery_sub == 1){
                return 'label-nhouse';
            }else {
                return 'label-uhouse';
            }
        }else {
            return 'label-mansion';
        }
    }

    public function getTopFilePathInfoAttribute()
    {
        $path1 = asset(root('img/noimage2.jpg'));
        $path2 = asset(root('img/noimage2.jpg'));
        if (!empty($this->mansion_id)) {
            if (!empty(optional($this)->image_mansion)) {
                $path1 = asset(str_replace('public', '/storage', optional($this)->image_mansion->file_path));
            }
            if (!empty($this->floor_file_path)) {
                $path2 = asset($this->floor_file_path);
            } elseif (!empty($this->sheet_file_path3) && $this->sheet_file_type == 1) {
                $path2 = asset(str_replace('public/', 'storage/', $this->sheet_file_path3));
            }
        } else {
            if (!empty($this->top_file_path)) {
                $path1 = asset($this->top_file_path);
            }
            if (!empty($this->floor_file_path)) {
                $path2 = asset($this->floor_file_path);
            } elseif (!empty($this->sheet_file_path3) && $this->sheet_file_type == 1) {
                $path2 = asset(str_replace('public/', 'storage/', $this->sheet_file_path3));
            }
        }
        return [$path1, $path2];
    }

    public function getSchoolInfo(){
        $res = [];
        if(isset($this->primary_school_name) && $this->primary_school_name != null) {
            $val = $this->primary_school_name .' 約'.$this->primary_school_distance .'m ';

            if(isset($this->primary_school_time)){
                $val .= '/ 徒歩'.$this->primary_school_time .'分';
            }
            $res[] = $val;
        }
        if(isset($this->primary_school_name2) && $this->primary_school_name2 != null) {
            $val = $this->primary_school_name2 .' 約'.$this->primary_school2_distance .'m ';

            if(isset($this->primary_school_time2)){
                $val .= '/ 徒歩'.$this->primary_school_time2 .'分';
            }
            $res[] = $val;
        }
        if(isset($this->primary_school_name3) && $this->primary_school_name3 != null) {
            $val = $this->primary_school_name3 .' 約'.$this->primary_school3_distance .'m ';

            if(isset($this->primary_school_time3)){
                $val .= '/ 徒歩'.$this->primary_school_time3 .'分';
            }
            $res[] = $val;
        }
        if(isset($this->secondary_school_name) && $this->secondary_school_name != null) {
            $val = $this->secondary_school_name .' 約'.$this->secondary_school_distance .'m ';

            if(isset($this->secondary_school_time)){
                $val .= '/ 徒歩'.$this->secondary_school_time .'分';
            }
            $res[] = $val;
        }
        if(isset($this->secondary_school_name2) && $this->secondary_school_name2 != null) {
            $val = $this->secondary_school_name2 .' 約'.$this->secondary_school2_distance .'m ';

            if(isset($this->secondary_school_time2)){
                $val .= '/ 徒歩'.$this->secondary_school_time2 .'分';
            }
            $res[] = $val;
        }
        if(isset($this->secondary_school_name3) && $this->secondary_school_name3 != null) {
            $val = $this->secondary_school_name3 .' 約'.$this->secondary_school3_distance .'m ';

            if(isset($this->secondary_school_time3)){
                $val .= '/ 徒歩'.$this->secondary_school_time3 .'分';
            }
            $res[] = $val;
        }
        return implode('<br>', $res);

    }

    public function getLandRightInfo(){
        $res = [];
        $dataLand = $this;
        if ($mansion = $this->getMansion())
            $dataLand = $mansion;

        if($this->property == null){
            switch($dataLand->land_right){
                case 2:
                case 3:
                $res[] = '借地権種別：'. getLeaseholdKind($dataLand->leasehold_kind);
                    break;
            }
            $landUnits = ["", "月", "年", "一括"];
            $landRent = '地代：';

            if ($this->land_rent == 1){
                $landRent .= $this->land_rent_val .'円/';
                $landRent .= $landUnits[$this->land_rent_unit] ?? "";
            }else {
                $landRent .= '無';
            }
            $res[] = $landRent;

            switch ($dataLand->land_right){
                case 2:
                case 3:
                    $res[] = '借地期間：'. ($dataLand->leasehold_period == 1 ? "残存" : "新規") . '('. $this->leasehold_period_year .'年' . $this->leasehold_period_month .'ヶ月)';
                    $res[] = '権利金：'.($dataLand->right_cost == 0 ? '無': '有(価格に含む) ('. $dataLand->right_cost_val .'万円)');
                    $res[] = '保証金：'.($dataLand->deposit_cost == 0 ? '無' : '有'. '('. $dataLand->deposit_cost_val .'万円)');
                    $res[] = '敷金：'.($dataLand->security_deposit_cost == 0 ? '無' : '有'. '('. $dataLand->security_deposit_cost_val .'万円)');
                    $res[] = '借地権その他契約事項：'. $dataLand->other_leasehold;

                if ($dataLand->land_right == 3){
                    $res[] = '借地権割合：'. $dataLand->leasehold_rate .'%';
                }
                $isMore = [3, 4, 7, 8];
                if (in_array($dataLand->leasehold_kind, $isMore)){
                    $registrationsText = ["未選択", "可", "不可", "相談"];
                    $seasonsText = ["未選択", "一定期間毎", "事前協議により決定"];
                    $costsText = ["未選択", "公式", "事前協議により決定"];
                    $transfersText = ["未選択", "可", "不可"];

                    $res[] = '定期借地権設定登記：'.$registrationsText[$dataLand->regular_leased_registration] ?? "";
                    $res[] = '定期借地権賃料改定時期：'.($seasonsText[$dataLand->regular_leased_season] ?? ""). ($dataLand->regular_leased_season == 1 ? $dataLand->regular_leased_season_nen . "年毎" : "");
                    $res[] = '定期借地権賃料改定額：'.$costsText[$dataLand->regular_leased_cost] ?? "";
                    $res[] = '定期借地権譲渡転貸：'. $transfersText[$dataLand->regular_leased_transfer] ?? "";

                    if ($dataLand->regular_leased_transfer == 1){
                        $transferMethodsText = ["未選択", "承諾不要", "通知要", "承諾要"];
                        $transferNecessitiesText = ["未選択", "要", "不要", "未定"];
                        $transferConsentsText = ["未選択", "地主", "転貸主"];
                        $res[] = '  承諾要否・方法：'.$transferMethodsText[$dataLand->regular_leased_transfer_method];
                        $res[] = ' 承諾料要否：'. $transferNecessitiesText[$dataLand->regular_leased_transfer_necessity];
                        $res[] = ' 承諾者：'.$transferConsentsText[$dataLand->regular_leased_transfer_consent];
                    }
                }
                break;
            }

        }else {
            switch($dataLand->land_right){
                case 2:
                case 3:
                    $res[] = '借地権種別：'. getLeaseholdKind($dataLand->leasehold_kind);

                    $landUnits = ["", "月", "年", "一括"];
                    $landRent = '地代：';

                    if ($this->land_rent == 1){
                        $landRent .= $this->land_rent_val .'円/';
                        $landRent .= $landUnits[$this->land_rent_unit] ?? "";
                    }else {
                        $landRent .= '無';
                    }
                    $res[] = $landRent;
                    $res[] = '借地期間：'. ($dataLand->leasehold_period == 1 ? "残存" : "新規") . '('. $this->leasehold_period_year .'年' . $this->leasehold_period_month .'ヶ月)';
                    $res[] = '権利金：'.($dataLand->right_cost == 0 ? '無': '有(価格に含む) ('. $dataLand->right_cost_val .'万円)');
                    $res[] = '保証金：'.($dataLand->deposit_cost == 0 ? '無' : '有'. '('. $dataLand->deposit_cost_val .'万円)');
                    $res[] = '敷金：'.($dataLand->security_deposit_cost == 0 ? '無' : '有'. '('. $dataLand->security_deposit_cost_val .'万円)');
                    $res[] = '借地権その他契約事項：'. $dataLand->other_leasehold;

                    if ($dataLand->land_right == 3){
                        $res[] = '借地権割合：'. $dataLand->leasehold_rate .'%';
                    }

                    $isMore = [3, 4, 7, 8];
                    if (in_array($dataLand->leasehold_kind, $isMore)){
                        $registrationsText = ["未選択", "可", "不可", "相談"];
                        $seasonsText = ["未選択", "一定期間毎", "事前協議により決定"];
                        $costsText = ["未選択", "公式", "事前協議により決定"];
                        $transfersText = ["未選択", "可", "不可"];

                        $res[] = '定期借地権設定登記：'.$registrationsText[$dataLand->regular_leased_registration] ?? "";
                        $res[] = '定期借地権賃料改定時期：'.($seasonsText[$dataLand->regular_leased_season] ?? ""). ($dataLand->regular_leased_season == 1 ? $dataLand->regular_leased_season_nen . "年毎" : "");
                        $res[] = '定期借地権賃料改定額：'.$costsText[$dataLand->regular_leased_cost] ?? "";
                        $res[] = '定期借地権譲渡転貸：'. $transfersText[$dataLand->regular_leased_transfer] ?? "";

                        if ($dataLand->regular_leased_transfer == 1){
                            $transferMethodsText = ["未選択", "承諾不要", "通知要", "承諾要"];
                            $transferNecessitiesText = ["未選択", "要", "不要", "未定"];
                            $transferConsentsText = ["未選択", "地主", "転貸主"];
                            $res[] = '  承諾要否・方法：'.$transferMethodsText[$dataLand->regular_leased_transfer_method];
                            $res[] = ' 承諾料要否：'. $transferNecessitiesText[$dataLand->regular_leased_transfer_necessity];
                            $res[] = ' 承諾者：'.$transferConsentsText[$dataLand->regular_leased_transfer_consent];
                        }
                    }
                    break;
            }
        }
        return $res;
    }

    public function getCurrentSatusInfo(){
        $status = getCurrentStatusName($this->current_status);
        if(getCurrentStatusName($this->current_status, $this->property)=='賃貸中' and ($this->current_status_val or $this->current_status_rate)){
            $status .= '(';
            if($this->current_status_val){
                $status .= '賃料：'.number_format($this->current_status_val, 0, ",", ","). '円/月 ';
            }
            if($this->current_status_rate){
                $status .= '利回り：'.$this->current_status_rate .'%';
            }
            $status .= ')';
        }
        return $status;
    }

    public function getEventScheduleNameInfo(){
        return !empty($this->event_category) && !empty($this->event_schedule) ? $this->getEventScheduleName() : '-';
    }

    public function getEventCategoryNameInfo(){
       return !empty($this->event_category) ? $this->getEventCategoryName() : '-';
    }

    public function getBalconyDirectionInfo(){
        $res = '';
        if($this->balcony_direction){
            $res = $this->getDirectionName($this->balcony_direction) . '向き';
        }
        $res .= $this->balcony_area_val ?? '-';
        $res .= '&nbsp;m&sup2';
        return $res;
    }

    public function getClassficationInfo(){
        $data = [];
        if ($this->classfication != 1) {
            if ($this->property == 4 || $this->property == 5) {
                $content = '';
                if ($this->classfication == 1) {
                    $content = '-';
                } else {
                    $content = $this->trading_classfication == 1 ? '一棟売' : '一括売';
                }
                $data[] = ['title' => '売買区分', 'content' => $content];

            } elseif (empty($this->property)) {
                if ($this->classfication == 1) {
                    $content = '-';
                } else {
                    $content = $this->trading_classfication == 1 ? '一棟売' : '一括売';
                }
                $data[] = ['title' => '売買区分', 'content' => $content];
            }

            if ($this->classfication == 2) {
                if ($this->classfication == 1 || $this->classfication == 3) {
                    $content = '-';
                } else {
                    $content = (!empty($this->use_method) && $this->classfication == 2) ? $this->getUserMethod() : '-';
                }
                $data[] = ['title' => '事業用用途', 'content' => $content];

                if ($this->property != 1 and $this->property != 2 and $this->property != 3) {
                    $names = ['-', '造作譲渡あり', '造作譲渡なし', '設備無償譲渡', 'スケルトン渡し'];
                    $data[] = ['title' => '事業用引渡し状態', 'content' => $names[$this->business_status] ?? ''];
                }

            }

            if ($this->classfication == 3) {
                $data[] = ['title' => '', 'content' => $this->getInvestmentPerformance()];
            }
        }
        return $data;
    }

    public function getLandStatusInfo(){
        if($this->land_condition1 or $this->land_condition_2 or $this->land_condition3){
            $res = '';
            if($this->land_condition_area1){
                $res .= '傾斜部分 '. $this->land_condition_area1;
                if($this->land_condition_unit1==2){
                    $res .= 'm&sup2';
                } elseif($this->land_condition_unit1==3){
                    $res .= '％';
                }
                $res .= '<br>';
            }

            if($this->land_condition_area2){
                $res .= '路地上部分 '. $this->land_condition_area2;
                if($this->land_condition_unit2==2){
                    $res .= 'm&sup2';
                } elseif($this->land_condition_unit2==3){
                    $res .= '％';
                }
                $res .= '<br>';
            }

            if($this->land_condition_area3){
                $res .= '高圧線下 '.$this->land_condition_area3 .'m&sup2;';
                $res .= '<br>';
            }

            return $res;
        }else {
            return '-';
        }
    }

    public function getUseAreaInfo(){
        $res = $this->use_area_name??'-';
        if($this->use_area_name != "" || $this->use_area_name != null){
            $res .= '<br>';
        }

        if($this->use_area_name_sub != ""){
            $res .= '一部 '.$this->use_area_name_sub .'を含む<br>';
        }

        if($this->use_area_name_sub2 != ""){
            $res .= '一部 '.$this->use_area_name_sub2 .'を含む<br>';
        }

        return $res;
    }

    public function getAnotherCostInfo(){
        $res = '';
        if($this->another_cost1){
            $res =  getAnotherCost($this, [1]);
        }
        if($this->another_cost2){
            $res .= '<br/>'. getAnotherCost($this, [2]);
        }
        if($this->another_cost3){
            $res .= '<br/>'. $this->another_cost3;
        }

        if(!$this->another_cost1 and !$this->another_cost2 and !$this->another_cost3){
            $res .= '-';
        }
        return $res;
    }

    public function getFilePath2(){
//        $verticalFile = RelationArticlePhoto::where('article_id', $this->building_id)->whereNotNull('vertical')->first();
        return str_replace('public', '/storage', $this->file_path2 ?? null);
    }

    public function getVertical(){
        $verticalFile = RelationArticlePhoto::where('article_id', $this->building_id)->whereNotNull('vertical')->first();
        return $verticalFile->vertical ?? null;
    }
    public function getPoint4(){
        $res = strlen($this->point_recommend4) > 15 ? mb_substr($this->point_recommend4, 0, 15,"UTF-8") : $this->point_recommend4;
        return $res ?? '';
    }
    public function getPoint3(){
        $res = strlen($this->point_recommend3) > 15 ? mb_substr($this->point_recommend3, 0, 15,"UTF-8") : $this->point_recommend3;
        return $res ?? '';
    }
    public function getPoint2(){
        $res = strlen($this->point_recommend2) > 15 ? mb_substr($this->point_recommend2, 0, 15,"UTF-8") : $this->point_recommend2;
        return $res ?? '';
    }
    public function getPoint(){
        $res = strlen($this->point_recommend) > 15 ? mb_substr($this->point_recommend, 0, 15,"UTF-8") : $this->point_recommend;
        return $res ?? '';
    }
    public function getMemobusiness($limit = 100){
        $res = strlen($this->memo_business) > $limit ? mb_substr($this->memo_business, 0, $limit,"UTF-8") : $this->memo_business;
        return nl2br($res ?? '');
    }
    public function getCatchCopy20(){
        $res = strlen($this->catchcopy) > 20 ? mb_substr($this->catchcopy, 0, 20,"UTF-8") : $this->catchcopy;
        return $res ?? '';
    }
    public function getCatchCopy50(){
        $res = strlen($this->catchcopy) > 50 ? mb_substr($this->catchcopy, 0, 50,"UTF-8") : $this->catchcopy;
        return $res ?? '';
    }
    public function getHomeMemo20(){
        $res = strlen($this->homes_memo) > 20 ? mb_substr($this->homes_memo, 0, 20,"UTF-8") : $this->homes_memo;
        return $res ?? '';
    }
    public function getHomeMemo50(){
        $res = strlen($this->homes_memo) > 50 ? mb_substr($this->homes_memo, 0, 50,"UTF-8") : $this->homes_memo;
        return $res ?? '';
    }

    public function getSheetCopy($limit = 50){
        $res = strlen($this->sheet_copy) > $limit ? mb_substr($this->sheet_copy, 0, $limit, "UTF-8") : $this->sheet_copy;
        return nl2br($res ?? '');
    }

    public function getSheetCopy2($limit = 25){
        $res = strlen($this->sheet_copy_2) > $limit ? mb_substr($this->sheet_copy_2, 0, $limit, "UTF-8") : $this->sheet_copy_2;
        return $res ?? '';
    }

    public function getMemobusiness2($limit = 70){
        $res = strlen($this->memo_business_2) > $limit ? mb_substr($this->memo_business_2, 0, $limit,"UTF-8") : $this->memo_business_2;
        return $res ?? '';
    }

    public function getSheetPoint($limit = 200){
        $res = strlen($this->sheet_point) > $limit ? mb_substr($this->sheet_point, 0, $limit, "UTF-8") : $this->sheet_point;
        return nl2br($res ?? '');
    }

    public function isOwnCompanyPublished(): bool
    {
        return $this->own_company == getConstant('OWN_COMPANY_PUBLISHED');
    }

    public function isPropertyRoom(): bool
    {
        return is_null($this->property);
    }

    public function isPropertyLand(): bool
    {
        $property = $this->property;

        return !is_null($property) && in_array($property, [
            getConstant('ARTICLE_PROPERTY_LAND'),
            getConstant('ARTICLE_PROPERTY_LAND_LEASE'),
            getConstant('ARTICLE_PROPERTY_LAND_OWN'),
        ]);
    }

    public function isPropertyHouse(): bool
    {
        $property = $this->property;

        return !is_null($property) && in_array($property, [
            getConstant('ARTICLE_PROPERTY_DETACHED_HOUSE'),
            getConstant('ARTICLE_PROPERTY_TERRACE_HOUSE'),
        ]);
    }

    public function isPropertyMansion(): bool
    {
        $property = $this->property;

        return !is_null($property) && in_array($property, [
            getConstant('ARTICLE_PROPERTY_MANSION'),
            getConstant('ARTICLE_PROPERTY_TOWN_HOUSE'),
        ]);
    }

    public function isPropertyLot(): bool
    {
        $property = $this->property;

        return !is_null($property) && in_array($property, [
            getConstant('ARTICLE_PROPERTY_LOT'),
        ]);
    }

    public function getEditUrl(): ?string
    {
        $buildingId = $this->building_id;
        if($this->isPropertyRoom()){
            return route('showEditRoom', ['mid' => $this->mansion_id, 'id' => $buildingId]);
        }

        if ($this->isPropertyLand()) {
            return route('showEditLand', ['id' => $buildingId]);
        }

        if ($this->isPropertyHouse()) {
            return route('showEditHouse', ['id' => $buildingId]);
        }

        if ($this->isPropertyMansion()) {
            return route('showEditMansion', ['id' => $buildingId]);
        }

        if ($this->isPropertyLot()) {
            return route('showEditLot', ['id' => $buildingId]);
        }

        return '';
    }

    public function isSendEmail(): bool
    {
        return $this->status == 1
            && $this->price != ''
            && $this->land_area_val != ''
            && ($this->zip != '' && $this->address1 != '' && $this->address2 != '' && $this->address3 != '' && $this->address4 != '')
            && ($this->main_traffic_line != '' && $this->main_traffic_station != '' && ($this->main_traffic_time != '' || ($this->main_traffic_bus_time != '' && $this->main_traffic_bus != '' && $this->main_traffic_bus_walk != '')))
            && $this->floor_file_path != '';
    }

    public function isVertical(): bool
    {
        return $this->vertical == getConstant('ARTICLE_VERTICAL');
    }

    public function getNameWithGouchi(): ?string
    {
        $name = !empty($this->article_name) ? $this->article_name : $this->name;

        return getBukkenNameWithGouchi($name, $this->name_gouchi);
    }
}
