package com.jwipc.order.controller.pc;

import cn.hutool.core.collection.CollUtil;
import com.jwipc.order.entity.BasicPlan;
import com.jwipc.order.entity.ShortagePlan;
import com.jwipc.order.logic.ShortagePlanLogic;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.SysConfigClient;
import com.jwipc.sys.client.dto.FileResposeCDTO;
import lombok.RequiredArgsConstructor;
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.util.List;

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

/**
 * @Author: swq
 * @Date: 2024/4/28 17:08
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/shortagePlan")
public class ShortagePlanPageController {
    private final ShortagePlanLogic shortagePlanLogic;
    private final SysConfigClient sysConfigClient;

    @RequestMapping("/index")
    public String index(Model model) {
        FileResposeCDTO fileResposeDTO = sysConfigClient.getFileConfig("basicPlan");
        model.addAttribute("fileId", fileResposeDTO.getId());
        model.addAttribute("isSupplier", true == LoginUtil.isSupplier() ? 1 : 0);
        return MODULE_ROOT + "/delivery/shortagePlan/list";
    }

    @RequestMapping("/fileLog")
    public String fileLog(Model model) {
        FileResposeCDTO fileResposeDTO = sysConfigClient.getFileConfig("basicPlan");
        model.addAttribute("fileId", fileResposeDTO.getId());
        model.addAttribute("isSupplier", true == LoginUtil.isSupplier() ? 1 : 0);
        return MODULE_ROOT + "/delivery/shortagePlan/plan_file_log_list";
    }

    /**
     * 供应商回复
     *
     * @return 页面
     */
    @GetMapping("/supplier/replyUI")
    public String replyConfirm(Model model, Long id, @RequestParam(defaultValue = "false") boolean isPurchaseReplace) {
        ShortagePlan plan = shortagePlanLogic.selectById(id);
        model.addAttribute("id", id);
        model.addAttribute("plan", plan == null ? new BasicPlan() : plan);
        if (isPurchaseReplace || LoginUtil.isSupplier() == true) {
            model.addAttribute("isSupplier", true);
        } else {
            model.addAttribute("isSupplier", false);
        }
        // 供应商 不可用采购代答复功能
        if (LoginUtil.isSupplier() == true) {
            model.addAttribute("isPurchaseReplace", false);
        } else {
            model.addAttribute("isPurchaseReplace", isPurchaseReplace);
        }
        return MODULE_ROOT + "/delivery/shortagePlan/supplier_reply";
    }


    /**
     * 查看
     *
     * @return 页面
     */
    @GetMapping("/show/date")
    public String showDate(Model model, Long id) {
        model.addAttribute("planId", id);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/delivery/shortagePlan/show_reply_by_date";
    }

    /**
     * 确认回复
     *
     * @return 页面
     */
    @GetMapping("/confirm/replyUI")
    public String confirmReply(Model model, Long id) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/delivery/shortagePlan/confirm_reply";
    }

    /**
     * 发布调整界面
     *
     * @return 调整
     */
    @GetMapping("/release/adjustmentUI")
    public String releaseAdjustment(Model model, Long id) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/delivery/shortagePlan/plan_release_adjustment";
    }

    //导入页面
    @GetMapping("/uploadData")
    public String uploadData(Model model) {
        return MODULE_ROOT + "/delivery/shortagePlan/uploadData";
    }

    /**
     * 查看回复进度
     *
     * @return 页面
     */
    @GetMapping("/look/reply/progress")
    public String lookReplyProgress(Model model, Long id) {
        model.addAttribute("planId", id);
        return MODULE_ROOT + "/delivery/shortagePlan/look_reply_progress";
    }

    //导入页面
    @GetMapping("/warning")
    public String warning(Model model) {
        List<ShortagePlan> list = shortagePlanLogic.selectPlanList();
        model.addAttribute("planId", CollUtil.isEmpty(list) ? 0 : list.get(0).getId());
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("serialNumber", CollUtil.isEmpty(list) ? 0 : list.get(0).getSerialNumber());
        return MODULE_ROOT + "/delivery/shortagePlan/warning";
    }

}
