<?php

namespace app\api\controller;

use app\api\extend\Basic;
use app\model\AppointWatchShop;
use app\model\HouseInfos;
use think\helper\Time;

/**
 * Created by PhpStorm.
 * User : zw
 * Date : 2017/12/8
 * Time : 17:18
 * Intro: 预约看铺模块
 */
class AppointmentTime extends Basic
{

    protected $db;
    protected $appointmentTimeModel;
    protected $houseInfoModel;

    function __construct($request = null)
    {
        parent::__construct($request);
        $this->appointmentTimeModel = new AppointWatchShop();
        $this->houseInfoModel = new HouseInfos();
    }

    /**
     * 增加预约楼盘信息
     * @return \think\Response
     */
    public function addAppointmentSeeShop()
    {
        $params = $this->params;
        /*   $params = array(
               "user_id" => 1,
               "house_id" => 1,
               "house_title" => "楼盘1",
               "appellation" => "张三",
               "phone" => "13817616312",
               "expect_time" => time(),
               "other_require" => "没有其他要求"
           );*/
        //todo 验证楼盘信息
        if (isset($params["house_id"]) && isset($params["house_title"])) {
            $where_["house_id"] = $params["house_id"];
            $where_["title"] = $params["title"];
            $houseInfo = $this->houseInfoModel->getHouseDetail("id", $where_);
            if (count($houseInfo) <= 0) {
                return $this->response("101", "楼盘ID或楼盘名错误");
            }
        } else {
            return $this->response("101", "楼盘信息不能为空");
        }
        //todo 参数校验
        if (!isset($params["phone"]) || !preg_match('/1[345678]\d{9}/', $params["phone"])) {
            return $this->response("101", "手机号为空或手机号无效");
        }

        if (!isset($params["expect_time"]) || $params['expect_time'] <= Time::today()) {
            return $this->response("101", "预约时间为空，或者小于当前时间");
        }
        $result = $this->appointmentTimeModel->insertAppointWatchShop($params);
        if ($result["code"] == 200) {
            return $this->response("200", "数据保存成功", $result["msg"]);
        }

        return $this->response("101", "数据保存失败");

    }

    public function getAppointmentSeeShopList()
    {
        /*   $params = array(
           "status" => 2, //看铺状态 0待经纪人确认（确认后存agents_id） 1:待看 2：已看' 如果是待看 0,1 已看是2
           "user_id" => "1",//对应u_users表
           "pageNo" => "1",
           "pageSize" => 15
       );*/

        $params = $this->params;
        $field = "id,user_id,agent_id,house_id,house_title,appellation,phone,expect_time,other_require,status,
                   is_cancel,is_evaluate,create_time";


        $where_ = [];
        if (!isset($params["status"])) {
            return $this->response("101", "请求状态不能为空");
        }
        if (!isset($params["user_id"])) {
            return $this->response("101", "请求用户不能为空");
        }

        $pageNo = empty($params['pageNo']) ? 1 : $params['pageNo'];
        $pageSize = empty($params['pageSize']) ? 15 : $params['pageSize'];

        $where_["status"] = array( "in", $params["status"] );
        $where_["user_id"] = array( "eq", $params["user_id"] );

        $result = $this->appointmentTimeModel->getAppointWatchShopList($pageNo, $pageSize, "id desc", $field, $where_);
        if(count($result) > 0 ){
            return $this->response("200","request success" ,$result);
        }else{
            return $this->response("200","此条件没有找到数据" );
        }
    }


}