<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\AgentEnterpriseSizeTypeModel;
use app\admin\model\v1\AgentModel;

/**
 * 代理商数据控制器
 */
class Agent
{
    // 企业规模列表
    public function enterpriseSizeTypes()
    {
        $types = AgentEnterpriseSizeTypeModel::field([
                'name',
                'value'
            ])
            ->language(request()->lang_id)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();
        return success('获取成功', $types);
    }

    // 分页
    public function index()
    {
        $param = request()->param([
            'corp_name',
            'size_type',
            'created_at',
            'page/d' => 1,
            'size/d' => 10
        ]);

        $agents = AgentModel::field([
                'id',
                'ip',
                'corp_name',
                'email',
                'phone',
                'business_type',
                'enterprise_size',
                'address',
                'message',
                'created_at'
            ])
            ->with([
                'businessType' => function ($query) {
                    $query->field(['name' => 'business_type_name', 'value']);
                },
                'enterpriseSizeType' => function ($query) {
                    $query->field(['name' => 'enterprise_size_name', 'value']);
                }
            ])
            ->withSearch(['corp_name', 'created_at'], [
                'corp_name'  => $param['corp_name'] ?? null,
                'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
            ])
            ->language(request()->lang_id)
            ->enterpriseSize($param['size_type'] ?? null)
            ->order(['id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
            ])
            ->bindAttr('businessType', ['business_type_name'])
            ->bindAttr('enterpriseSizeType', ['enterprise_size_name'])
            ->hidden(['business_type', 'businessType', 'enterprise_size', 'enterpriseSizeType']);

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

    // 导出
    public function export()
    {
        $schema = [
            'id'                   => 'ID',
            'created_at'           => '提交时间',
            'ip'                   => 'IP',
            'corp_name'            => '公司名称',
            'email'                => '邮箱',
            'phone'                => '手机号',
            'business_type_name'   => '业务类型',
            'enterprise_size_name' => '企业规模',
            'address'              => '公司地址',
            'message'              => '留言内容'
        ];

        // 获取要导出的代理商数据
        $agents = $this->getAgentExportData();

        // 导出
        return xlsx_writer($agents, $schema, '代理商申请列表' . date('YmdHis'));
    }
    // 获取要导出的代理商数据
    private function getAgentExportData()
    {
        $param = request()->param([
            'corp_name',
            'size_type',
            'created_at'
        ]);

        return AgentModel::field([
                'id',
                'ip',
                'corp_name',
                'email',
                'phone',
                'business_type',
                'enterprise_size',
                'address',
                'message',
                'created_at'
            ])
            ->with([
                'businessType' => function ($query) {
                    $query->field(['name' => 'business_type_name', 'value']);
                },
                'enterpriseSizeType' => function ($query) {
                    $query->field(['name' => 'enterprise_size_name', 'value']);
                }
            ])
            ->withSearch(['corp_name', 'created_at'], [
                'corp_name'  => $param['corp_name'] ?? null,
                'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
            ])
            ->language(request()->lang_id)
            ->enterpriseSize($param['size_type'] ?? null)
            ->order(['id' => 'desc'])
            ->select()
            ->bindAttr('businessType', ['business_type_name'])
            ->bindAttr('enterpriseSizeType', ['enterprise_size_name'])
            ->hidden(['business_type', 'businessType', 'enterprise_size', 'enterpriseSizeType']);
    }
}
