<?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\info;
use app\ais\controller\Common;
use app\ais\model\AisInfoOrder;
use think\facade\Request;
use think\helper\Time;

class Order extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'订单管理','url'=>url("info.order/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $infoid = 0,int $types = 1){
        $condition = [];
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $condition[] = ['is_del', '=',0];
        $view['keyword']   = Request::param('keyword/s');
        if($view['keyword'] ){
            $condition[] = ['phone','<=', $view['keyword']];
        }
        $view['starttime'] = Request::param('starttime/s');
        $view['endtime']   = Request::param('endtime/s');
        $view['time']      = Request::param('time/d',0);
        if ($view['time']) {
            switch ($view['time']) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time', '>=', $start];
            $condition[] = ['create_time', '<=', $end];
        } else {
            if ($view['starttime']) {
                $condition[] = ['create_time', '>=', strtotime($view['starttime'])];
            }
            if ($view['endtime']) {
                $condition[] = ['create_time', '<=', strtotime($view['endtime'])];
            }
        }
        $view['types']     = $types;
        switch ($types) {
            case 1:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 0];
                break;
            case 2:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 1];
                break;
            default:
                $condition[] = ['paid_at', '=',0];
                break;
        }
        $view['lists']   = AisInfoOrder::where($condition)->order('id desc')->paginate(20, false, ['query' => ['keyword' => $view['keyword'], 'starttime' => $view['starttime'], 'endtime' => $view['endtime'], 'time' => $view['time']]]);
        $view['pay']     = AisInfoOrder::where($this->mini_program)->where(['paid_at' => 1])->sum('amount');
        $view['invalid'] = AisInfoOrder::where($this->mini_program)->where(['status' => 1])->sum('amount');
        $view['infoid']  = $infoid;
        if($infoid){
            $view['tabs'] = [
                ['name' =>'信息内容','url' =>url('info.index/reply',['id' => $infoid])],
                ['name' =>'订单列表','url' =>url('info.order/index',['infoid' => $infoid]),'action' => 1],
            ];
        }
        return view()->assign($view);
    }

    /**
     * 订单详情
     */
    public function detail(int $id){
        $info         = AisInfoOrder::where(['id' => $id])->find();
        $info->field  = json_decode($info->fields, true);
        $view['info'] = $info;
        return view()->assign($view);
    }

    /**
     * 删除
     */
    public function delete($id){
        $result = AisInfoOrder::update(['is_del'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('index',['status' => $this->request->param('status/d')])]);
        }
        return enjson(0,'操作失败');
    }

    /**
     * 订单完成
     */
    public function completion($id){
        $result = AisInfoOrder::update(['status'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('index',['status' => $this->request->param('status/d')])]);
        }else{
            return enjson(0,'操作失败');
        }
    }
}