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

use App\Core\Controller;
use Illuminate\Http\Request;
use App\Domains\Export\Services\ExportService;
use App\Domains\Export\Services\ImportService;
use App\Domains\Virtual\Exports\PropertyExport;
use App\Domains\Virtual\Services\PropertyService;
use App\Domains\Virtual\Repositories\PropertySettingRepository;
use App\Exceptions\InvalidArgumentException;

class PropertyController extends Controller
{
    protected $request;
    protected $propertyService;

    /**
     * 构造函数，自动注入.
     */
    public function __construct(Request $request, PropertyService $propertyService)
    {
        $this->request = $request;
        $this->propertyService = $propertyService;
    }

    /**
     * 配置.
     *
     * @return \Illuminate\Http\Response
     */
    public function settings()
    {
        if ($this->request->isMethod('POST')) {
            $values = $this->request->get('data');
            $res = $this->propertyService->settingsStore($values);
            return res($res, '修改成功');
        } else {
            $conditions = $this->request->all();
            $res = $this->propertyService->settings($conditions);
            return res($res, '配置分类', 201);
        }
    }

    /**
     * 列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $conditions = $this->request->all();
        $list = $this->propertyService->index($conditions);
        return res($list, '客户套餐属性配置列表', 201);
    }

    /**
     * 存储.
     *
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
        $values = $this->request->get('data');
        $res = $this->propertyService->store($values);
        return res($res, '修改成功');
    }

    /**
     * 导出.
     *
     * @return \Illuminate\Http\Response
     */
    public function export()
    {
        $conditions = $this->request->all();

        try {
            $export = new PropertyExport($conditions);
            $url = ExportService::store($export, 'public');
        } catch (\Exception $e) {
            throw $e;
        }

        return res($url, '导出成功', 201);
    }

    /**
     * 导入.
     *
     * @return \Illuminate\Http\Response
     */
    public function import()
    {
        $file = $this->request->file('file');

        $settings = app(PropertySettingRepository::class)->getAll();
        $provinces = $settings['province'] ?? [];

        $data = ImportService::load($file);

        $array = [];

        $columns = [
            'company_id',
            'package_id',
            'product',
            'vehicle',
            'company',
            'customer',
        ];

        foreach ($data as $i => $item) {
            $row = $i + 2;

            foreach ($item as $v => $value) {
                if (strpos($v, '*') !== false) {
                    $item[trim($v, '*')] = $value;
                }
            }

            if (empty($item['企业id']) || empty($item['套餐id'])) {
                throw new InvalidArgumentException("行 {$row} #:无效行");
            }

            $value = [
                'company_id' => $item['企业id'],
                'package_id' => $item['套餐id'],
                'product' => $item['产品类型'],
                'vehicle' => $item['车辆类型'],
                'commercial_vehicle' => $item['商用车分类'],
                'company' => $item['公司类型'],
                'platform' => $item['平台/api'],
                'customer' => $item['客户类型'],
            ];

            foreach ($provinces as $province) {
                $value['province'][$province] = !empty($item[$province]) ? $item[$province] : 0;
                $value['province'][$province] = sprintf("%.2f", $value['province'][$province]);
            }

            foreach ($columns as $column) {
                if (empty($value[$column])) {
                    $filed = PropertyService::$property_types[$column];
                    throw new InvalidArgumentException("行 {$row} #:字段 {$filed} 不能为空");
                }
            }

            $array[] = $value;
        }

        $this->propertyService->store($array);

        return res(null, '导入成功');
    }
}
