<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\ConfigT;
use common\models\OrderT;
use common\models\PhoneDayT;
use common\models\UserT;
use common\models\SysIpT;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Cookie;
use yii\web\Response;
use yii\web\User;

class SystemController extends \yii\web\Controller
{
    public $my = null;
    public $web = null;
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    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 {
            $this->my = null;
        }

        $this->web = ConfigT::findOne(['id'=>1]);
    }

    public function actionConfig()
    {
        return $this->render('config');
    }

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

        if($request->isPost) {
            $name = $request->post('name');
            $super_password = $request->post('super_password');
            $max_other_money = $request->post('max_other_money');
            $max_other_area_rate = $request->post('max_other_area_rate');
            $max_dis_money = $request->post('max_dis_money');
            $max_out_rate = $request->post('max_out_rate');

            $tran = UserT::getDb()->beginTransaction();

            try {
                $row = $this->web;
                $row->name = $name;
                if($super_password != '') {
                    $row->super_salt = MyLib::randomStr(4);
                    $row->super_password = MyLib::hashPwd($super_password,$row->super_salt);
                }
                $row->max_other_money = $max_other_money;
                $row->max_other_area_rate = $max_other_area_rate;
                $row->max_dis_money = $max_dis_money;
                $row->max_out_rate = $max_out_rate;
                $row->save();

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }

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

    /**
     * IP管理
     * IP动态管理功能
     * @param 参数
     * @return 返回类型
     * @author liukangle
     *
     */
    public function actionIp() {
        return $this->render('ip');
    }

    public function actionIpJson(){
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $ips = SysIpT::find()->orderBy('id ASC');
        $total = $ips->count();
        $items = $ips->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $data['rows'][] = $row;
        }

        return $data;
    }

    /**
     * 修改，添加页面
     * 修改，添加
     * @param id int
     * @return 返回类型
     * @author liukangle
     *
     */
    public function actionIpEdit()
    {

        $request = Yii::$app->request;
        $id = $request->get('id');
        if($id > 0) {
            $info = SysIpT::findOne(['id'=>$id]);
        } else {
            $info = new SysIpT();
        }
        return $this->render('ip-edit',[
            'info' => $info,
        ]);
    }

    /**
     * 修改保存添加
     * 功能
     * @param 参数
     * @return 返回类型
     * @author liukangle
     *
     */
    public function actionIpSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost){

            $data = $request->post();
            if($data['id'] > 0) {
                $row = SysIpT::findOne(['id'=>$data['id']]);
            } else {
                $row = new SysIpT();
            }
            $row->attributes = $data;
            if(!$row->validate()){
                $result['msg'] = current(current($row->errors)).'！';
                return $result;
            }
            $row->save();

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

        return $result;
    }

    /**
     * 删除
     * ip删除
     * @param 参数
     * @return 返回类型
     * @author liukangle
     *
     */
    public function actionIpDelete()
    {
        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');
            if($id > 0){
                $row = SysIpT::findOne(['id'=>$id]);
                if($row) {
                    $row->delete();
                }

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

            return $result;
        }
    }

    public function actionTest() {
        $items = OrderT::find()
            ->where('status_id=10')
            ->all();
        foreach($items as $item) {
            $car_info = $item->car;
            if(!$car_info) {
                echo 'error';
                break;
            }
            $car_info->op1_id = $item->user_id;
            if(!$car_info->save()) {
                print_r($car_info->getErrors());
                break;
            }
        }
        return count($items);
    }
}
