<?php



namespace app\admin\controller\mall;



use Throwable;

use app\common\controller\Backend;

use app\common\model\MallAddress;



/**

 * 收获地址管理

 */

class Address extends Backend

{

    /**

     * MallAddress模型对象

     * @var object|null

     * @phpstan-var \app\common\model\MallAddress|null

     */

    protected ?object $model = null;



    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time', 'region_origin'];



    protected array $withJoinTable = ['playxUserAsset'];



    protected string|array $quickSearchField = ['id'];



    public function initialize(): void

    {

        parent::initialize();

        $this->model = new MallAddress();

    }



    /**

     * 查看

     * @throws Throwable

     */

    public function index(\Webman\Http\Request $request): \support\Response

    {

        $response = $this->initializeBackend($request);

        if ($response !== null) {

            return $response;

        }



        if ($request->get('select') || $request->post('select')) {

            $this->_select();

            return $this->success();

        }



        list($where, $alias, $limit, $order) = $this->queryBuilder();

        $res = $this->model

            ->with(['playxUserAsset' => function ($query) {

                $query->field('id,username');

            }])

            ->visible(['playxUserAsset' => ['username']])

            ->alias($alias)

            ->where($where)

            ->order($order)

            ->paginate($limit);



        return $this->success('', [

            'list'   => $res->items(),

            'total'  => $res->total(),

            'remark' => get_route_remark(),

        ]);

    }



    public function edit(\Webman\Http\Request $request): \support\Response

    {

        $response = $this->initializeBackend($request);

        if ($response !== null) {

            return $response;

        }



        if ($request->method() === 'POST') {

            $post = $request->post();

            $regionOrigin = trim(strval($post['region_origin'] ?? ''));

            if (isset($post['region']) && $regionOrigin !== '' && MallAddress::isAreaIdList($regionOrigin)) {

                $displayOrigin = MallAddress::resolveAreaNames($regionOrigin);

                if (MallAddress::normalizeRegion($post['region']) === MallAddress::normalizeRegion($displayOrigin)) {

                    $request->setPost(array_merge($post, ['region' => $regionOrigin]));

                }

            }

            return $this->_edit();

        }



        $pk = $this->model->getPk();

        $id = $request->get($pk);

        $row = $this->model->find($id);

        if (!$row) {

            return $this->error(__('Record not found'));

        }



        $dataLimitAdminIds = $this->getDataLimitAdminIds();

        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {

            return $this->error(__('You have no permission'));

        }



        $rowData = $row->toArray();

        $rowData['region_origin'] = $row->getData('region');



        return $this->success('', ['row' => $rowData]);

    }

}

