package com.jwipc.tender.controller.pc;

import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.tender.constant.ModuleConst;
import com.jwipc.tender.dto.QuotaManagementDTO;
import com.jwipc.tender.logic.QuotaManagementLogic;
import com.jwipc.tender.model.query.QuotaManagementQuery;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
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 java.util.List;

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

/**
 * @author Al
 * @create 2024-03-02 18:01
 */
@Controller
@RequestMapping(MODULE_ROOT + "/quotaManage/quotaManagement")
@RequiredArgsConstructor
public class QuotaManagementPageController {
    private final QuotaManagementLogic quotaManagementLogic;
    //配额维护主页
    @GetMapping("/index")
    public String index() {
        return MODULE_ROOT + "/quotaManage/quotaManagement/list";
    }
    @AuditLogInter(value = "新增/编辑查看详情", module = "配额维护")
    @GetMapping("/detail")
    public String detail(Model model, Long id,String quotaCode, String materialCode, Integer flag) {
//        MoldFormDTO moldForm;
//        moldForm = moldFormLogic.getById(id);
//        model.addAttribute("quotaManagement", quotaManagement);
        // 新增 type = 1
        // 编辑 type = 2
        // 查看 type = 3
        if (StringUtils.isNotEmpty(quotaCode)) {
            model.addAttribute("quotaCode", quotaCode);
            model.addAttribute("materialCode", materialCode);
//            QuotaManagementDTO quotaAgreementDTO;
//            quotaAgreementDTO = quotaManagementLogic.findByQuery(quotaManagementQuery);
            QuotaManagementQuery quotaManagementQuery = new QuotaManagementQuery();
            quotaManagementQuery.setQuotaCode(quotaCode);
            List<QuotaManagementDTO> quotaList = quotaManagementLogic.findByQuery(quotaManagementQuery);
            if (CollectionUtils.isNotEmpty(quotaList)) {
                model.addAttribute("reason", quotaList.get(0).getReason());
                model.addAttribute("factoryCode", quotaList.get(0).getFactoryCode());
            }
        }
        model.addAttribute("lookFlag", flag==2?1:0);
        if (flag==2){
            flag=1;
        }
        if (flag == 0) {
            return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/detail";
        } else if (flag == 1) {
            return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/edit";
        } else {
            return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/look";
        }
        //return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/detail";
    }

    @GetMapping("/look")
    public String look(Model model,String quotaCode) {
        if (StringUtils.isNotEmpty(quotaCode)) {
            model.addAttribute("quotaCode", quotaCode);
            QuotaManagementQuery quotaManagementQuery = new QuotaManagementQuery();
            quotaManagementQuery.setQuotaCode(quotaCode);
            List<QuotaManagementDTO> quotaList = quotaManagementLogic.findByQuery(quotaManagementQuery);
            if (CollectionUtils.isNotEmpty(quotaList)) {
                model.addAttribute("reason", quotaList.get(0).getReason());
                model.addAttribute("factoryCode", quotaList.get(0).getFactoryCode());
            }
        }
        model.addAttribute("lookFlag", 1);
        return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/detail";
    }
    /**
     * 新增配额界面
     *
     * @return 页面
     */
    @GetMapping("edit")
    public String getQuotaDetailPath(String quotaCode, String materialCode, Integer flag, Model model) {
        // 新增 type = 1
        // 编辑 type = 2
        // 查看 type = 3
        if (StringUtils.isNotEmpty(quotaCode)) {
            model.addAttribute("quotaCode", quotaCode);
            model.addAttribute("materialCode", materialCode);
            QuotaManagementQuery quotaManagementQuery = new QuotaManagementQuery();
            quotaManagementQuery.setQuotaCode(quotaCode);
            List<QuotaManagementDTO> quotaList = quotaManagementLogic.findByQuery(quotaManagementQuery);
            if (CollectionUtils.isNotEmpty(quotaList)) {
                model.addAttribute("reason", quotaList.get(0).getReason());
            }
        }
        if (flag == 1) {
            return  ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/detail";
        } else if (flag == 2) {
            return ModuleConst.MODULE_ROOT + "/quotaManage/quotaManagement/edit";
        } else {
            return "quotaManagement/maintain/look";
        }
    }
}
