<?php

namespace frontend\controllers;

use common\models\CaiwuT2;
use common\libs\MyLib;

use common\models\Scheme;
use common\models\NonAutoInsurance;

use common\models\OrderT;
use common\models\OrderUserT;
use common\models\OrderCaiwuT1;
use common\models\StatusT;

use common\models\NonLogT;
use Yii;
use yii\data\Pagination;
use yii\web\Response;

class NonAutoInsuranceController extends BaseController
{
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $insurant_name = $request->get('insurant_name');
        $page = $request->get('page',1);
        $user_id = $request->get('user_id');
        $status = $request->get('status');
        $query = NonAutoInsurance::find();

        if($insurant_name != '') {
            $query = $query->Where(['like','insurant_name',$insurant_name]);
        }
        if($status == 1) {
            $query = $query->andWhere('non_user_id is null');
        }
        if($status == 2) {
            $query = $query->andWhere('non_user_id>0');
        }
        $total = $query->count();
        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);
        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();
        $page_info = MyLib::getPageInfo($pagination);
        $query = $query->orderBy('id DESC');
        $items = $query->all();
        $user_items = $this->my->getChildren();
        return $this->render('index',[
            'items'=>$items,
            'user_items'=>$user_items,
            'user_id'=>$user_id,
            'status'=>$status,
            'page_info'=>$page_info,
            'page'=>$page,
            'insurant_name'=>$insurant_name,
        ]);
    }
    public function actionAssign()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = $request->post('ids',array());
            $user_id = $request->post('user_id',0);

            if($user_id == 0) {
                $result['msg'] = '请先选择分配给哪个业务员';
                return $result;
            }
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选被保险人';
                return $result;
            }
            $tran = NonAutoInsurance::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $n_info = NonAutoInsurance::findOne(['id'=>$id]);
                    if($n_info->non_user_id > 0) {
                        continue;
                    }
                    $n_info->non_user_id = $user_id;
                    $n_info->save();

                    //添加保单
                    $res = new OrderT();
                    $res->non_id = $id;
                    $res->user_id = $user_id;
                    $res->status_id =14;
                    $res->lock_id = $user_id;
                    $res->insurance_status = 2;
                    $res->id_man = $n_info->insurant_name;
                    $res->id_number = $n_info->insurant_number;
                    $res->save();

                    //添加保单操作员
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $user_id;
                    $order_user_info->order_id = $res->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    $order_user_info->save();

                    //添加保单
//                    $order_row = new OrderT();
//                    $order_row->car_id = $n_info->id;
//                    $order_row->car_no = $b_info->car->car_no;
//                    $order_row->engine_no = $b_info->car->engine_no;
//                    $order_row->car_frame_no = $b_info->car->car_frame_no;
//                    $order_row->car_man = $b_info->car->car_man;
//                    $order_row->user_id = $user_id;
//                    $order_row->status_id = 1;
//                    $order_row->id_man = $b_info->car->car_man;
//                    $order_row->link_man = $b_info->car->car_man;
//                    $order_row->link_phone = $b_info->car->phone;
//                    $order_row->save();
//
//                    //添加保单操作员
//                    $order_user_info = new OrderUserT();
//                    $order_user_info->user_id = $user_id;
//                    $order_user_info->order_id = $order_row->id;
//                    $order_user_info->type_id = 1;
//                    $order_user_info->status = 0;
//                    $order_user_info->save();

                    $this->addNonLog($id,'从非车险库分配数据给业务员:'.$n_info->user->getShowName(),1);
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionReturn()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        if($request->isPost) {
            $ids = $request->post('ids',array());
            if(count($ids) == 0) {
                $result['msg'] = '请先钩选需要被保险人';
                return $result;
            }
            $tran = NonAutoInsurance::getDb()->beginTransaction();
            try {
                foreach($ids as $id) {
                    $n_info = NonAutoInsurance::findOne(['id'=>$id]);
//                    $this->addLog($n_info->id,'从B库回收数据到A库',1);

                    //删除保单 还没有正本生成的保单可以直接删除,正本生成后,需要退单操作
                    //删除操作人员
                    $orders = OrderT::find()
                        ->where('non_id='.$n_info->id.' and status_id<15')
                        ->andWhere('insurance_status=2')
                        ->all();
                    foreach($orders as $order_info) {
                        OrderUserT::deleteAll('order_id='.$order_info->id);
                        //删除保单
                        $order_info->delete();
                    }


                    $n_info->non_user_id = 0;
                    $n_info->save();
                    $this->addNonLog($id,'从非车险库回收数据',1);
//                    $b_info->delete();
                }
                $result['success'] = true;
                $result['msg'] = '操作成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }
    public function actionAjaxHistoryIndex()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $non_id = $request->get('non_id');
        $page = $request->get('page',1);

        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';

        $query = NonLogT::find()
            ->where(['non_id'=>$non_id,'type'=>1])
            ->orderBy('op_time DESC');

        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);

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

        $page_info = MyLib::getAjaxPageInfo($pagination,'car_history_list');
        $html = $this->renderPartial('/car/ajax-history-index',[
            'car_id' => $non_id,
            'items' => $items,
            'page' => $page,
            'page_info' => $page_info
        ]);
        $result['success'] = true;
        $result['msg'] = '读取成功';
        $result['html'] = $html;
        return $result;
    }
    //非车险库订单详情
