<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\{Category, Donate as DonateModel};
use liuniu\IdentityCard;
use liuniu\UtilService;
use think\Request;

class Donate extends Api
{
    protected $noNeedRight = ['*'];

    public function apply(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['cid', $this->cid],
                ['user_id', $this->auth->getUserinfo()['id']],
                ['full_name', ''],
                ['sex', 0],
                ['birthday', date("Y-m-d")],
                ['id_card', ''],
                ['vocation', ''],
                ['education', 0],
                ['category_ids', 0],
                ['tel', ''],
                ['mobile', ''],
                ['executor', ''],
                ['executor_relation', ''],
                ['executor_id_card', ''],
                ['executor_tel', ''],
                ['executor_mobile', ''],
            ], $request
        );
        if ($where['full_name'] == '') $this->error('姓名不能为空');
        if (!IdentityCard::isValid($where['id_card'])) $this->error('身份证号不正确!');
        if ($where['vocation'] == '') $this->error('职业不能为空');
        if ($where['category_ids'] == '') $this->error('捐献类型不能空');
        if ($where['executor_relation'] == '') $this->error('执行人关系');
        if (!preg_match("/^1\d{10}$/", $where['mobile'])) $this->error('手机号不对');
        if (!preg_match("/^1\d{10}$/", $where['executor_mobile'])) $this->error('执行人移动电话不对');
        if (!IdentityCard::isValid($where['executor_id_card'])) $this->error('执行人身份证号不正确!');
        $this->success(DonateModel::create($where));
    }

    public function ify(Request $request)
    {
        $this->success('获取成功', Category::getCategoryArray('donate'));
    }

    public function lst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['cid', $this->cid],
                ['user_id', $this->auth->getUserinfo()['id']],
                ['page', 1],
                ['limit', 10],
                ['status', -2],
            ]
        );
        return $this->success('获取成功', DonateModel::lst($where));
    }

    public function view()
    {
        $id = input('id', 0);
        if ($id == 0) $this->error('参数错误!');
        $rs = DonateModel::info($id);
        if (!$rs) $this->error(HelpModel::getErrorInfo());
        return $this->success('获取成功', $rs);
    }
}