package kr.co.jinwoosi.clfd.conts.company.web;

import kr.co.jinwoosi.clfd.conts.company.model.Company;
import kr.co.jinwoosi.clfd.conts.company.service.CompanyService;
import kr.co.jinwoosi.clfd.conts.prod.model.Prod;
import kr.co.jinwoosi.clfd.framework.base.web.BaseController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class CompanyController extends BaseController {

    @Value("#{prop['AppConf.filePath.image']}")
    private String imgUrl;

    @Resource(name = "companyService")
    private CompanyService companyService;

    @RequestMapping(value = "/system/company/list.mng", method = RequestMethod.GET)
    public String list(Model model, Company company) {
        model.addAttribute("companyInfo", companyService.select(company));
        return "system/company/list";
    }

    @RequestMapping(value = "/system/company/list.json", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(Company company) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            result.put("data", companyService.select(company));
            result.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("result", "fail");
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/system/company/update.json", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(@RequestParam(required = false, value = "uploadFile") MultipartFile multipartFile, Company company) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (multipartFile == null || multipartFile.isEmpty()) {
            } else {
                File dirPath = new File(imgUrl);
                if (!dirPath.isDirectory()) {
                    dirPath.mkdir();
                }
                Company fileInfo = companyService.select(company);
                if (fileInfo.getImageDir() != null && fileInfo.getCompanyLogo() != null) {
                    String filePath = imgUrl + fileInfo.getImageDir() + '_' + fileInfo.getCompanyLogo();
                    File deleteFile = new File(filePath);
                    deleteFile.delete();
                }
                String uuidPath = UUID.randomUUID().toString();
                String fileName = multipartFile.getOriginalFilename().replaceAll(" ", "_");
                company.setImageDir(uuidPath);
                company.setCompanyLogo(fileName);

                File file = new File(imgUrl + uuidPath + '_' + fileName);
                try {
                    multipartFile.transferTo(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            company.setUpdId(getUserId());
            companyService.update(company);
            result.put("result", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("result", "fail");
            result.put("message", e.getMessage());
        }
        return result;
    }

}
