<?php
namespace app\mobile\controller;
use think\exception\HttpResponseException;

class OmsLeaveComplete extends OmsBase
{
    private function dispatch($action,$method)
    {
        return $this->guarded(function()use($action,$method){
            $this->requireMethod($method);$account=$this->current();$actor=OmsVisitor::adminFor($account);
            if(!$actor||(int)$actor['dpt']!==6)$this->fail('仅人事部门账号可以办理离职',403);
            if($action==='preview'){
                $mode=$this->request->post('mode');
                if(!in_array($mode,['complete','create'],true))$this->fail('请选择已申请办理或未申请办理');
                if($mode==='create'){$rows=$this->request->post('rows');if(!is_array($rows)||count($rows)!==1)$this->fail('小程序每次办理一名员工');}
            }
            $handler=new \app\mobile\service\MobileLeaveHandler($this->app,$actor,$this->bearer());
            try{$response=$handler->$action();}catch(HttpResponseException $e){$response=$e->getResponse();}
            $body=$response->getData();
            if(!is_array($body))$body=json_decode($response->getContent(),true);
            return json(['code'=>$body['code']??1,'message'=>$body['message']??$body['msg']??'操作失败','data'=>$body['data']??null],$response->getCode());
        });
    }
    public function options(){return $this->dispatch('options','GET');}
    public function records(){return $this->dispatch('index','GET');}
    public function detail(){return $this->dispatch('detail','GET');}
    public function employee(){return $this->dispatch('employee','GET');}
    public function preview(){return $this->dispatch('preview','POST');}
    public function commit(){return $this->dispatch('commit','POST');}
}
