package com.jwipc.sys.controller.pc;

import com.jwipc.prop.SysProp;
import com.jwipc.sys.logic.FileLogic;
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 javax.servlet.http.HttpServletResponse;

import java.io.IOException;

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

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/file/")
@RequiredArgsConstructor
public class FilePageController {
    private final FileLogic fileLogic;
    private final SysProp sysProp;

    //通用的文件导入页面
    @GetMapping("importData")
    public String importData(String template, Model model) {
        // TODO: 2024/2/1  jiez 需要从系统配置表中获取
        model.addAttribute("templateFileId", 1);
        return MODULE_ROOT + "/file/import_data";
    }

    //通用的文件导入页面（模板取前端）
    @GetMapping("importExcelData")
    public String importExcelData(String path, Model model) {
        model.addAttribute("path", path);
        return MODULE_ROOT + "/file/import_excel_data";
    }

    @GetMapping("importQueryExcelData")
    public String importQueryExcelData(String url, Model model) {
        model.addAttribute("url", url);
        return MODULE_ROOT + "/file/import_query_excel_data";
    }

    @GetMapping("fileUpload")
    public String fileUpload(String optionKey, Model model) {
        model.addAttribute("optionKey", optionKey);
        return MODULE_ROOT + "/file/file_upload";
    }

    @GetMapping("preview")
    public void preview(HttpServletResponse response, Long fileId, String fileName) throws IOException {
        String originUrl = sysProp.getHostUrl()+ "/pc/sys/file/rest/download/" + fileId;
        String previewUrl = originUrl + "&fullfilename="+fileName;
        response.sendRedirect(sysProp.getPreviewUrl() + "?url="+previewUrl);
    }
}