//    public function actionInfoList()
//    {
//        $request = Yii::$app->request;
//        $id = $request->get('id',0);
//        $back_params = $request->get('back_params');
//        if($id > 0) {
//            $info = NonAutoInsurance::findOne(['id'=>$id]);
//        } else {
//            $info = new NonAutoInsurance();
//        }
//        $scheme_items=Scheme::find()->all();
//
//        return $this->render('info',[
//            'scheme_items' => $scheme_items,
//            'back_params' => $back_params,
//            'info' => $info,
//        ]);
//    }
    public function actionAjaxNonIndex()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $page = 1;
        $insurant_name = $request->post('in_name');
        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';
        $query = NonAutoInsurance::find();

        $query = $query->andWhere('insurant_name = "'.$insurant_name.'"');

        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);
        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();
//        dd($items);
        $page_info = MyLib::getAjaxPageInfo($pagination,'non_list');
        $html = $this->renderPartial('ajax-non-index',[
//            'car_id' => $non_id,
            'items' => $items,
            'page' => $page,
            'page_info' => $page_info
        ]);
        if($items){
            $result['success'] = true;
            $result['msg'] = '读取成功';
            $result['html'] = $html;
        }

        return $result;

    }
    public function actionAjaxBdInfo()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $scheme_items=Scheme::find()->all();
        //先查询id如果没有新增
        $id = $request->get('id');
        $ordid = $request->get('ordid');
        $fixId = $request->get('fixId',0);
        $info=null;
        $order_info=null;

        if($id && $ordid){

            $info = NonAutoInsurance::findOne(['id'=>$id]);

            if($info){
//                $order_info = OrderT::find()->where(['id'=>$ordid])->one();
//                dd($order_info);
                $order_info = OrderT::findOne(['id'=>$ordid]);;
            }

        }

        $html = $this->renderPartial('ajax-bd-info',[
            'scheme_items' => $scheme_items,
            'fixId' => $fixId,
            'order_info' => $order_info?$order_info:'',
            'info' => $info?$info:'',
        ]);

        $result['success'] = true;
        $result['msg'] = '读取成功';
        $result['html'] = $html;


        return $result;
    }
    //非车险保单信息增加保存
    public function actionAjaxBdSave()
    {
        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');
            $order_id= $request->post('order_id');
            $fix_id= $request->post('fixId');
            $status_id= $request->post('status_id');
            $scheme_id= $request->post('scheme_id');
            $insurer_date_start = $request->post('insurer_date_start');
            $num = $request->post('num');
            $seat = $request->post('seat');
            $insurant_name = $request->post('insurant_name');
            $insurant_number = $request->post('insurant_number');
            $insurant_phone = $request->post('insurant_phone');
            $insurant_phone1 = $request->post('insurant_phone1');
            $policy_holder_name = $request->post('policy_holder_name');
            $policy_holder_number = $request->post('policy_holder_number');
            $policy_holder_phone = $request->post('policy_holder_phone');
            $policy_holder_phone1 = $request->post('policy_holder_phone1');
            $beneficiary_name = $request->post('beneficiary_name');
            $beneficiary_number = $request->post('beneficiary_number');
            $beneficiary_phone = $request->post('beneficiary_phone');
            $beneficiary_phone1 = $request->post('beneficiary_phone1');
//            dd($insurant_phone);
//            if($user_id == 0) {
//                $result['msg'] = '请先选择分配给哪个业务员';
//                return $result;
//            }

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

            try {
//                $model = NonAutoInsurance::findOne(['id'=>$id]);
                //车险保单关联非车险保单,保存非车险保单号
                $fixOrder= OrderT::findOne(['id'=>$fix_id]);
                $query = NonAutoInsurance::find();
                $query->andWhere(['id'=>$id]);
//                if($id!=''){
//
//                }
//                aa($query);
                $model=$query->one();
//                dd($model);
                if($fixOrder->user_id){
                    $uid = $fixOrder->user_id;
                }else{
                    $uid = $this->my->id;
                }

                //修改非车险库
//                dd($model);
                if(!$model){
                    $model=NonAutoInsurance::find()->where(['insurant_name'=>$insurant_name])->andWhere(['insurant_number'=>$insurant_number])->one();
                    if(!$model){
                        $model= new NonAutoInsurance();
                    }

//                dd($insurant_phone);
                }
                $model->scheme_id=$scheme_id;
                $model->insurer_date_start=$insurer_date_start;
                $model->num=$num;
                $model->seat=$seat;
                $model->insurant_name=$insurant_name;
                $model->insurant_number=$insurant_number;
                $model->insurant_phone=$insurant_phone;
                $model->insurant_phone1=$insurant_phone1;
                $model->policy_holder_name=$policy_holder_name;
                $model->policy_holder_number=$policy_holder_number;
                $model->policy_holder_phone=$policy_holder_phone;
                $model->policy_holder_phone1=$policy_holder_phone1;
                $model->beneficiary_name=$beneficiary_name;
                $model->beneficiary_number=$beneficiary_number;
                $model->beneficiary_phone=$beneficiary_phone;
                $model->beneficiary_phone1=$beneficiary_phone1;
                $model->non_user_id=$uid;
                $model->save();
//                $model->save();
//                dd($model->id);

                //查询保单
//                $res=OrderT::find()->where('user_id ='.$uid)
//                    ->andWhere('id_man="'.$model->insurant_name.'"')
//                    ->one();

                $res=OrderT::findOne(['id'=>$order_id]);
//                dd($res);
                if(!$res){
                    $res = new OrderT();
                    $res->user_id = $uid;
                    $res->status_id =$status_id;
                    $res->lock_id = $uid;
                    $this->addNonLog($model->id, '创建非车险订单', 1);
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $uid;
                    $order_user_info->order_id = $res->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    $order_user_info->save();
//                    dd(22);
                }
//                dd($res);
                if($res){
//                dd(55);
                    if($res->lock_id != $this->my->id && $res->lock_id!=0) {
                        $result['msg'] = '该记录已经被别人锁定';
                        return $result;
                    }

//                    if($res->status_id >=15){
//                        $result['msg'] = '该记录已经在核保或已完成不能修改';
//                        return $result;
//                    }
                }
//                die;
//                $model->save();

//
//                if(!$res){
//                    //添加保单
////                    $res = new OrderT();
////                    $res->user_id = $uid;
////                    $res->status_id =$status_id;
////                    $res->lock_id = $uid;
//
//                    //添加保单操作员
////                    $order_user_info = new OrderUserT();
////                    $order_user_info->user_id = $uid;
////                    $order_user_info->order_id = $res->id;
////                    $order_user_info->type_id = 1;
////                    $order_user_info->status = 0;
////                    $order_user_info->save();
//                }
//                //提交
                if($status_id>14 && $res){
                    $res->return_status_id = 0;
                    $res->return_remark = '';
                    $res->return_time = 0;
                    $res->status_id=15;
                    $res->lock_id=0;
                    $res->submit_date = date('Y-m-d');

                }

                $insurance_status=2;
                $res->insurance_status = $insurance_status;
                $res->insurer_date_start = $insurer_date_start;
                $res->id_man = $insurant_name;
                $res->id_number = $insurant_number;
                $res->ord_scheme_id = $scheme_id;
                $res->non_num = $num;
                $res->non_id =$model->id;
                $res->car_no =$fixOrder->car_no;
                $fbaofei=0;
                if($res->ord_scheme_id){
                    $scheme_items=Scheme::findOne(['id'=>$res->ord_scheme_id]);
                    $fbaofei=$scheme_items->baofei;
                }
//
                $res->non_baofei = sprintf("%.2f",$fbaofei);
                $res->save();

                //车险保单保存
                $fixOrder->relevancy_non_id=$res->id;
                $fixOrder->non_id=$model->id;
                $fixOrder->insurance_status=3;


                $fixOrder->save();

                $order_user_info = OrderUserT::findOne(['user_id' => $this->my->id, 'order_id' => $res->id, 'status' => 0]);
                if ($order_user_info) {
                    $order_user_info->status = 1;
                    $order_user_info->save();
                }
//
                if($status_id>14 && $res){
                    $tmp_status_info = StatusT::findOne(['id' => $status_id]);
                    $log_msg = '';
                    if ($tmp_status_info) {
                        $log_msg = '进入' . $tmp_status_info->name;
                    }
                    $this->addNonLog($res->non_id, $log_msg, 1);

                }


//                dd($model->save());

                $result['success'] = true;
                $result['id'] = $model->id;
                $result['ordid'] = $res->id;
                $result['msg'] = '保存成功';
                $tran->commit();


            }catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }

        return $result;
    }
    //非车险库详情
    public function actionInfo()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $back_params = $request->get('back_params');
        if($id > 0) {
            $info = NonAutoInsurance::findOne(['id'=>$id]);
        } else {
            $info = new NonAutoInsurance();
        }
        $scheme_items=Scheme::find()->all();

        return $this->render('info',[
        'scheme_items' => $scheme_items,
        'back_params' => $back_params,
        'info' => $info,
        ]);
    }
    //非车险首拨列表
    public function actionBdIndex()
    {
        $request = Yii::$app->request;
        $insurant_name = $request->get('insurant_name');
        $page = $request->get('page',1);

//        $query = NonAutoInsurance::find();
        $query = OrderT::find()
            ->Where('status_id =14')
            ->andWhere('insurance_status=2')
            ->andWhere('return_status_id=0');
        if($insurant_name != '') {
            $query = $query->andWhere('id_man like "'.$insurant_name.'"');
        }
        $total = $query->count();
        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);
        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();
