<?php
/**
 *
 * @author：llbjj
 * @DateTime：2024/2/2 13:27
 * @Description：
 *
 */

namespace Application\Controller\Drug;

use Laminas\Config\Factory;

class DrugController extends \Laminas\Mvc\Controller\AbstractActionController
{
    public function patientDrugAction() {
        return $this->RenderApiJson()->Success(
            $this->LocalService()->tmpDoctoradvicelock->getpatientdrug()
        );
    }

    public function drugLockAction() {
        $patient_id = $this->params()->fromPost('patient_id', 0);
        $drug_id = $this->params()->fromPost('drug_id', 0);

        if(empty($patient_id) || empty($drug_id)) return $this->RenderApiJson()->Success();

        return $this->RenderApiJson()->Success(
            $this->LocalService()->tmpDoctoradvicelock->getlockdrugData($patient_id, $drug_id)
        );
    }

    public function drugDayAction() {
        $patient_id = $this->params()->fromPost('patient_id', 0);
        $drug_id = $this->params()->fromPost('drug_id', 0);

        if(empty($patient_id) || empty($drug_id)) return $this->RenderApiJson()->Success();
        return $this->RenderApiJson()->Success(
            $this->LocalService()->tmpDoctoradviceformal->getdormaldrugData($patient_id,$drug_id)
        );
    }

    public function drugDaySaveAction() {
        $drugDayData = $this->params()->fromPost('drugDay', []);

        foreach($drugDayData as $drugDay) {
            $this->LocalService()->tmpDoctoradviceformal->save($drugDay);
        }

        return $this->RenderApiJson()->Success();
    }

    public function arrangeDrugSaveAction() {
        $fileName = $this->params()->fromPost('fileName', '');
        $arrangeDrug = $this->params()->fromPost('arrangeDrug', []);

        if(!file_exists(APP_PATH . '/data/drug/')) {
            mkdir(APP_PATH . '/data/drug/', 0777, true);
        }
        Factory::toFile(APP_PATH . '/data/drug/'.$fileName.'.php', $arrangeDrug);
        return $this->RenderApiJson()->Success();
    }
}