package com.jwipc.spe.controller;

import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.dto.QmNoticeOrderDTO;
import com.jwipc.spe.dto.QmRectifiedMeasuresDTO;
import com.jwipc.spe.logic.QmNoticeOrderLogic;
import com.jwipc.spe.logic.QmRectifiedMeasuresLogic;
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 static com.jwipc.spe.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-02-28 15:36
 * 考核指标
 */
@Controller
@RequestMapping(MODULE_ROOT + "/qualityManage/launchedQualityManage")
@RequiredArgsConstructor
public class QmNoticeOrderPageController {
    private final QmNoticeOrderLogic qmNoticeOrderLogic;
    private final QmRectifiedMeasuresLogic qmRectifiedMeasuresLogic;
    //private final OrgLogic orgLogic;
    //主页面
    @GetMapping("/index")
    public String index() {
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/list";
    }
    //新增详情页
    @GetMapping("/detail")
    public String detail(Model model, Long id) {
        CurrentUserDTO user = LoginUtil.getCurrentUser();
        model.addAttribute("loginName",user.getName());
        if (null!=user.getDeptCode()){
        model.addAttribute("orgName",user.getDeptName());
        }
        QmNoticeOrderDTO qmNoticeOrder;
        qmNoticeOrder = qmNoticeOrderLogic.getById(id);
        model.addAttribute("qmNoticeOrder", qmNoticeOrder);
        QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO = qmRectifiedMeasuresLogic.getByNoticeOrderId(id);
        model.addAttribute("qmRectifiedMeasuresDTO", qmRectifiedMeasuresDTO);
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/detail";
    }

    /**
     * 质量整改审批视图
     */
    @RequestMapping("examine")
    public String confirmUI(Model model, Long id) {
        QmNoticeOrderDTO qmNoticeOrder = new QmNoticeOrderDTO();
        qmNoticeOrder.setId(id);
        QmNoticeOrderDTO supplierQmNoticeOrder = qmNoticeOrderLogic.getById(id);
        model.addAttribute("qmNoticeOrder", supplierQmNoticeOrder);
        QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO = qmRectifiedMeasuresLogic.getByNoticeOrderId(id);
        model.addAttribute("qmRectifiedMeasuresDTO", qmRectifiedMeasuresDTO);
        CurrentUserDTO user = LoginUtil.getCurrentUser();
        //supplierQmNoticeOrder.setDataType("data_type", 1);
        supplierQmNoticeOrder.setId(id);
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/examine";
    }

    /**
     * 质量单打印
     * @param model
     * @param id
     * @return
     */
    @GetMapping("print")
    public String print(Model model, Long id,String noticeOrderCode){
        QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO = qmRectifiedMeasuresLogic.getByNoticeOrderId(id);
        model.addAttribute("qmNoticeOrder", qmNoticeOrderLogic.getById(id));
        model.addAttribute("qmRectifiedMeasuresDTO", qmRectifiedMeasuresDTO);
        model.addAttribute("noticeOrderCode", noticeOrderCode);
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/print_template";
    }

    /**
     * 质量整改审批视图
     */
    @RequestMapping("lookNew")
    public String lookNew(Model model, Long id) {
        QmNoticeOrderDTO qmNoticeOrder = new QmNoticeOrderDTO();
        qmNoticeOrder.setId(id);
        QmNoticeOrderDTO supplierQmNoticeOrder = qmNoticeOrderLogic.getById(id);
        model.addAttribute("qmNoticeOrder", supplierQmNoticeOrder);
        QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO = qmRectifiedMeasuresLogic.getByNoticeOrderId(id);
        model.addAttribute("qmRectifiedMeasuresDTO", qmRectifiedMeasuresDTO);
        CurrentUserDTO user = LoginUtil.getCurrentUser();
        //supplierQmNoticeOrder.setDataType("data_type", 1);
        supplierQmNoticeOrder.setId(id);
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/lookNew";
    }

    @GetMapping("/look")
    public String look(Model model, Long id) {
        CurrentUserDTO user = LoginUtil.getCurrentUser();
        model.addAttribute("loginName",user.getLoginName());
        if (null!=user.getDeptCode()){
            model.addAttribute("orgName",user.getDeptName());
        }
        QmNoticeOrderDTO qmNoticeOrder;
        qmNoticeOrder = qmNoticeOrderLogic.getById(id);
        model.addAttribute("qmNoticeOrder", qmNoticeOrder);
//        Supplier supplier = supplierService.getById(id);
//        model.addAttribute("isexternal", supplier.getIsExternal());
        return MODULE_ROOT + "/qualityManage/launchedQualityManage/look";
    }
}