//        dd($total);
        $page_info = MyLib::getPageInfo($pagination);
        $query = $query->orderBy('id DESC');
        $items = $query->all();


        return $this->render('bd-index',[
            'items'=>$items,
            'page_info'=>$page_info,
            'page'=>$page,
            'insurant_name'=>$insurant_name,
        ]);
    }
//    public function actionBdIndex()
//    {
//        $request = Yii::$app->request;
//        $insurant_name = $request->get('insurant_name');
//        $page = $request->get('page',1);
//
//        $query = OrderT::find()
//            ->Where('status_id =14')
//            ->andWhere('insurance_status=2');
////        $query = $query->Where(['like','insurant_name',$insurant_name]);
//        if($insurant_name != '') {
//            $query = $query->andWhere(['like','id_man',$insurant_name]);
//        }
//        $total = $query->count();
//        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
//        $pagination->setPage($page-1);
//        $query = $query->offset($pagination->offset)->limit($pagination->limit);
//        $items = $query->all();
////        dd($items);
//        $page_info = MyLib::getPageInfo($pagination);
//        $query = $query->orderBy('id DESC');
//        $items = $query->all();
//        return $this->render('bd-index',[
//            'items'=>$items,
//            'page_info'=>$page_info,
//            'page'=>$page,
//            'insurant_name'=>$insurant_name,
//        ]);
//    }
    //搜索后添加
    public function actionMySearch() {
        return $this->render('my-search');
    }
    public function actionSearchNon()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $insurant_name = $request->post('in_name');
        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';
        $query = NonAutoInsurance::find();

        $query = $query->andWhere('insurant_name = "'.$insurant_name.'"');

        $items = $query->one();

        if($items){
          $result['success'] = true;
          $result['result'] = $items;
          $result['msg'] = '读取成功';
        }


        return $result;
    }
    public function actionMySearchResult()
    {
        $request = Yii::$app->request;
        $insurant_name = $request->get('insurant_name');

        $query = NonAutoInsurance::find();
        if($insurant_name != '') {
            $query = $query->andWhere('insurant_name like "'.$insurant_name.'"');
        }
        $items = $query->all();

        return $this->render('my-search-result',[
            'items' => $items
        ]);
    }
    //新增非车险详情
    public function actionAddBdInfo()
    {
        $scheme_items=Scheme::find()->all();
        //先查询id如果没有新增
        $request = Yii::$app->request;
        $id = $request->get('id');
        $ordid = $request->get('ordid');
        $info=null;
        $order_info=null;
        if($id && $ordid){
//            $info=new NonAutoInsurance();
            $info = NonAutoInsurance::findOne(['id'=>$id]);
//            dd($info);
            if($info){
                $order_info = OrderT::findOne(['id'=>$ordid]);
//                $order_info = $info->getOrders()->orderBy('id DESC')->one();
//                dd($order_info);
            }

        }
//        dd($info);

//        if(!$order_info) {
//            $user_id = 0;
//            $order_info = new OrderT();
//            $order_info->insurance_status = 2;
//            $order_info->non_id = $id;
//            $order_info->user_id = $user_id;
//            $order_info->status_id =14;
//            $order_info->lock_id = $user_id;
//            $order_info->id_man = $info->insurant_name;
//            $order_info->id_number = $info->insurant_number;
//            $order_info->save();
//        }

        return $this->render('add-bd-info',[
            'scheme_items' => $scheme_items,
            'order_info' => $order_info?$order_info:'',
//            'back_params' => $back_params,
            'info' => $info?$info:'',
        ]);
    }
    //非车险保单信息增加保存
    public function actionAddBdSave()
    {
        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');
            $order_id= $request->post('order_id');
            $status_id= $request->post('status_id');
            $scheme_id= $request->post('scheme_id');
            $insurer_date_start = $request->post('insurer_date_start');
            $num = $request->post('num');
            $seat = $request->post('seat');
            $insurant_name = $request->post('insurant_name');
            $insurant_number = $request->post('insurant_number');
            $insurant_phone = $request->post('insurant_phone');
            $insurant_phone1 = $request->post('insurant_phone1');
            $policy_holder_name = $request->post('policy_holder_name');
            $policy_holder_number = $request->post('policy_holder_number');
            $policy_holder_phone = $request->post('policy_holder_phone');
            $policy_holder_phone1 = $request->post('policy_holder_phone1');
            $beneficiary_name = $request->post('beneficiary_name');
            $beneficiary_number = $request->post('beneficiary_number');
            $beneficiary_phone = $request->post('beneficiary_phone');
            $beneficiary_phone1 = $request->post('beneficiary_phone1');
//            dd($insurant_phone);
//            if($user_id == 0) {
//                $result['msg'] = '请先选择分配给哪个业务员';
//                return $result;
//            }

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

            try {
//                $model = NonAutoInsurance::findOne(['id'=>$id]);
                $query = NonAutoInsurance::find();
                $query->andWhere(['id'=>$id]);
//                if($id!=''){
//
//                }
//                aa($query);
                $model=$query->one();
//                dd($model);
                $uid = $this->my->id;
                //修改非车险库
//                dd($model);
                if(!$model){
                    $model=NonAutoInsurance::find()->where(['insurant_name'=>$insurant_name])->andWhere(['insurant_number'=>$insurant_number])->one();
                    if(!$model){
                        $model= new NonAutoInsurance();
                    }

//                dd($insurant_phone);
                }
                $model->scheme_id=$scheme_id;
                $model->insurer_date_start=$insurer_date_start;
                $model->num=$num;
                $model->seat=$seat;
                $model->insurant_name=$insurant_name;
                $model->insurant_number=$insurant_number;
                $model->insurant_phone=$insurant_phone;
                $model->insurant_phone1=$insurant_phone1;
                $model->policy_holder_name=$policy_holder_name;
                $model->policy_holder_number=$policy_holder_number;
                $model->policy_holder_phone=$policy_holder_phone;
                $model->policy_holder_phone1=$policy_holder_phone1;
                $model->beneficiary_name=$beneficiary_name;
                $model->beneficiary_number=$beneficiary_number;
                $model->beneficiary_phone=$beneficiary_phone;
                $model->beneficiary_phone1=$beneficiary_phone1;
                $model->non_user_id=$uid;
                $model->save();
//                $model->save();
//                dd($model->id);

                //查询保单
//                $res=OrderT::find()->where('user_id ='.$uid)
//                    ->andWhere('id_man="'.$model->insurant_name.'"')
//                    ->one();
                $res=OrderT::findOne(['id'=>$order_id]);
                if(!$res){
                    $res = new OrderT();
                    $res->user_id = $uid;
                    $res->status_id =$status_id;
                    $res->lock_id = $uid;
                    $this->addNonLog($model->id, '创建新的订单', 1);
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $uid;
                    $order_user_info->order_id = $res->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    $order_user_info->save();
                }
                if($res){

                    if($res->lock_id != $this->my->id && $res->lock_id!=0) {
                        $result['msg'] = '该记录已经被别人锁定';
                        return $result;
                    }
//                    if($res->status_id >=15){
//                        $result['msg'] = '该记录已经在核保或已完成不能修改';
//                        return $result;
//                    }
                }

//                $model->save();

//
//                if(!$res){
//                    //添加保单
////                    $res = new OrderT();
////                    $res->user_id = $uid;
////                    $res->status_id =$status_id;
////                    $res->lock_id = $uid;
//
//                    //添加保单操作员
////                    $order_user_info = new OrderUserT();
////                    $order_user_info->user_id = $uid;
////                    $order_user_info->order_id = $res->id;
////                    $order_user_info->type_id = 1;
////                    $order_user_info->status = 0;
////                    $order_user_info->save();
//                }
//                //提交
                if($status_id>14 && $res){
                    $res->return_status_id = 0;
                    $res->return_remark = '';
                    $res->return_time = 0;
                    $res->status_id=15;
                    $res->lock_id=0;
                    $res->submit_date = date('Y-m-d');


                }
                $insurance_status=2;
                $res->insurance_status = $insurance_status;
                $res->insurer_date_start = $insurer_date_start;
                $res->id_man = $insurant_name;
                $res->id_number = $insurant_number;
                $res->ord_scheme_id = $scheme_id;
                $res->non_num = $num;
                $res->non_id =$model->id;
                $fbaofei=0;
                if($res->ord_scheme_id){
                    $scheme_items=Scheme::findOne(['id'=>$res->ord_scheme_id]);
                    $fbaofei=$scheme_items->baofei;
                }
//
                $res->non_baofei = sprintf("%.2f",$fbaofei);
                $res->save();

                $order_user_info = OrderUserT::findOne(['user_id' => $this->my->id, 'order_id' => $res->id, 'status' => 0]);
                if ($order_user_info) {
                    $order_user_info->status = 1;
                    $order_user_info->save();
                }
//
                if($status_id>14 && $res){
                    $tmp_status_info = StatusT::findOne(['id' => $status_id]);
                    $log_msg = '';
                    if ($tmp_status_info) {
                        $log_msg = '进入' . $tmp_status_info->name;
                    }
                    $this->addNonLog($res->non_id, $log_msg, 1);

                }


//                dd($model->save());

                $result['success'] = true;
                $result['id'] = $model->id;
                $result['ordid'] = $res->id;
                $result['msg'] = '保存成功';
                $tran->commit();


            }catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }

        return $result;
    }
    //非车险保单信息保存
    public function actionBdSave()
    {
        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');
            $order_id= $request->post('order_id');
            $status_id= $request->post('status_id');
            $scheme_id= $request->post('scheme_id');
            $insurer_date_start = $request->post('insurer_date_start');
            $num = $request->post('num');
            $seat = $request->post('seat');
            $insurant_name = $request->post('insurant_name');
            $insurant_number = $request->post('insurant_number');
            $insurant_phone = $request->post('insurant_phone');
            $policy_holder_name = $request->post('policy_holder_name');
            $policy_holder_number = $request->post('policy_holder_number');
            $policy_holder_phone = $request->post('policy_holder_phone');
            $beneficiary_name = $request->post('beneficiary_name');
            $beneficiary_number = $request->post('beneficiary_number');
            $beneficiary_phone = $request->post('beneficiary_phone');
//            dd($insurant_phone);
//            if($user_id == 0) {
//                $result['msg'] = '请先选择分配给哪个业务员';
//                return $result;
//            }

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

            try {
//                $model = NonAutoInsurance::findOne(['id'=>$id]);
                $query = NonAutoInsurance::find();
                if($id!=''){
                    $query->andWhere(['id'=>$id]);
                }
//                if($insurant_name!=''){
//                    $query->andWhere(['insurant_name'=>$insurant_name]);
//                }
//                aa($query);
                $model=$query->one();
//                dd($model);
                $uid = $this->my->id;
                //修改非车险库
                if(!$model){
                    $model= new NonAutoInsurance();
//                dd($insurant_phone);
                }
                $model->scheme_id=$scheme_id;
                $model->insurer_date_start=$insurer_date_start;
                $model->num=$num;
                $model->seat=$seat;
                $model->insurant_name=$insurant_name;
                $model->insurant_number=$insurant_number;
                $model->insurant_phone=$insurant_phone;
                $model->policy_holder_name=$policy_holder_name;
                $model->policy_holder_number=$policy_holder_number;
                $model->policy_holder_phone=$policy_holder_phone;
                $model->beneficiary_name=$beneficiary_name;
                $model->beneficiary_number=$beneficiary_number;
                $model->beneficiary_phone=$beneficiary_phone;
                $model->non_user_id=$uid;
                $model->save();
//                $model->save();
//                dd($model->id);

                //查询保单
//                $res=OrderT::find()->where('user_id ='.$uid)
//                    ->andWhere('id_man="'.$model->insurant_name.'"')
//                    ->one();
                $res=OrderT::findOne(['id'=>$order_id]);
                if(!$res){
                    $res = new OrderT();
                    $res->user_id = $uid;
                    $res->status_id =$status_id;
                    $res->lock_id = $uid;
                    $this->addNonLog($model->id, '创建新的订单', 1);
                    $order_user_info = new OrderUserT();
                    $order_user_info->user_id = $uid;
                    $order_user_info->order_id = $res->id;
                    $order_user_info->type_id = 1;
                    $order_user_info->status = 0;
                    $order_user_info->save();
                }
                if($res){

                    if($res->lock_id != $this->my->id && $res->lock_id!=0) {
                        $result['msg'] = '该记录已经被别人锁定';
                        return $result;
                    }
//                    if($res->status_id >=15){
//                        $result['msg'] = '该记录已经在核保或已完成不能修改';
//                        return $result;
//                    }
                }

//                $model->save();

//
//                if(!$res){
//                    //添加保单
////                    $res = new OrderT();
////                    $res->user_id = $uid;
////                    $res->status_id =$status_id;
////                    $res->lock_id = $uid;
//
//                    //添加保单操作员
////                    $order_user_info = new OrderUserT();
////                    $order_user_info->user_id = $uid;
////                    $order_user_info->order_id = $res->id;
////                    $order_user_info->type_id = 1;
////                    $order_user_info->status = 0;
////                    $order_user_info->save();
//                }
//                //提交
                if($status_id>14 && $res){
                    $res->status_id=15;
                    $res->lock_id=0;
                    $res->submit_date = date('Y-m-d');


                }
                $insurance_status=2;
                $res->insurance_status = $insurance_status;
                $res->insurer_date_start = $insurer_date_start;
                $res->id_man = $insurant_name;
                $res->id_number = $insurant_number;
                $res->ord_scheme_id = $scheme_id;
                $res->non_num = $num;
                $res->non_id =$model->id;
                $fbaofei=0;
                if($res->ord_scheme_id){
                    $scheme_items=Scheme::findOne(['id'=>$res->ord_scheme_id]);
                    $fbaofei=$scheme_items->baofei;
                }
//
                $res->non_baofei = sprintf("%.2f",$fbaofei);
                $res->save();

                $order_user_info = OrderUserT::findOne(['user_id' => $this->my->id, 'order_id' => $res->id, 'status' => 0]);
                if ($order_user_info) {
                    $order_user_info->status = 1;
                    $order_user_info->save();
                }
//
                if($status_id>14 && $res){
                    $tmp_status_info = StatusT::findOne(['id' => $status_id]);
                    $log_msg = '';
                    if ($tmp_status_info) {
                        $log_msg = '进入' . $tmp_status_info->name;
                    }
                    $this->addNonLog($res->non_id, $log_msg, 1);

                }


//                dd($model->save());

                $result['success'] = true;
                $result['id'] = $model->id;
                $result['ordid'] = $res->id;
                $result['msg'] = '保存成功';
                $tran->commit();


            }catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }

        return $result;
    }
   
    //非车险库订单详情
    public function actionBdInfo()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
