package com.yinet.web.controller.touchscreen;
import com.alibaba.fastjson.JSON;
import com.yinet.common.config.Global;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.domain.Stree;
import com.yinet.common.utils.StringUtils;
import com.yinet.common.utils.file.FileUploadUtils;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.service.TouchScreenGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 办事指南信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/touchscreen/guidance")
public class GuidanceAdminController extends BaseController {
    static String prefix = "touchscreen/admin/guidance";

    @Autowired
    private TouchScreenGuidanceService guidanceService;
    @GetMapping()
    public String guidance() {
        return prefix + "/list";
    }
    /**
     * 获取办事指南列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<TouchScreenGuidance> list() {
        List<TouchScreenGuidance> guidanceList = guidanceService.selectGuidanceList();
        return guidanceList;
    }
    /**
     * 删除菜单
     */
    @GetMapping("/remove/{guidanceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("guidanceId") String guidanceId) {
        if (guidanceService.selectCountGuidanceByParentId(guidanceId) > 0) {
            return AjaxResult.warn("存在子项,不允许删除");
        }
        return toAjax(guidanceService.deleteGuidanceById(guidanceId));
    }

    @PostMapping("/uploadDoc")
    @ResponseBody
    public String fileUpload(@RequestParam("docFile") MultipartFile[] files) {
        Map result = new HashMap();
        List<String> filename = new ArrayList<String>();
        List<String> fileurl = new ArrayList<String>();
        for (MultipartFile item : files) {
            try {
                String upload = FileUploadUtils.upload(Global.getGuidanceDocPath(), item);
                fileurl.add(upload);

                filename.add(item.getOriginalFilename().substring(0, item.getOriginalFilename().lastIndexOf(".")));
//                filename.add(upload.substring(upload.lastIndexOf("/") + 1, upload.length()));
                System.out.println("文件名称：" + item.getOriginalFilename());
                System.out.println("上传路径：" + upload);
            } catch (IOException e) {
                return "上传失败";
            }
        }
        result.put("filename", StringUtils.join(filename.toArray(), ","));
        result.put("fileurl", StringUtils.join(fileurl.toArray(), ","));
        return JSON.toJSONString(result);
    }
    @PostMapping("/uploadIcon")
    @ResponseBody
    public String iconFileUpload(@RequestParam("iconFile") MultipartFile file) {
        String iconUrl;
        try {
            iconUrl = FileUploadUtils.upload(Global.getGuidanceIconPath(), file);
        } catch (IOException e) {
            return "上传失败";
        }
        System.out.println("上传路径：" + iconUrl);
        return "{\"iconUrl\":\"" + iconUrl + "\"}";
    }
    /**
     * 新增办事指南
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") String id, ModelMap mmap) {
        TouchScreenGuidance guidance = null;
        if (!id.equals("0")) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new TouchScreenGuidance();
            guidance.setTsGuidanceId("0");
            guidance.setGuidanceName("根目录");
        }
        mmap.addAttribute("guidanceNode", guidance);
        return prefix + "/add";
    }
    /**
     * 新增保存办事指南
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TouchScreenGuidance guidance) {
        if (guidance.getGuidanceType().equals("M") && !guidance.getParentId().equals("0")) {
            return AjaxResult.warn("不允许在目录下创建目录");
        }
        int i = guidanceService.insertGuidance(guidance);
        return toAjax(i);
    }
    /**
     * 编辑办事指南
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id,ModelMap mmap) {
        TouchScreenGuidance touchScreenGuidance = guidanceService.selectGuidanceById(id);
        mmap.addAttribute("guidanceNode", touchScreenGuidance);
        return prefix + "/edit";
    }
    /**
     * 编辑保存办事指南
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TouchScreenGuidance guidance, String delIds) {
        if (guidance.getTsGuidanceId().equals(guidance.getParentId())) {
            return AjaxResult.warn("不允许把自己设置为上级目录");
        } else if (guidance.getParentId().equals(0L) && !guidance.getGuidanceType().equals("M")) {
            return AjaxResult.warn("主目录不允许更改为事项");
        } else if (guidanceService.selectCountGuidanceByParentId(guidance.getTsGuidanceId()) > 0 && !guidance.getGuidanceType().equals("M")) {
            return AjaxResult.warn("存在子项,不允许更改指南类型");
        }
        int i = guidanceService.updateGuidance(guidance, delIds);
        return toAjax(i);
    }
    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/guidanceTreeData")
    @ResponseBody
    public List<Stree> guidanceTreeData() {
        List<Stree> strees = guidanceService.guidanceTreeData();
        return strees;
    }
    /**
     * 选择办事指南树
     */
    @GetMapping("/selectGuidanceTree/{id}")
    public String selectGuidanceTree(@PathVariable("id") String id,ModelMap mmap) {
        TouchScreenGuidance guidance = null;
        if (!id.equals("0")) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new TouchScreenGuidance();
            guidance.setTsGuidanceId("0");
            guidance.setGuidanceName("根目录");
        }
        mmap.put("guidance", guidance);
        return prefix + "/tree";
    }


}