package com.yinet.web.controller.touchscreen;

import com.yinet.common.config.Global;
import com.yinet.common.config.ServerConfig;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.utils.QRCodeUtils;
import com.yinet.common.utils.ServletUtils;
import com.yinet.system.domain.GuidanceFile;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.service.IGuidanceFileService;
import com.yinet.system.service.TouchScreenGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 微信端办事指南
 * z
 *
 * @author yient
 */
@Controller
@RequestMapping("/wx/touchscreen")
public class WXBaseWebController extends BaseController {
    static String touchIndex = "touchscreen/wxweb/";
    @Autowired
    private TouchScreenGuidanceService touchScreenGuidanceService;
    @Autowired
    private IGuidanceFileService guidanceFileService;

    /**
     * 首页列表
     *
     * @return
     */

    @GetMapping(value = "/")
    public String wxIndex(){
        return touchIndex +  "zhengding";
    }



    @GetMapping(value = "/{zName}")
    public String Index(@PathVariable("zName") String zName, Model model) {
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectParentGuidance(zName);
        model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "list";
    }
/* public String Index(@PathVariable("id") String id,Model model) {
       List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectParentGuidanceList();
        model.addAttribute("guidanceList", guidanceList);
     //   String parentId="0";
     //   List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByZNameFindAll(id,parentId);
     //   model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "list";
    }*/
    /**
     * 科室指南视图
     *
     * @return
     */
    @GetMapping(value = "/itemlist/{id}")
    public String ItemList(@PathVariable("id") String id, Model model) {
        TouchScreenGuidance touchScreenGuidance = touchScreenGuidanceService.selectGuidanceById(id);
        model.addAttribute("guidance", touchScreenGuidance);
        return touchIndex + "itemlist";
    }


    /**
     * 科室指南列表
     *
     * @return
     */
    @PostMapping("/itemlist/{id}")
    @ResponseBody
    public TableDataInfo GetNoticeList(@PathVariable("id") String id) {
        startPage();
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListById(id);
        return getDataTable(guidanceList);
    }

    /**
     * 科室指南列表
     *
     * @return
     */
    @GetMapping("/itemlist/viewDoc/{id}")
    public String viewDoc(@PathVariable("id") String id, Model model) {
        GuidanceFile guidanceFile = guidanceFileService.selectGuidanceFileById(id);
        model.addAttribute("guidance", guidanceFile);
        String url = Global.getKKFileView();
        model.addAttribute("kkview", url);
        return touchIndex + "viewdoc";
    }


    /**
     * 查看文档
     *
     * @return
     */
    @GetMapping(value = "/itemlist/viewdoc/{type}/{id}")
    public String viewDocByType(@PathVariable("id") String id, @PathVariable("type") String type, Model model) throws IOException {
        GuidanceFile temp = new GuidanceFile();
        temp.setFileType(type);
        temp.setGuidanceId(id);
        TouchScreenGuidance touchScreenGuidance = touchScreenGuidanceService.selectGuidanceById(id);
        List<GuidanceFile> guidanceFiles = guidanceFileService.selectGuidanceFileList(temp);
        if (guidanceFiles.size() == 1) {
            GuidanceFile guidanceFile = guidanceFiles.get(0);
            String domain = ServerConfig.getDomain(ServletUtils.getRequest());
            String qrUrl = domain + guidanceFile.getFileUrl();
            String s = QRCodeUtils.generalQRCode(qrUrl);
            guidanceFile.setQrCode(s);
            model.addAttribute("guidance", guidanceFile);
            String url = Global.getKKFileView();
            model.addAttribute("kkview", url);
            return touchIndex + "viewdoc";
        } else {
            model.addAttribute("guidanceName",touchScreenGuidance);
            model.addAttribute("guidanceFiles", guidanceFiles);
            return touchIndex + "itemlist2";
        }
    }

}