//        $back_params = $request->get('back_params');
        $info = NonAutoInsurance::findOne(['id'=>$id]);

        $order_info = $info->getOrders()->orderBy('id DESC')->one();
        if(!$order_info) {
            $user_id = 0;
            $order_info = new OrderT();
            $order_info->insurance_status = 2;
            $order_info->non_id = $id;
            $order_info->user_id = $user_id;
            $order_info->status_id =14;
            $order_info->lock_id = $user_id;
            $order_info->id_man = $info->insurant_name;
            $order_info->id_number = $info->insurant_number;
            $order_info->save();
        }

        //生成新的订单
        $scheme_items=Scheme::find()->all();

        return $this->render('bd-info',[
            'scheme_items' => $scheme_items,
            'order_info' => $order_info,
//            'back_params' => $back_params,
            '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){

            if($request->post('id') > 0) {
                $model = NonAutoInsurance::findOne(['id'=>$request->post('id')]);
            } else {
                $model = new NonAutoInsurance();
            }
//           $res= NonAutoInsurance::find()->where('insurant_name="'.$request->post('insurant_name').'"')
//               ->andWhere('insurant_number="'.$request->post('insurant_number').'"')
//               ->one();
//            if($res){
//                $result['msg'] = '已有被保险人,请前往非车险库修改';
//                return $result;
//            }
            $model->insurant_name = $request->post('insurant_name');
            $model->insurant_number= $request->post('insurant_number');
            $model->insurant_phone = $request->post('insurant_phone');
            $model->policy_holder_name = $request->post('policy_holder_name');
            $model->policy_holder_number= $request->post('policy_holder_number');
            $model->policy_holder_phone = $request->post('policy_holder_phone');
            $model->beneficiary_name = $request->post('beneficiary_name');
            $model->beneficiary_number= $request->post('beneficiary_number');
            $model->beneficiary_phone = $request->post('beneficiary_phone');
            if($model->save()){
                $result['success'] = true;
                $result['msg'] = '保存成功';
            }

//            $company = $request->post('company');
//
//            $order = CarT::findOne(['id'=>$car_id]);
//          ;



            //操作日志
//            $this->addLog($car_id,'操作了商业止保，交强止保，保险公司',1);


        }
        return $result;
    }
    //非车险保单保存
    public function actionBdSavesk()
    {
        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');
            $order_id= $request->post('order_id');
            $status_id= $request->post('status_id');
            $scheme_id= $request->post('scheme_id');
            $insurer_date_start = $request->post('insurer_date_start');
            $num = $request->post('num');
            $seat = $request->post('seat');
            $insurant_name = $request->post('insurant_name');
            $insurant_number = $request->post('insurant_number');

//            if($user_id == 0) {
//                $result['msg'] = '请先选择分配给哪个业务员';
//                return $result;
//            }

            $tran = NonAutoInsurance::getDb()->beginTransaction();
            $model = NonAutoInsurance::findOne(['id'=>$id]);
            try {
               $uid = $this->my->id;
                //修改非车险库
                $model->scheme_id=$scheme_id;
                $model->insurer_date_start=$insurer_date_start;
                $model->num=$num;
                $model->seat=$seat;
                $model->insurant_name=$insurant_name;
                $model->insurant_number=$insurant_number;
                $model->save();

                //查询保单
//                $res=OrderT::find()->where('user_id ='.$uid)
//                    ->andWhere('id_man="'.$model->insurant_name.'"')
//                    ->one();
                $res=OrderT::findOne(['id'=>$order_id]);
                if($res){

                    if($res->lock_id != $this->my->id && $res->lock_id!=0) {
                        $result['msg'] = '该记录已经被别人锁定';
                        return $result;
                    }
//                    if($res->status_id >=15){
//                        $result['msg'] = '该记录已经在核保或已完成不能修改';
//                        return $result;
//                    }
                }

                if(!$res){
                    //添加保单
//                    $res = new OrderT();
//                    $res->user_id = $uid;
//                    $res->status_id =$status_id;
//                    $res->lock_id = $uid;

                    //添加保单操作员
//                    $order_user_info = new OrderUserT();
//                    $order_user_info->user_id = $uid;
//                    $order_user_info->order_id = $res->id;
//                    $order_user_info->type_id = 1;
//                    $order_user_info->status = 0;
//                    $order_user_info->save();
                }
                //提交
                if($status_id>14 && $res){
                    $res->status_id=15;
                    $res->lock_id=0;
                    $res->submit_date = date('Y-m-d');

//                    $model->non_user_id=$uid;
//                    $model->save();

                }

//                $res->insurance_status = 2;
                $res->insurer_date_start = $insurer_date_start;
                $res->id_man = $insurant_name;
                $res->id_number = $insurant_number;
                $res->ord_scheme_id = $scheme_id;
                $res->non_num = $num;
//                $res->non_id = $id;
                $fbaofei=0;
                if($res->ord_scheme_id){
                    $scheme_items=Scheme::findOne(['id'=>$res->ord_scheme_id]);
                    $fbaofei=$scheme_items->baofei;
                }

                $res->non_baofei = sprintf("%.2f",$fbaofei);
                $res->save();
                $order_user_info = OrderUserT::findOne(['user_id' => $this->my->id, 'order_id' => $res->id, 'status' => 0]);
                if ($order_user_info) {
                    $order_user_info->status = 1;
                    $order_user_info->save();
                }

                if($status_id>14 && $res){
                    $tmp_status_info = StatusT::findOne(['id' => $status_id]);
                    $log_msg = '';
                    if ($tmp_status_info) {
                        $log_msg = '进入' . $tmp_status_info->name;
                    }
                    $this->addNonLog($res->non_id, $log_msg, 1);

                }


                $result['success'] = true;
                $result['msg'] = '保存成功';
                $tran->commit();
            }catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }

        return $result;
    }

    //非车险核保列表
    public function actionHbIndex()
    {
        $request = Yii::$app->request;
        $insurant_name = $request->get('insurant_name');
//        dd($insurant_name);
        $page = $request->get('page',1);

        $query = OrderT::find()
            ->Where('status_id =15')
            ->andWhere('insurance_status=2');
//        $query = $query->Where(['like','insurant_name',$insurant_name]);
        if($insurant_name != '') {
            $query = $query->andWhere(['like','id_man',$insurant_name]);
        }
        $total = $query->count();
        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);
        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();
