<?php
namespace App\Domains\Company\Http\Controllers;

use App\Dicts;
use App\Core\Controller;
use Illuminate\Http\Request;
use App\Domains\Company\PayType;
use App\Domains\Virtual\Services\ProductService;

class BootstrapController extends Controller
{
    protected $request;

    /**
     * 构造函数，自动注入.
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->account = $request->user('company');
    }

    /**
     * 程序初始化
     */
    public function index(Dicts $dicts)
    {
        // 运营商列表
        $carrier_operator = $dicts->get('carrier_operator');
        $carrier_operator = json_decode(json_encode($carrier_operator, JSON_FORCE_OBJECT));

        $pay_types = new PayType();

        $order_status = json_decode(json_encode($dicts->get('order_status'), JSON_FORCE_OBJECT));
        $transaction_status = json_decode(json_encode($dicts->get('transaction_status'), JSON_FORCE_OBJECT));
        $pay_channels = ['bank' => '银行转账', 'alipay' => '支付宝转账'];

        $res = compact('carrier_operator', 'pay_channels', 'pay_types', 'order_status', 'transaction_status');

        return res($res, '程序初始化', 201);
    }

    /**
     * 获取企业套餐列表
     */
    public function products(ProductService $productService)
    {
        $conditions = $this->request->all();

        $conditions['company_id'] = $this->account->company_id;

        $conditions['type'] = $conditions['type'] ? ($conditions['type'] == 1 ? 0 : $conditions['type']) : 0;

        $res = $productService->index($conditions);

        $res = $res->where('status', 0)->transform(function ($item) {
            return [
                'id' => $item->id,
                'sn' => $item->sn,
                'name' => $item->name,
                'carrier_operator' => $item->carrier_operator,
                'carrier_operator_name' => $item->carrier_operator_name,
                'package_id' => $item->package_id,
                'package_name' => $item->package['name'] ?? '',
                'price' => $item->price
            ];
        })->values();

        return res($res, '企业套餐列表', 201);
    }
}
