<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\AppointmentT;
use common\models\CarT;
use common\models\CarTypeT;
use common\models\CarUseT;
use common\models\InsurerTypeT;
use common\models\OrderT;
use common\models\PayTypeT;
use common\models\UserT;
use Yii;
use yii\data\Pagination;
use yii\web\Response;

class SalesMngController extends \yii\web\Controller
{
    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()
    {
        return $this->render('index');
    }

    public function actionMyCars()
    {
        $request = Yii::$app->request;

        $query = CarT::find()
            ->where('user_id='.$this->my->id)
            ->orderBy('id DESC');
        $total = $query->count();
        $pagination = new Pagination(['totalCount' => $total]);

        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();

        return $this->renderPartial('my-cars',[
            'items'=>$items,
            'pagination' => $pagination
        ]);
    }

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

        $car_use_items = CarUseT::find()->all();
        $car_type_items = CarTypeT::find()->all();

        return $this->renderPartial('my-cars-edit',[
            'info' => $info,
            'car_use_items' => $car_use_items,
            'car_type_items' => $car_type_items
        ]);
    }

    public function actionMyCarsSave()
    {
        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);
            $car_no = $request->post('car_no');
            $engine_no = $request->post('engine_no');
            $car_frame_no = $request->post('car_frame_no');
            $factory_model = $request->post('factory_model');
            $car_model = $request->post('car_model');
            $seats = $request->post('seats');
            $car_type_id = $request->post('car_type_id');
            $car_use_id = $request->post('car_use_id');
            $register_date = $request->post('register_date');
            $insurer1_date = $request->post('insurer1_date');
            $insurer2_date = $request->post('insurer2_date');
            $displacement = $request->post('displacement');
            $car_year = $request->post('car_year');
            $brand = $request->post('brand');
            $car_man = $request->post('car_man');
            $phone = $request->post('phone');
            $remark = $request->post('remark');

            if($car_no == '') {
                $result['msg'] = '请输入车牌号码!';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = CarT::findOne(['id'=>$id]);
            } else {
                $row = new CarT();
                $row->user_id = $this->my->id;
            }
            $row->car_no = $car_no;
            $row->engine_no = $engine_no;
            $row->car_frame_no = $car_frame_no;
            $row->factory_model = $factory_model;
            $row->car_model = $car_model;
            $row->seats = $seats;
            $row->car_type_id = $car_type_id;
            $row->car_use_id = $car_use_id;
            $row->register_date = $register_date;
            $row->insurer1_date = $insurer1_date;
            $row->insurer2_date = $insurer2_date;
            $row->displacement = $displacement;
            $row->car_year = $car_year;
            $row->brand = $brand;
            $row->car_man = $car_man;
            $row->phone = $phone;
            $row->remark = $remark;
            $row->save();

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

    public function actionSalesAppointment()
    {
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $car_info = CarT::findOne(['id'=>$car_id]);

        $a_query = $car_info->getAppointments()
            ->orderBy('pdate DESC,ptime ASC,id DESC');
        $total = $a_query->count();
        $pagination = new Pagination(['totalCount' => $total]);

        $a_query = $a_query->offset($pagination->offset)->limit($pagination->limit);
        $items = $a_query->all();

        return $this->renderPartial('sales-appointment',[
            'car_info'=>$car_info,
            'items'=>$items,
            'pagination'=>$pagination
        ]);
    }

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

        if($request->isPost) {
            $car_id = $request->post('car_id',0);
            $pdate = $request->post('pdate');
            $ptime = $request->post('ptime');
            $remark = $request->post('remark');

            if($remark == '') {
                $result['msg'] = '请输入备注!';
                return $result;
            }

            $row = new AppointmentT();

            $row->car_id = $car_id;
            $row->pdate = $pdate;
            $row->ptime = $ptime;
            $row->remark = $remark;
            $row->user_id = $this->my->id;
            $row->save();

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

    public function actionSalesOrdersEdit()
    {
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $id = $request->get('id',0);
        $car_info = CarT::findOne(['id'=>$car_id]);
        $info = OrderT::findOne(['id'=>$id]);
        if(empty($info)) {
            $info = new OrderT();
        }

        $pay_type_items = PayTypeT::find()->all();
        $insurer_type_items = InsurerTypeT::find()
            ->all();

        return $this->renderPartial('sales-orders-edit',[
            'info'=>$info,
            'car_info'=>$car_info,
            'pay_type_items'=>$pay_type_items,
            'insurer_type_items' => $insurer_type_items
        ]);
    }

    public function actionSalesOrdersSave()
    {
        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);
            $car_id = $request->post('car_id');
            $gift_other = $request->post('gift_other');
            $id_man = $request->post('id_man');
            $id_number = $request->post('id_number');
            $link_man = $request->post('link_man');
            $link_phone = $request->post('link_phone');
            $insurer1_begin_date = $request->post('insurer1_begin_date');
            $insurer2_begin_date = $request->post('insurer2_begin_date');
            $send_address = $request->post('send_address');
            $pay_type_id = $request->post('pay_type_id');
            $remark = $request->post('remark');

            $row = null;
            if($id > 0) {
                $row = OrderT::findOne(['id'=>$id]);
            } else {
                $row = new OrderT();
                $row->user_id = $this->my->id;
            }
            $row->car_id = $car_id;
            $row->gift_other = $gift_other;
            $row->id_man = $id_man;
            $row->id_number = $id_number;
            $row->link_man = $link_man;
            $row->link_phone = $link_phone;
            $row->insurer1_begin_date = $insurer1_begin_date;
            $row->insurer2_begin_date = $insurer2_begin_date;
            $row->send_address = $send_address;
            $row->pay_type_id = $pay_type_id;
            $row->status = 0;
            $row->remark = $remark;
            $row->save();

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

    public function actionSalesOrders()
    {
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $car_info = CarT::findOne(['id'=>$car_id]);

        $a_query = $car_info->getOrders()
            ->orderBy('id DESC');
        $total = $a_query->count();
        $pagination = new Pagination(['totalCount' => $total]);

        $a_query = $a_query->offset($pagination->offset)->limit($pagination->limit);
        $items = $a_query->all();

        return $this->renderPartial('sales-orders',[
            'car_info'=>$car_info,
            'items'=>$items,
            'pagination'=>$pagination
        ]);
    }
}
