<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 订单管理
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisShopOrder;
use app\ais\model\AisBank;
use app\ais\model\AisShop;
use app\ais\model\AisConfig;
use app\ais\model\AisCouponUser;
use app\ais\model\AisVipUser;
use app\common\model\SystemUser;
use app\common\facade\WechatPay;

class Shoporder extends Base{

    /**
     * 初始化当前应用是否登录
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
    }

    /**
     * @param int $store_id
     * 店铺订单
     */
    public function index(){
        $param['active']  = $this->request->param('active/d',0);
        $param['page']    = $this->request->param('page/d',1);
        $this->apiSign($param);
        $condition['uid']    = $this->user->id;
        $condition['is_del'] = 0;
        switch ($param['active']) {
            case 1:
                $condition['paid_at'] = 0;
                break;
            case 2:
                $condition['paid_at'] = 1;
                $condition['status']  = 0;
                break;
            case 3:
                $condition['paid_at'] = 1;
                $condition['status']  = 1;
                break;
        }
        $order = AisShopOrder::with(['store' => function($query) {
            $query->field('name,address,id');
        }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->where($condition)->page($param['page'],10)->select();
        if($order->isEmpty()){
            return enjson(204);
        }    
        $data = [];
        $ids  = [];
        foreach ($order as $key => $value) {
            $data[$key] = $value;
            $data[$key]['status_text'] = $value['statu'];
            $data[$key]['end_paytime'] = 0;
            if($value['paid_at'] == 0){
                $data[$key]['end_paytime'] = (($value['create_time'] + 60 * 10) - time()) * 1000;
            }
            if($value['paid_at'] == 0 && $data[$key]['end_paytime'] < 0){
                $ids[] = $value['id'];
                unset($data[$key]);
            }
        }
        if(!empty($ids)){ //过期了更改了状态
            AisShopOrder::where(['id' => $ids])->update(['is_del' => 1]);
        }
        if(empty($data)){
            return enjson(204);
        }
        return enjson(200,array_values($data));
    }

      
    /**
     * @param int $store_id
     */
    public function views(){
        $param['id'] = $this->request->param('id/d',0);
        $this->apiSign($param);
        $condition['uid'] = $this->user->id;
        $condition['id']  = $param['id'];
        $order = AisShopOrder::with(['store'	=> function($query) {
            $query->field('name,address,id,longitude,latitude');
        }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del,thrifty')->where($condition)->find();
        if(empty($order)){
            return enjson(204);
        }
        $order->end_paytime = 0;
        if(!$order->paid_at){
            $order->end_paytime = (($order->create_time + 60 * 10) - time()) * 1000;
        }
        if($order->paid_at == 0 && $order->end_paytime < 0){
            $order->is_del = 1;
            $order->save();
        }
        $shop_cache = $order->shop_cache;
        $shop_cache['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($shop_cache['notice']));
        $order->shop_cache  = $shop_cache;
        $order->paid_time   = date('Y-m-d H:i:s',$order->paid_time);
        $order->create_time = date('Y-m-d H:i:s',$order->create_time);
        return enjson(200,$order);
    }

    /**
     * @param int $order_id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取核心订单
     */
    public function orderVer(){
        $param['id'] = $this->request->param('id/d',0);
        $this->apiSign($param);
        $condition['id']      = $param['id'];
        $condition['paid_at'] = 1;
        $condition['status']  = 0;
        $condition['is_del']  = 0;
        $info = AisShopOrder::with(['store'	=> function($query) {
            $query->field('name,address,id,longitude,latitude');
        }])->where($condition)->field('store_id,uid,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->find();
        if(empty($info)){
            return enjson(303,'订单不存在',['url' => '/pages/index']);
        }
        $uid =  array_flip(array_column($info->storeWorker->toArray(),'uid'));
        if(empty($uid)){
            return enjson(303,'商家没有设置核销员',['url' => '/pages/index']);
        }
        if (!isset($uid[$this->user->id])){
            return enjson(303,'你无权限核销订单',['url' => '/pages/index']);
        }
        if(request()->isPost()){
            $info->status = 1;
            $info->save();
            return enjson(303,'订单成功核销',['url' => '/pages/user/index']);
        }else{
            $shop_cache = $info->shop_cache;
            $shop_cache['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($shop_cache['notice']));
            $info->shop_cache  = $shop_cache;
            $info->paid_time   = date('Y-m-d H:i:s',$info->paid_time);
            $info->create_time = date('Y-m-d H:i:s',$info->create_time);
            return enjson(200,'成功',$info);
        }
    }
}