package com.jwipc.inquiry.controller;

import com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.dto.PurchaseApplyBillDetailDTO;
import com.jwipc.inquiry.logic.PurchaseApplyBillDetailLogic;
import com.jwipc.inquiry.model.query.PurchaseApplyBillDetailQuery;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

import static com.jwipc.inquiry.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 采购申请明细 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-02-29
 */
@Controller
@RequestMapping(MODULE_ROOT + "/purchase/apply/bill/detail")
@RequiredArgsConstructor
public class PurchaseApplyBillDetailPageController {
    private final PurchaseApplyBillDetailLogic purchaseApplyBillDetailLogic;
    @GetMapping("/index")
    public String index(Model model, String purchasingBusinessType) {
        model.addAttribute("userName", LoginUtil.getName());
        model.addAttribute("purchasingBusinessType", purchasingBusinessType);
        return MODULE_ROOT + "/purchase/detail/list";
    }

    @GetMapping("/transfer/inquiry")
    public String transferInquiry(Model model, String ids,Integer optType,String purchasingBusinessType,String procureSubclass) {
        InquiryDTO inquiryDTO = new InquiryDTO();
        PurchaseApplyBillDetailQuery query = new PurchaseApplyBillDetailQuery();
        query.setIds(ids);
        List<PurchaseApplyBillDetailDTO> purchaseApplyBillDetailDTOS =  purchaseApplyBillDetailLogic.listAll(query);
        inquiryDTO.setDataSource(1);
        inquiryDTO.setPurchaseGroupCode(purchaseApplyBillDetailDTOS.get(0).getPurchaseGroupCode());
        inquiryDTO.setCompanyCode(purchaseApplyBillDetailDTOS.get(0).getCompanyCode());
        //默认采购组织
        inquiryDTO.setPurchaseOrgCode("8000");
        inquiryDTO.setPurchaseBillDetailIds(ids);
        inquiryDTO.setPurchaseType("0");
        inquiryDTO.setBeginTime(LocalDateTime.now());
        inquiryDTO.setPurchaseCategoryCode("Z005");
        inquiryDTO.setSpotFlag(0);
        inquiryDTO.setTemplateCode("PLATE_PRO_M002");
        model.addAttribute("data", inquiryDTO);
        if (StringUtils.isNotEmpty(purchasingBusinessType)) {
            inquiryDTO.setPurchaseRequestType(purchasingBusinessType);
        }
        model.addAttribute("optType", optType);
        model.addAttribute("procureSubclass", procureSubclass);
        return MODULE_ROOT + "/hall/detail";
    }

    /**
     * 采购申请转招标
     * @param model
     * @param ids
     * @param
     * @return
     */
    @GetMapping("/transfer/tender")
    public String transferTender(Model model, String ids, String purchasingBusinessType) {
        InquiryDTO inquiryDTO = new InquiryDTO();
//        PurchaseApplyBillDetailQuery query = new PurchaseApplyBillDetailQuery();
//        query.setIds(ids);
//        List<PurchaseApplyBillDetailDTO> purchaseApplyBillDetailDTOS =  purchaseApplyBillDetailLogic.listAll(query);
//        inquiryDTO.setDataSource(1);
//        inquiryDTO.setPurchaseGroupCode(purchaseApplyBillDetailDTOS.get(0).getPurchaseGroupCode());
//        inquiryDTO.setCompanyCode(purchaseApplyBillDetailDTOS.get(0).getCompanyCode());
//        inquiryDTO.setPurchaseBillDetailIds(ids);
//        inquiryDTO.setPurchaseType("0");
//        inquiryDTO.setBeginTime(LocalDateTime.now());
//        model.addAttribute("data", inquiryDTO);
        model.addAttribute("purchaseIds", ids);
        model.addAttribute("optType", 1);
        model.addAttribute("type", 0);
        model.addAttribute("purchasingBusinessType", purchasingBusinessType);
        model.addAttribute("pgo", "8000");
        return "/pc/tender/detail";
    }
    @GetMapping("/purchaseMethodEdit")
    public String purchaseMethodEdit(Model model, Long id) {
        InquiryDTO inquiryDTO = new InquiryDTO();
        PurchaseApplyBillDetailQuery query = new PurchaseApplyBillDetailQuery();
        query.setId(id);
        PurchaseApplyBillDetailDTO purchaseApplyBillDetailDTOS =  purchaseApplyBillDetailLogic.selectById(query);
        model.addAttribute("data", purchaseApplyBillDetailDTOS);
        return MODULE_ROOT + "/purchase/detail/purchaseMethodEdit";
    }

    /**
     * 资产类申请特殊窗口
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/purchaseMethodEditTow")
    public String purchaseMethodEditTow(Model model, Long id) {
        InquiryDTO inquiryDTO = new InquiryDTO();
        PurchaseApplyBillDetailQuery query = new PurchaseApplyBillDetailQuery();
        query.setId(id);
        PurchaseApplyBillDetailDTO purchaseApplyBillDetailDTOS =  purchaseApplyBillDetailLogic.selectById(query);
        model.addAttribute("data", purchaseApplyBillDetailDTOS);
        return MODULE_ROOT + "/purchase/detail/purchaseMethodEditTow";
    }

    /**
     * 资产类申请特殊窗口-设置采购路径
     * @param model
     * @param ids 采购申请明细ID集合
     * @return
     */
    @GetMapping("/purchaseWayMethodEditTow")
    public String purchaseWayMethodEditTow(Model model, @RequestParam String ids) {
        InquiryDTO inquiryDTO = new InquiryDTO();
        PurchaseApplyBillDetailQuery query = new PurchaseApplyBillDetailQuery();
        query.setIds(ids); // 设置ID集合查询参数
        List<PurchaseApplyBillDetailDTO> purchaseApplyBillDetailDTOS = purchaseApplyBillDetailLogic.listAll(query);
        model.addAttribute("data", purchaseApplyBillDetailDTOS);
        model.addAttribute("ids", ids);
        return MODULE_ROOT + "/purchase/detail/purchaseWayMethodEditTow";
    }


    @RequestMapping("/select/dialog")
    public String selectDialog(Model model) {
        return MODULE_ROOT + "/purchase/detail/group_dialog";
    }

    @RequestMapping("/select/purchaseDialog")
    public String selectContractPurchase(Model model, String purchasingBusinessType, String contractType, String contractCompanyCode, String contractSupplierCode) {
        model.addAttribute("purchasingBusinessType", purchasingBusinessType);
        model.addAttribute("contractType", contractType);
        model.addAttribute("companyCode", contractCompanyCode);
        if(StringUtils.isNotEmpty(contractType) && "03".equals(contractType)){
            model.addAttribute("supplierCode", contractSupplierCode);
        }
        return MODULE_ROOT + "/purchase/detail/purchase_dialog";
    }

}
