package com.openhis.web.pharmacyWarehousemanage.controller;

import com.core.common.core.domain.R;
import com.openhis.common.enums.LocationForm;
import com.openhis.common.enums.SupplyType;
import com.openhis.web.pharmacyWarehousemanage.appservice.IPharmacyWarehouseCommonService;
import com.openhis.web.pharmacyWarehousemanage.appservice.IPharmacyWarehousePurchaseOrderService;
import com.openhis.web.pharmacyWarehousemanage.appservice.IPharmacyWarehouseReceiptApprovalService;
import com.openhis.web.pharmacyWarehousemanage.dto.PharmacyWarehouseDetailDto;
import com.openhis.web.pharmacyWarehousemanage.dto.PharmacyWarehouseSearchParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 药库订货单
 *
 * @author  yuxj
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/pharmacy-warehouse/purchase-order")
@Slf4j
public class PharmacyWarehousePurchaseOrderController {

    @Autowired
    private IPharmacyWarehousePurchaseOrderService pharmacyWarehousePurchaseOrderService;

    @Autowired
    private IPharmacyWarehouseCommonService pharmacyWarehouseCommonService;

    @Autowired
    private IPharmacyWarehouseReceiptApprovalService pharmacyWarehouseReceiptApprovalService;
    /**
     * 订货单初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> init() {
        return pharmacyWarehousePurchaseOrderService.init();
    }

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

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

    /**
     * 添加/编辑采购单
     *
     * @param purchaseOrderDtoList 采购信息
     * @return 操作结果
     */
    @PutMapping("/addOrEdit-purchaseOrder")
    public R<?> addOrEditPurchaseOrder(@Validated @RequestBody List<PharmacyWarehouseDetailDto> purchaseOrderDtoList) {
        return pharmacyWarehousePurchaseOrderService.addOrEditPurchaseOrder(purchaseOrderDtoList);
    }

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

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

//    /**
//     * 提交审批
//     *
//     * @param busNo 单据号
//     * @return 操作结果
//     */
//    @PutMapping("/submit-approval")
//    public R<?> submitApproval(@RequestParam String busNo) {
//        return pharmacyWarehouseCommonService.submitApproval(busNo);
//    }
//
//    /**
//     * 撤回审批
//     *
//     * @param busNo 单据号
//     * @return 操作结果
//     */
//    @PutMapping("/withdraw-approval")
//    public R<?> withdrawApproval(@RequestParam String busNo) {
//        return pharmacyWarehouseCommonService.withdrawApproval(busNo);
//    }

}
