<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\BulkPurchaseInquiryModel;
use app\admin\model\v1\LanguageModel;
use app\admin\model\v1\SysConfigModel;

/**
 * 批量采购询盘控制器
 */
class BulkPurchaseInquiry
{
    // 采购可选品类
    public function interested()
    {
        $config_name = 'bulk_purchase_inquiry_interested';
        $config = SysConfigModel::hasWhere('group', function($query) {
                $query->where('language_id', '=', request()->lang_id);
            })
            ->byName($config_name)
            ->find();
        if (empty($config)) {
            return error('当前选定语言的采购可选品类配置出错');
        }

        return success('获取成功', explode(',', preg_replace('/\r?\n/', ',', $config->value)));
    }
    
    // 分页
    public function index()
    {
        $param = request()->param([
            'corp_name',
            'interested',
            'created_at',
            'page/d' => 1,
            'size/d' => 10
        ]);

        $msgs = BulkPurchaseInquiryModel::field([
                'id',
                'ip',
                'corp_name',
                'first_name',
                'last_name',
                'email',
                'phone',
                'interested',
                'message',
                'created_at'
            ])
            ->withSearch(['corp_name', 'interested', 'created_at'], [
                'corp_name'  => $param['corp_name'] ?? null,
                'interested' => $param['interested'] ?? null,
                'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
            ])
            ->language(request()->lang_id)
            ->order(['id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
            ]);

        return success('获取成功', $msgs);
    }

    // 导出
    public function export()
    {
        $schema = [
            'id'         => 'ID',
            'created_at' => '提交时间',
            'ip'         => 'IP',
            'corp_name'  => '公司名称',
            'username'   => '姓名',
            'email'      => '邮箱',
            'phone'      => '手机号码',
            'interested' => '想采购的产品',
            'message'    => '询问内容',
        ];

        // 获取要导出的采购询盘数据
        $data = $this->getExportData();

        // 导出
        return xlsx_writer($data, $schema, '批量购买询盘列表' . date('YmdHis'));
    }
    // 获取要导出的采购询盘数据
    private function getExportData()
    {
        $param = request()->param([
            'corp_name',
            'interested',
            'created_at',
        ]);

        return BulkPurchaseInquiryModel::field([
                'id',
                'ip',
                'corp_name',
                'CONCAT(first_name, last_name)' => 'username',
                'last_name',
                'email',
                'phone',
                'interested',
                'message',
                'created_at'
            ])
            ->withSearch(['corp_name', 'interested', 'created_at'], [
                'corp_name'  => $param['corp_name'] ?? null,
                'interested' => $param['interested'] ?? null,
                'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
            ])
            ->language(request()->lang_id)
            ->order(['id' => 'desc'])
            ->select();
    }
}
