<?php
//
//namespace Application\Service\Extension\CheckLogic;

use Application\Service\Extension\CheckLogic\AeContentProvider;
use Application\Service\Extension\CheckLogic\FormContentProvider;
use \Application\Form\item\patient\PatientFormModel;
use Application\Service\Extension\Laminas;

/**
 * @param int $d
 * @return int
 */
function _day(int $d = 1): int
{
    return 60 * 60 * 24 * $d;
}

/**
 * @param $h
 * @return float|int
 */
function hour ($h)
{
    return 60 * 60 * $h;
}

/**
 * @param $m
 * @return float|int
 */
function minute($m)
{
    return 60 * $m;
}

/**
 * @param $val
 * @param null $type
 * @return int|mixed
 */
function _val($val, $type = null)
{
    if ($type === null) {
        // 也许是日期
        if (stripos($val, '-') !== false) {
            $timeStamp = strtotime($val);
            if ($timeStamp !== false) {
                $val = $timeStamp;
            }
        }
    }

    return $val;
}

/**
 * @param string $params
 * @return false|float|int|string
 */
function _empty(string $params)
{
    list($checkTimeSign, $formVarName, $fieldVarName) = explode('.', $params);
    $provider = new FormContentProvider([$checkTimeSign, $formVarName, $fieldVarName]);
    $provider->isSkipEmptyException = true;
    return $provider->get() === true;
}

function _isChecked(string $params, $checkedVal): bool
{
    list($checkTimeSign, $formVarName, $fieldVarName) = explode('.', $params);
    $provider = new FormContentProvider([$checkTimeSign, $formVarName, $fieldVarName]);
    $data = $provider->get();
    if (!is_array($data)) {
        $data = (array)$data;
    }

    if (in_array($checkedVal, $data)) {
        return true;
    }

    return false;
}

function _notChecked(string $params)
{

}

function _visit(string $checkTimeSign, string $formVarName,string $fieldVarName)
{
    if ($checkTimeSign === 'AE') {
        $contentProvider = new AeContentProvider([$checkTimeSign, $formVarName, $fieldVarName]);
    } else {
        $contentProvider = new FormContentProvider([$checkTimeSign, $formVarName, $fieldVarName]);
    }

    return $contentProvider->get();
}


function _outAnchorTime(string $params): bool
{
    list($checkTimeId, $formVarName, $fieldVarName) = explode('.', $params);

    // 选中的字段的值
    $contentProvider = new FormContentProvider([$checkTimeId, $formVarName, $fieldVarName]);
    $data = $contentProvider->get();

    // 锚点日期的值
    $checkTimeInfo = Laminas::$serviceManager->itemChecktime->fetchOne([
        'where' => [
            'id' => $checkTimeId,
        ]
    ]);
    $fieldInfo = Laminas::$serviceManager->itemFormField->fetchOne([
        'where' => [
            'id' => $checkTimeInfo['field_id']
        ]
    ]);
    $formVarName = Laminas::$serviceManager->itemForm->fetchOne([
        'where' => ['id' => $fieldInfo['form_id']]
    ])['var_name'];

    $contentProvider = new FormContentProvider([$checkTimeId, $formVarName, $fieldInfo['var_name']]);
    $anchorTime = $contentProvider->get();

    $startTime = strtotime(PatientFormModel::_getTimeUnit($checkTimeInfo['start_time'], $checkTimeInfo['time_unit']), $anchorTime);
    $endTime = strtotime(PatientFormModel::_getTimeUnit($checkTimeInfo['end_time'], $checkTimeInfo['time_unit']), $anchorTime);

    return !($data > $startTime && $data < $endTime);
}