package com.eactive.eai.rms.onl.apim.approval.credential;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
@RequiredArgsConstructor
public class CredentialManController extends OnlBaseAnnotationController {

    private final CredentialManService credentialManService;

    @GetMapping(value = "/onl/apim/approval/credential/credentialMan.view")
    public void view() {
    }

    @GetMapping(value = "/onl/apim/approval/credential/credentialMan.view", params = "cmd=DETAIL")
    public String detailView() {
        return "/onl/apim/approval/credential/credentialManDetail";
    }

    @PostMapping(value = "/onl/apim/approval/credential/credentialMan.json", params = "cmd=LIST")
    public ResponseEntity<GridResponse<CredentialUI>> selectList(@SortDefault("clientname") Pageable pageable,
        CredentialSearch clientSearch) {
        Page<CredentialUI> page = credentialManService.selectList(pageable, clientSearch);
        return ResponseEntity.ok(new GridResponse<>(page));
    }

    @PostMapping(value= "/onl/apim/approval/credential/credentialMan.json",params = "cmd=LIST_DETAIL_COMBO")
    public ResponseEntity<Map<String, Object>> selectDetailCombo() throws Exception {
        Map<String, Object> map = credentialManService.selectDetailCombo();
        return ResponseEntity.ok(map);
    }

    @PostMapping(value = "/onl/apim/approval/credential/credentialMan.json", params = "cmd=DETAIL")
    public ResponseEntity<CredentialUI> selectDetail(String clientId) {
        CredentialUI clientUI = credentialManService.selectDetail(clientId);
        return ResponseEntity.ok(clientUI);
    }

    @PostMapping(value = "/onl/apim/approval/credential/credentialMan.json", params = "cmd=INSERT")
    public ResponseEntity<Void> insert(CredentialUI clientUI) {
        credentialManService.insert(clientUI);
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/apim/approval/credential/credentialMan.json", params = "cmd=UPDATE")
    public ResponseEntity<Void> update(CredentialUI clientUI) {
        credentialManService.update(clientUI);
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/apim/approval/credential/credentialMan.json", params = "cmd=DELETE")
    public ResponseEntity<Void> delete(String clientId) {
        credentialManService.delete(clientId);
        return ResponseEntity.ok().build();
    }
}
