package com.openhis.web.pharmacyDispensarymanage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.core.common.core.domain.R;
import com.openhis.common.enums.LocationForm;
import com.openhis.common.enums.SupplyType;
import com.openhis.web.pharmacyDispensarymanage.appservice.IPharmacyDispensaryCommonService;
import com.openhis.web.pharmacyDispensarymanage.appservice.IPharmacyDispensaryRequisitionOrderService;
import com.openhis.web.pharmacyDispensarymanage.appservice.IPharmacyDispensaryReceiptApprovalService;
import com.openhis.web.pharmacyDispensarymanage.dto.PharmacyDispensaryDetailDto;
import com.openhis.web.pharmacyDispensarymanage.dto.PharmacyDispensarySearchParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 药房请领单
 *
 * @author  yuxj
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/pharmacy-dispensary/requisition-order")
@Slf4j
public class PharmacyDispensaryRequisitionOrderController {

    @Autowired
    private IPharmacyDispensaryRequisitionOrderService pharmacyDispensaryRequisitionOrderService;

    @Autowired
    private IPharmacyDispensaryCommonService pharmacyDispensaryCommonService;

    @Autowired
    private IPharmacyDispensaryReceiptApprovalService pharmacyDispensaryReceiptApprovalService;
    /**
     * 请领单初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> init() {
        return pharmacyDispensaryRequisitionOrderService.init();
    }

    /**
     * 获取药品
     *
     * @param requisitionOrderSearchParam 查询条件
     * @return 药品信息
     */
    @GetMapping(value = "/medication-info")
    public R<?> getMedicationInfo(PharmacyDispensarySearchParam requisitionOrderSearchParam,
                                  @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return pharmacyDispensaryCommonService.getMedicationInfo(requisitionOrderSearchParam, pageNo, pageSize, searchKey, request);
    }

    /**
     * 请领单单据列表
     *
     * @param requisitionOrderSearchParam 查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 请领单据分页列表
     */
    @GetMapping(value = "/requisition-order-page")
    public R<?> getPage(PharmacyDispensarySearchParam requisitionOrderSearchParam,
                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return pharmacyDispensaryCommonService.getPage(requisitionOrderSearchParam, pageNo, pageSize, searchKey, request,
            null, SupplyType.PURCHASE_REQUISITION.getValue());
    }
    /**
     * 请领单单据详情
     *
     * @param busNo 单据号
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @return 请领单单据详情
     */
    @GetMapping(value = "/requisition-order-detail-page")
    public R<?> getDetail(@RequestParam String busNo,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return pharmacyDispensaryRequisitionOrderService.getDetailPage(busNo, pageNo, pageSize);
    }
    /**
     * 获取单据号
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/busNo-init")
    public R<?> getBusNo() {
        return pharmacyDispensaryRequisitionOrderService.getBusNo();
    }

    /**
     * 添加/编辑采购单
     *
     * @param requisitionOrderDtoList 采购信息
     * @return 操作结果
     */
    @PutMapping("/addOrEdit-requisitionOrder")
    public R<?> addOrEditRequisitionOrder(@Validated @RequestBody List<PharmacyDispensaryDetailDto> requisitionOrderDtoList) {
        return pharmacyDispensaryRequisitionOrderService.addOrEditRequisitionOrder(requisitionOrderDtoList);
    }

    /**
     * 删除单据
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @DeleteMapping("/delete-requisitionOrder")
    public R<?> deleteRequisitionOrder(@RequestParam String busNo) {
        return pharmacyDispensaryCommonService.deleteOrder(busNo);
    }

    /**
     * 同意审批
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @PutMapping("/agree-approval")
    public R<?> agreeApproval(@RequestParam String busNo) {
        return pharmacyDispensaryReceiptApprovalService.requisitionOrderAgreeApproval(busNo);
    }

}