//        dd($items);
        $page_info = MyLib::getPageInfo($pagination);
        $query = $query->orderBy('id DESC');
        $items = $query->all();

        return $this->render('hb-index',[
            'items'=>$items,
            'page_info'=>$page_info,
            'page'=>$page,
            'insurant_name'=>$insurant_name,
        ]);
    }
    //非车险库订单详情
    public function actionHbInfo()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $oid = $request->get('oid');
//        $back_params = $request->get('back_params');
        $info = NonAutoInsurance::findOne(['id'=>$id]);

        //生成新的订单
        $scheme_items=Scheme::find()->all();

        $order_info = OrderT::findOne(['id'=>$oid]);

//
        if($order_info->lock_id == 0) {
            $tran = OrderT::getDb()->beginTransaction();
            try {
                $order_info->lock_id = $this->my->id;
                $order_info->save();
                $order_user_info = new OrderUserT();
                $order_user_info->user_id = $this->my->id;
                $order_user_info->order_id = $order_info->id;
                $order_user_info->type_id = $order_info->status_id;
                $order_user_info->status = 0;
                $order_user_info->save();
                $tran->commit();

            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        } else {
            if($order_info->lock_id != $this->my->id) {
                echo '该记录已经被别人锁定,【<a href="/non-auto-insurance/hb-index">返回</a>】';
                exit;
            }
        }

        return $this->render('hb-info',[
            'scheme_items' => $scheme_items,
//            'back_params' => $back_params,
            'info' => $info,
            'order_info' => $order_info,
        ]);
    }
    //非车险保单详情
    public function actionBdInfoSearch()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $oid = $request->get('oid');
