package com.openhis.web.materialmanage.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.materialmanage.appservice.IMaterialCommonService;
import com.openhis.web.materialmanage.appservice.IMaterialReceiptApprovalService;
import com.openhis.web.materialmanage.appservice.IMaterialReturnToWarehouseOrderService;
import com.openhis.web.materialmanage.dto.MaterialDetailDto;
import com.openhis.web.materialmanage.dto.MaterialSearchParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 物资退库单
 *
 * @author  yuxj
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/material/returnToWarehouse-order")
@Slf4j
public class MaterialReturnToWarehouseOrderController {

    @Autowired
    private IMaterialReturnToWarehouseOrderService materialReturnToWarehouseOrderService;

    @Autowired
    private IMaterialCommonService materialCommonService;
    @Autowired
    private IMaterialReceiptApprovalService materialReceiptApprovalService;
    /**
     * 退库单初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> init() {
        return materialReturnToWarehouseOrderService.init();
    }

    /**
     * 获取器材
     *
     * @param purchaseOrderSearchParam 查询条件
     * @return 器材信息
     */
    @GetMapping(value = "/device-info")
    public R<?> getDeviceInfo(MaterialSearchParam 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 materialCommonService.getDeviceInfo(purchaseOrderSearchParam, pageNo, pageSize, searchKey, request);
    }

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

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

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

}
