<?php

/**
 * 控制器:保险管理后台控制器
 * date:2019-08-31
 */

namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use app\modules\logic\model\OrderSettleModel;
use app\modules\logic\services\TechService;
use Yii;
use yii\web\Controller;

class SafeController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'safe') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
	 * [actionList 保险管理列表]
	 * @apiDoc
	 * @api    v1/safe/list
	 * @group  safe
	 * @name   保险列表
	 * @desc   保险管理->出行意外险，施工质保险列表
	 * @method POST
	 * @param currentPage string 当前页 noreq 1
     * @param pageSize string 每页条数 noreq 15
     * @param type int 保险类型【1，出行意外，2，施工质保】 req
     * @param addtime_from string 查询时间【开始】 noreq
	 * @param addtime_to string 查询时间【结束】 noreq
	 * @return json
		 {
		"code": "000000",
		"msg": "操作成功",
		"total": "1",
		"info": [
		    {
		        "order_id": "1",
		        "nid": "xxx",
		        "user_id": "1",
		        "created": "1970-01-01 08:00:00",
		        "tech_info": {
		            "mobile": "18980647855",
		            "realname": "张三",
		            "id_card": "513701199007225224"
		        },
		        "money": 50,
		        "status": "正常"
		    }
		]
		}
	 */
	public function actionList(){
		if (empty($this->para['type']) || !in_array($this->para['type'], ['1','2'])) {
            return $this->response('para_miss', '请选择正确的类型');
        }
		$_where = ['cols'=>['order_id','nid','user_id','created']];
		$_where['order'] = ['order_id' => 'desc'];
		/*查询时间搜索*/
		if (!empty($this->para['addtime_from'])) {
			$_where['scope']['ltt']['created'] = strtotime($this->para['addtime_from']);
		}
		if (!empty($this->para['addtime_to'])) {
			$_where['scope']['mtt']['created'] = strtotime($this->para['addtime_to']);
		}
		/*分页处理*/
        $currentPage = 1;
        $pageSize = 15;
        if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
            $currentPage = $this->para['currentPage'];
        }
        if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
            $pageSize = $this->para['pageSize'];
        }
        $OrderSettle = new OrderSettleModel();
        $total = $OrderSettle->getWidgetTotal($_where);
        $list = $OrderSettle->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
            foreach ($list as &$value) {
            	$value['tech_info'] = TechService::getTechBase($value['user_id']);
            	$value['money'] = 50;
            	$value['status']='正常';
                $value['created'] = date("Y-m-d H:i:s", $value['created']);
            }
        }
        return $this->response('search_succ', $list, $total);
	}
}