<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;
use app\admin\model\User;
use app\admin\model\Product;

use think\Db;

class Order extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/order.lang.php');
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function index()
    {
        $model_order = Model('Order');
        $order_identity = input('order_identity', '');
        $user_id = trim(input('user_id', ''));
        $product_id = trim(input('product_id', ''));
        $timeRang = trim(input('timeRang', ''));

        $where = [];
        if ($order_identity != '') {
            $where['order_identity'] = $order_identity;
        }

        if ($user_id) {
            $umodel = (new User())->getIdsByEmailLike($user_id);
            $uids = array_merge([0], $umodel);
            $where['user_id'] = ['IN', $uids];
        }

        if ($product_id) {
            $pmodel = (new Product())->getIdsByNameLike($product_id);
            $pids = array_merge([0], $pmodel);
            $where['product_id'] = ['IN', $pids];
        }

        if ($timeRang) {
            $t1 = substr($timeRang, 0, 10);
            $t2 = substr($timeRang, -10, 10);
            $where['order_buyTime'] = [[">=", $t1], ['<=', $t2]];
        }

        $order_list = $model_order->getOrdList($where, '*', 10);
        $this->assign('order_list', $order_list);
        $this->assign('show_page', $order_list->render());
        $this->setAdminCurItem('index');
        $this->assign('oinput', ['order_identity' => input("order_identity", ''), 'user_id' => input("user_id", ''), 'product_id' => input("product_id", ""), 'timeRang' => $timeRang]);
        return $this->fetch();
    }

}
