<?php
require_once(PLUGIN_DIR . 'Dao/SearchDao.php');
require_once(PLUGIN_DIR . 'Dao/InsertDao.php');
require_once(PLUGIN_DIR . 'Tools/Image.php');
require_once(PLUGIN_DIR . 'Tools/Core.php');
require_once(PLUGIN_DIR . 'Admin/Tospur_House.php');
class House extends Tospur_House{
    public static function init_view(){
        global $wpdb;
        $type = $_POST["type"];
        $context = array();
        $context['siteUrl'] = get_site_url();
        if($type==1){
            $insert_tospur_house_array = array(
                'name' => $_POST['housename'],
                'average_price' => $_POST['average_price'],
                'latest_news' => $_POST['latest_news'],
                'address' => $_POST['address'],
                'traffic' => $_POST['traffic'],
                'periphery' => $_POST['periphery'],
                'developer' => $_POST['developers'],
                'check_in_time' => $_POST['check_in_time'],
                'property_age' => $_POST['property_age'],
                'decoration' => $_POST['decoration'],
                'covered_area' => $_POST['covered_area'],
                'volume_rate' => $_POST['volume_rate'],
                'greening_rate' => $_POST['greening_rate'],
                'households' => $_POST['households'],
                'parking_spaces' => $_POST['parking_spaces'],
                'property_management' => $_POST['property_management'],
                'overview' => $_POST['overview'],
                'city_id' => $_POST['baseCity'],
                'district_id' => $_POST['baseAreaId'],
                'plate_id' => $_POST["basePlateId"],
                'room_id' => $_POST['baseRoom'],
                "location" => $_POST["location"],
                "property_money" => $_POST["property_money"],
                'community_name' => $_POST["community_name"],
            );
            $wpdb->query("START TRANSACTION");
            if(isset($_POST['houseId'])){
                $insert_tospur_house_array['status'] = 0;
                $result = House::data_update($_POST['houseId'],$insert_tospur_house_array);
                if($result != 203){
                    $wpdb->query("ROLLBACK");
                    print_r($result);
                    echo "新房房源修改失败";
                }else{
                    $wpdb->query("COMMIT");
                    echo "新房房源修改成功";
                }
            }else{
                $insert_tospur_house_array['status'] = $_POST['status'];
                $result = House::data_insert($insert_tospur_house_array);
                if($result != 200){
                    $wpdb->query("ROLLBACK");
                    print_r($result);;
                    echo "新增房源失败";
                }else{
                    $wpdb->query("COMMIT");
                    echo "新增房源成功";
                }
            }
            exit;
        }else if(isset($_GET['edit'])){
            $context['houseId'] = $_GET['id'];
            $context = array_merge($context,SearchDao::getDetailInfo($_GET['id'],"edit"));
            $context["district"] = SearchDao::searchCity($context['result']->city_id);
            $context["plate"] = SearchDao::searchCity($context['result']->city_id,$context['result']->district_id);
            $context["mark"] = SearchDao::searchHouseTag($_GET['id']);
            $context["searchStatus"] = SearchDao::searchStatus($_GET['id'],1);
        }
        $context['role'] = House::getCurrentRole();
        $context["city"] = SearchDao::searchCity();
        $context["buildProperty"] = SearchDao::searchBuildProperty();
        $context["room"] = SearchDao::searchRoom();
        $context["photoType"] = SearchDao::searchPhotoType();
        $context["status"] = searchDao::searchStatusType(1);
        Timber::render("newhouse.html",$context);
    }

    public static function data_update($houseId,$insert_tospur_house_array){
        global $wpdb;
        $data = $_POST["data"];
        $wpdb->update(Config::TOSPUR_HOUSE_TABLE,$insert_tospur_house_array,array("id" => $houseId));
        $result = 203;
        $exist_ids = array();
        if(isset($_POST['exists'])){
            foreach($_POST['exists'] as $id => $item){
                $wpdb->update(Config::A_DISTRICT_AREA_TABLE,array(
                    "buildproperty_id" => $item['buildProperty'],
                    "house_area" => $item['housearea']
                ),array("image_id" => $id));
                $exist_ids[] = $id;
            }
            $old_exists_ids = explode(",",$_POST['exists_ids']);
            $delete_ids = array();
            foreach(array_diff($old_exists_ids,$exist_ids) as $key => $id){
                $delete_ids[] = $id;
            }
            $delete_ids = implode(",",$delete_ids);
            $wpdb->query("delete from ".Config::A_DISTRICT_AREA_TABLE." where house_id = {$houseId} and image_id in ({$delete_ids});");
            $wpdb->query("delete from ".Config::A_HOUSE_IMAGE_TABLE." where house_id = {$houseId} and image_id in ({$delete_ids});");
        }
        InsertDao::addMainImage($houseId,$data);

        $exists_photo_ids = array();
        if(isset($_POST['exists_photo'])){
            foreach($_POST['exists_photo'] as $id => $item){
                $wpdb->update(Config::TOSPUR_IMAGE_TABLE,array(
                    "image_type" => $item['type'],
                ),array("id" => $id));
                $exists_photo_ids[] = $id;
            }
        }
        $old_exists_photo_ids = explode(",",$_POST['exists_photo_ids']);
        $delete_photo_ids = array();
        foreach(array_diff($old_exists_photo_ids,$exists_photo_ids) as $key => $id){
            $delete_photo_ids[] = $id;
        }
        $delete_photo_ids = implode(",",$delete_photo_ids);
        $wpdb->query("delete from ".Config::A_HOUSE_IMAGE_TABLE." where house_id = {$houseId} and image_id in ({$delete_photo_ids});");

        $wpdb->delete(Config::A_HOUSE_RECOMMEND_TABLE,array("house_id" => $houseId));
        $res = InsertDao::addRecommend($houseId,$data);
        if($res == 504){
            $result = $res;
            echo "推荐房源修改失败";
        }

        $wpdb->delete(Config::A_HOUSE_USER_TABLE,array("house_id" => $houseId));
        $res = InsertDao::addRecConsultant($houseId,$data);
        if($res == 505){
            $result = $res;
            echo "推荐置业顾问修改失败";
        }
        InsertDao::addHouseTag($_POST['mark'],$houseId);
        return $result;
    }

    public static function data_insert($params){
        global $wpdb;
        //房源与类型以及面积信息
        $data = $_POST["data"];
        //获取新房信息，存入tospur_house表
        $params['user_id'] = get_current_user_id();
        $params['creattime'] = date("Y-m-d H:i:s");
        $params['house_type'] = 0;
        $res = $wpdb->get_results('SELECT * FROM tospur_house WHERE name="' . $_POST['housename'] . '" and address="' . $_POST['address'] . '" and house_type=0', OBJECT);
        if (!$res) {
            $houseId =  InsertDao::insert_tospur_house($params);

            InsertDao::addMainImage($houseId,$data);

            InsertDao::addRecommend($houseId,$data);

            InsertDao::addRecConsultant($houseId,$data);

            InsertDao::addHouseTag($_POST['mark'],$houseId);

        }else{
            return 506;
        }
        return 200;
    }

} 