<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/10/30
 * Time: 9:51 AM
 * 楼盘评测
 */

namespace backend\controllers;

use backend\base\CommonController;
use backend\base\Help;
use Yii;
use common\models\HouseComment;
use common\models\HouseCommentreply;

class RoutinecommentController extends CommonController
{
    public function actionHome()
    {
        return $this->render('home');
    }

    /*
     * 点评楼盘统计列表
     */
    public function actionHomeform()
    {

        $model = new HouseComment();
        $rows = $model->getList(Yii::$app->request->post());
        if ($rows) {
            foreach ($rows as &$val) {
                $val['yes'] = HouseComment::find()->andWhere(['del' => 1])->andWhere(['read' => 2])->andWhere(['hid' => $val['hid']])->count();
                $val['no'] = HouseComment::find()->andWhere(['del' => 1])->andWhere(['read' => 1])->andWhere(['hid' => $val['hid']])->count();
                $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
                if (isset(Yii::$app->params['houseCommentReadState'][$val['read']])) {
                    $val['read'] = Yii::$app->params['houseCommentReadState'][$val['read']];
                }


            }
            return Help::JsonData(0, Yii::t('app', 'get_success'), $model->Total(), $rows);
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'get_error'));
    }

    //编辑点评列表
    public function actionOwnhome()
    {
        return $this->render('ownhome', ['type' => 1]);
    }

    //客户点评列表
    public function actionGuesthome()
    {
        return $this->render('ownhome', ['type' => 2]);
    }

    public function actionCreate()
    {
        $model = new \common\models\HouseComment();

        return $this->render('create', ['label' => $model->xuqiu]);
    }

    public function actionCreateform()
    {
        $input = Yii::$app->request->post();
        $model = new HouseComment();
        $input['ip'] = Yii::$app->request->userIP;
        $true = $model->Iplimit($input);
        if ($true === false) return Help::JsonCode(Help::ERROR, '一个楼盘最多只能点评2次');

        $input['initiator'] = 1;

        if ($model->load($input, '') && $model->save()) {
            return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_success'));
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'add_error'), $model->errors);
    }

    /*
     * 状态修改
     */
    public function actionDelandshow()
    {
        $input = Yii::$app->request->post();
        $model = new \common\models\HouseComment();
        $row = $model->FindById($input['id']);
        if (!empty($row)) {
            if ($row->load($input, '') && $row->save()) {
                return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
            }
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'edit_error'), $model->errors);
    }


    public function actionMorecomment()
    {
        $id = Yii::$app->request->get('hid');
        if (is_numeric($id)) {
            return $this->render('morecomment', ['hid' => $id]);
        }
        return $this->goBack();
    }

    public function actionMorecommentform()
    {
        $model = new HouseComment();
        $input = Yii::$app->request->post();
        $model->load($input, '');
        $rows = $model->MoregetList($input);
        if ($rows) {

            foreach ($rows as &$val) {
//                    $val['yes'] = HouseComment::find()->andWhere(['read'=>2])->andWhere(['hid'=>$val['hid']])->count();
//                    $val['no'] = HouseComment::find()->andWhere(['read'=>1])->andWhere(['hid'=>$val['hid']])->count();
                $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
                $val['mobile'] = preg_replace("/(\d{3})\d\d(\d{2})/", "\$1****\$3", $val['mobile']);
                if (isset(Yii::$app->params['houseCommentReadState'][$val['read']])) {
                    $val['read'] = Yii::$app->params['houseCommentReadState'][$val['read']];
                }
                if (isset($val['reply'])) {
                    $val['reply'] = 2;
                } else {
                    $val['reply'] = 1;
                }
            }
            return Help::JsonData(0, Yii::t('app', 'get_success'), $model->moreTotal(Yii::$app->request->post()), $rows);
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'get_error'));
    }

    //阅读点评的信息
    public function actionRead()
    {
        $model = new HouseComment();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if (!empty($row)) {
            if ($row['read'] == 1) {
                $Transaction = Yii::$app->db->beginTransaction();
                $row->read = 2;

                if ($row->save() && $this->RecordUser(['id' => $row['id'], 'type' => 1])) {
                    $Transaction->commit();
                } else {
                    $Transaction->rollBack();
                    return $this->goBack();
                }
            }
            if ($row['initiator'] == 2) {
                $row['mobile'] = $this->mobile($row['mobile']);
            }
            $replyModel = new HouseCommentreply();
            $replyData = $replyModel->FindById(['cid' => $row['id']]);

//                p($replyData);
            $row['demand'] = isset($model->xuqiu[$row['demand']]) ? $model->xuqiu[$row['demand']] : '';
            return $this->render('read', ['model' => $row, 'reply' => $replyData]);
        }
    }

    public function actionCreatereply()
    {
        $model = new \common\models\HouseCommentreply();
        $input = Yii::$app->request->post();
        $recordData = [];
        if (!empty($input['reply_id']) && is_numeric($input['reply_id'])) {
            $row = $model->FindById($input['reply_id']);

            if (is_object($row)) {
                if ($row->load($input, '') && $row->save()) {
                    $recordData['id'] = $input['reply_id'];
                    $recordData['type'] = 3;
                }
            }
        } else {

            if ($model->load($input, '') && $model->save()) {

                $recordData['id'] = $model->attributes['id'];
                $recordData['type'] = 2;
            }
        }

        if (!empty($recordData['id']) && !empty($recordData['type']) && $this->RecordUser($recordData)) {
            return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'edit_error'));

    }

    /*
     * 修改
     */
    public function actionEdit()
    {
        $model = new HouseComment();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if ($row) {
            $house = (new \common\models\House())->FindById($row['hid']);
            return $this->render('edit', ['model' => $row, 'label' => $model->xuqiu, 'house' => $house]);
        }
        return $this->goBack();
    }

    /*
     * 修改数据
     */
    public function actionEditform()
    {
        $input = Yii::$app->request->post();
        $model = new HouseComment();
        $row = $model->FindById($input['id']);
        if ($row) {
            if ($row->load($input, '') && $row->save()) {
                return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
            }
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'edit_error'), isset($row->errors) ? $row->errors : null);
    }

    private function RecordUser($arr)
    {
        $m = new \common\models\HouseCommentreadrecord();
        $m->cid = $arr['id'];
        $m->uid = Yii::$app->session['user_info']['uid'];
        $m->ip = Yii::$app->request->userIP;
        $m->type = $arr['type'];
        return $m->save();
    }

    public function mobile($phone)
    {
        return preg_replace("/(\d{3})\d\d(\d{2})/", "\$1****\$3", $phone);
    }
}