//        $back_params = $request->get('back_params');
        $info = NonAutoInsurance::findOne(['id'=>$id]);

        //生成新的订单
                $scheme_items=Scheme::find()->all();

        $order_info = OrderT::findOne(['id'=>$oid]);

//

        return $this->render('bd-info-search',[
            'scheme_items' => $scheme_items,
//            'back_params' => $back_params,
            'info' => $info,
            'order_info' => $order_info,
        ]);
    }
    // 非车险核保保存
    public function actionHbSave()
    {
        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');
            $status_id= $request->post('status_id');
            $insurer_date_end =$request->post('insurer_date_end');
            $insurer_non_no = $request->post('insurer_non_no');
            $el_insurance = $request->post('el_insurance');
            $non_rate = $request->post('non_rate');

            if($insurer_date_end == '') {
                $result['msg'] = '非车险终保日期不能为空';
                return $result;
            }
            if($insurer_non_no == '') {
                $result['msg'] = '非车险保单号不能为空';
                return $result;
            }
            if($el_insurance == '') {
                $result['msg'] = '非车险电子保单状态不能为空';
                return $result;
            }
            if($non_rate == '') {
                $result['msg'] = '非车险手续费不能为空';
                return $result;
            }

            $tran = OrderT::getDb()->beginTransaction();
            $model = OrderT::findOne(['id'=>$id]);
            try {
                $uid = $this->my->id;

                //修改非车险库
                if($model){
                    $old_status_id=$model->status_id;
                    $model->insurer_date_end=$insurer_date_end;
                    $model->insurer_non_no=$insurer_non_no;
                    $model->el_insurance=$el_insurance;
                    $model->non_rate=$non_rate;
                    //提交

                    if($status_id > $old_status_id) {

                        $model->op1_id=$uid;
                        $model->lock_id=0;
                        $model->status_id=$status_id;
                        $model->print_date = date('Y-m-d');

                        //非车险回单匹配
                        //财务处理
                        CaiwuT2::deleteAll(['order_id'=>$model->id]);
                        //商业
                        if($model->insurer_non_no != '') {
                            $row = null;
                            CaiwuT2::deleteAll('insurer_no="'.$model->insurer_non_no.'"');
                            $row = new CaiwuT2();
                            $row->insurer_no = $model->insurer_non_no;

                            $row->id_man = $model->id_man;

                            $row->total = $model->non_baofei; //总保费
//                            dd($fbaofei);
                            $row->total_clear=0;
                            $row->total_tax=0;
                            $row->total_clear_real=0;
                            $row->total_tax_real=0;
                            $row->total_real = sprintf("%.2f",($model->non_baofei * $model->non_rate/100));
                            $row->total_rate = $model->non_rate;
                            $row->total_dis = $row->total_real;

                            $row->user_id = $model->user_id;
                            $row->status = 0;
                            $row->insurer_type = 1;
                            $row->order_id = $model->id;
                            if(!$row->save()) {
                                var_dump($row->errors);
                                exit;
                            }
                        }

                        $this->addNonLog($model->non_id,'正本生成,流程结束',1);
                    }

                    $model->save();

                    //出纳办结
                    $order_caiwu = OrderCaiwuT1::findOne(['order_id'=>$model->id]);
                    if(!$order_caiwu) {
                        $order_caiwu = new OrderCaiwuT1();
                        $order_caiwu->order_id = $model->id;
                    }
                    $order_caiwu->status_id = 12;
                    $order_caiwu->pay_date = substr($model->print_date,0,7);
                    $order_caiwu->save();




                    }


                    //处理操作人员状态
                    $order_user_info = OrderUserT::findOne(['order_id'=>$id,'user_id'=>$this->my->id,'type_id'=>$old_status_id,'status'=>0]);
                    if($order_user_info) {
                        $order_user_info->status = 1; //操作结束
                        $order_user_info->save();
                    }










                $result['success'] = true;
                $result['msg'] = '保存成功';
                $tran->commit();
            }catch (\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }

        return $result;
    }
}
