<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\PhoneDayT;
use common\models\PhoneServerT;
use common\models\PhoneTimeT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class PhoneServerController extends \yii\web\Controller
{
    public $enableCsrfValidation = false;
    public $my = null;

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    public function actionIndex()
    {
        $request = Yii::$app->request;
        $name = $request->get('name');

        $query = PhoneServerT::find();
        if($name != '') {
            $query = $query->andWhere(['like','name',$name]);
        }
        $items = $query->all();

        return $this->render('index',[
            'items' => $items,
            'name' => $name
        ]);
    }

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = PhoneServerT::findOne(['id'=>$id]);
        } else {
            $info = new PhoneServerT();
        }

        return $this->render('edit',[
            'info' => $info
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $id = $request->post('id',0);
            $name = $request->post('name');
            $ip = $request->post('ip');

            if($name == '') {
                $result['msg'] = '请输入公司名称!';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = PhoneServerT::findOne(['id'=>$id]);
            } else {
                $row = new PhoneServerT();
            }
            $row->name = $name;
            $row->ip = $ip;
            $row->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    public function actionDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $row = PhoneServerT::findOne(['id'=>$id]);
                $row->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

    public function actionPhoneBegin()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $caller = $request->post('caller');
        $phone_id = $request->post('phone_id',0);
        if($caller != '') {
            if($phone_id > 0) {
                $item = PhoneTimeT::findOne(['id'=>$phone_id]);
                $item->begin_time = time();
            } else {
                $item = new PhoneTimeT();
                $item->user_id = $this->my->id;
                $item->begin_time = 0;
            }
            $item->caller = $caller;
            $item->save();
            $session->set('phone_id',$item->id);
            $phone_id = $item->id;
        }
        $result = array();
        $result['success'] = true;
        $result['msg'] = '开始计时';
        $result['phone_id'] = $phone_id;
        return $result;
    }

    public function actionPhoneEnd()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $phone_id = $request->post('phone_id');
        if($phone_id != '') {
            $item = PhoneTimeT::findOne(['id'=>$phone_id]);
            if(isset($item)) {
                if($item->end_time == '') {
                    $item->end_time = time();
                    $item->call_time = $item->end_time - $item->begin_time;
                    $item->save();
                    if($item->begin_time == 0) {
                        $item->delete();
                    } else {
                        $day_info = PhoneDayT::find()
                            ->where('user_id='.$this->my->id.' and call_date="'.date('Y-m-d').'"')
                            ->one();
                        if(!$day_info) {
                            $day_info = new PhoneDayT();
                            $day_info->user_id = $this->my->id;
                            $day_info->call_date = date('Y-m-d');
                            $day_info->call_time = $item->call_time;
                            $day_info->save();

                            PhoneTimeT::deleteAll('user_id='.$this->my->id.' and created_at<="'.date('Y-m-d H:i:s').'"');
                        }
                    }
                }
            }
            $session->remove('phone_id');
        }
        $result = array();
        $result['success'] = true;
        $result['msg'] = '计时结束';
        return $result;
    }
}
