package com.openhis.web.pharmacyDispensarymanage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.openhis.web.pharmacyDispensarymanage.appservice.IPharmacyDispensaryReturnToWarehouseOrderService;
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.IPharmacyDispensaryReceiptApprovalService;
import com.openhis.web.pharmacyDispensarymanage.appservice.IPharmacyDispensaryReturnToWarehouseOrderService;
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/returnToWarehouse-order")
@Slf4j
public class PharmacyDispensaryReturnToWarehouseOrderController {

    @Autowired
    private IPharmacyDispensaryReturnToWarehouseOrderService pharmacyDispensaryReturnToWarehouseOrderService;

    @Autowired
    private IPharmacyDispensaryCommonService pharmacyDispensaryCommonService;
    @Autowired
    private IPharmacyDispensaryReceiptApprovalService pharmacyDispensaryReceiptApprovalService;
    /**
     * 退库单初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> init() {
        return pharmacyDispensaryReturnToWarehouseOrderService.init();
    }

    /**
     * 获取药品
     *
     * @param purchaseOrderSearchParam 查询条件
     * @return 药品信息
     */
    @GetMapping(value = "/medication-info")
    public R<?> getMedicationInfo(PharmacyDispensarySearchParam purchaseOrderSearchParam,
                                  @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(purchaseOrderSearchParam, pageNo, pageSize, searchKey, request);
    }

    /**
     * 退库单单据列表
     *
     * @param returnToDispensaryOrderSearchParam 查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 退库单据分页列表
     */
    @GetMapping(value = "/returnToDispensary-order-page")
    public R<?> getPage(PharmacyDispensarySearchParam returnToDispensaryOrderSearchParam,
                        @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(returnToDispensaryOrderSearchParam, pageNo, pageSize, searchKey, request,
            "1", SupplyType.PURCHASE_RETURN.getValue());
    }
    /**
     * 退库单单据详情
     *
     * @param busNo 单据号
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @return 退库单单据详情
     */
    @GetMapping(value = "/returnToDispensary-order-detail-page")
    public R<?> getDetail(@RequestParam String busNo,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return pharmacyDispensaryReturnToWarehouseOrderService.getDetailPage(busNo, pageNo, pageSize);
    }
    /**
     * 获取单据号
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/busNo-init")
    public R<?> getBusNo() {
        return pharmacyDispensaryReturnToWarehouseOrderService.getBusNo();
    }

    /**
     * 添加/编辑退库单
     *
     * @param returnToDispensaryOrderDtoList 退库信息
     * @return 操作结果
     */
    @PutMapping("/addOrEdit-returnToDispensaryOrder")
    public R<?> addOrEditReturnToDispensaryOrder(@Validated @RequestBody List<PharmacyDispensaryDetailDto> returnToDispensaryOrderDtoList) {
        return pharmacyDispensaryReturnToWarehouseOrderService.addOrEditReturnToWarehouseOrder(returnToDispensaryOrderDtoList);
    }

    /**
     * 删除单据
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @DeleteMapping("/delete-returnToDispensaryOrder")
    public R<?> deleteReturnToDispensaryOrder(@RequestParam String busNo) {
        return pharmacyDispensaryCommonService.deleteOrder(busNo);
    }
    /**
     * 同意审批
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @PutMapping("/agree-approval")
    public R<?> agreeApproval(@RequestParam String busNo) {
        return pharmacyDispensaryReceiptApprovalService.returnToWarehouseAgreeApproved(busNo);
    }

}
