package com.ruoyi.web.controller.Ytj;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HyDictionary;
import com.ruoyi.system.service.IYtjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.ruoyi.common.utils.ImgUtils.Base64ToImage;

/**
 * 前台工商个体户设立
 * @author ruoyi
 */
@Controller
@RequestMapping("/Ytj/GsYw")
public class GsYwController extends BaseController {
    static String prefix = "Ytj/GsYw/";
    @Autowired
    private IYtjService ytjService;

    /**
     * 返回主页面
     * @return
     */
    @GetMapping()
    public String Index(ModelMap modelMap) {
        modelMap.put("city",Global.getCityName());
        return "Ytj/allIndex";
    }
    @GetMapping("/canvasbg")
    public String canvasbg() {
        return "Ytj/canvasbg";
    }

    /**
     * 读取身份证页面
     * @return
     */
    @GetMapping("/Read")
    public String Read(Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/Read";
    }

    /**
     * 检测是否认证
     * @return
     */
    @GetMapping("/CheckRz")
    @ResponseBody
    public AjaxResult CheckRz(@RequestParam("card") String card, @RequestParam("name") String name, HttpServletResponse response) {

        List<String> data=ytjService.CheckRz(card,name,response);

        return AjaxResult.success(data);

    }

    /**
     * 身份证信息页面
     * @return
     */
    @GetMapping("/Card")
    public String Card(Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/Card";
    }

    /**
     * 办理须知
     * @return
     */
    @GetMapping("/GsIndex")
    public String GsIndex(Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/GsIndex";

    }

    /**
     * 填写信息
     * @param model
     * @return
     */
    @GetMapping("/Mcxx")
    public String Mcxx(Model model) {
        String BSID= UUID.randomUUID().toString();
        model.addAttribute("BSID",BSID);
        model.addAttribute("city", Global.getCityName());
        model.addAttribute("scity", Global.getSuperCity());
        model.addAttribute("rzdbsocre", Global.getConfig("ruoyi.rzdbsocre"));
        return prefix+"/Mcxx";
    }

    /**
     * 提交成功
     * @return
     */
    @GetMapping("/sl6")
    public String sl6(Model model) {
        model.addAttribute("city", Global.getCityName());
        return prefix+"/sl6";
    }

//-----------------------方法-------------------------------------------------

    /**
     * 提交读取身份证信息
     * @param request
     * @return isok 成功标志位
     */
    @PostMapping(value="/SubmitCard")
    @ResponseBody
    public int SubmitCard( HttpServletRequest request) {

        String sfzm = request.getParameter("sfzm"); //姓名

        String tximg = request.getParameter("tximg");//头像照片
        String sfzh = request.getParameter("sfzh");
        String sfzdz = request.getParameter("sfzdz");
        String sjhm = request.getParameter("sjhm");
        int isok = ytjService.SubmitCard(sfzm, tximg, sfzh, sfzdz, sjhm);
        return isok;
    }

    /**
     * 得到用户的身份id
     * @param hm
     * @return
     */
    @GetMapping(value="/GetUserCard")
    @ResponseBody
    public Map GetUserCard(String hm,Model model) {
        Map map = null;
        try {
            map = ytjService.GetUserCard(hm);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     *查找子级行政区划
     * @return
     */
    @GetMapping(value="/QhList")
    @ResponseBody
    public List<Map> QhList() {
        List<Map> qh = ytjService.GetQh();
        return qh;
    }

    /**
     * 根据pid查找区划
     * @param pid
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetQu")
    @ResponseBody
    public List<Map> GetQu(String pid) throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetQu(pid);
        return map;
    }

    /**
     * 查找行业分类
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetHy")
    @ResponseBody
    public List<Map> GetHy() throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetHy();
        return map;
    }

    /**
     * 查找行业分类子级
     * @param pid
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetChildHy")
    @ResponseBody
    public List<HyDictionary> GetChildHy(String pid) throws UnsupportedEncodingException {
        List<HyDictionary> map = ytjService.GetChildHy(pid);
        return map;
    }

    /**
     * 查询行业名称
     * @param name
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetHymc")
    @ResponseBody
    public List<HyDictionary> GetHymc(String name) throws UnsupportedEncodingException {
        List<HyDictionary> map = ytjService.GetHymc(name);
        return map;
    }

    /**
     * 查找组织形式
     * @param name  组织形式名称
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetZzxx")
    @ResponseBody
    public List<HyDictionary> GetZzxx(String name) throws UnsupportedEncodingException {
        List<HyDictionary> map = ytjService.GetZzxx(name);
        return map;
    }

    /**
     * 检测字号是否有重复存在
     * @param request
     * @return
     */
    @PostMapping(value="/Check")
    @ResponseBody
    public String Check(HttpServletRequest request) {
        String a = request.getParameter("xyzh");
        String b = request.getParameter(a);
        String zwzh1 = request.getParameter("zwzh1");
        String zwzh2 = request.getParameter("zwzh2");
        String zwzh3 = request.getParameter("zwzh3");
        String c = request.getParameter("HY1");
        System.err.println(b+"zw:"+zwzh1+"2:"+zwzh2+"3:"+zwzh3+"c:"+ c);
        String isok = ytjService.Check(b, zwzh1, zwzh2, zwzh3, c);

        System.err.println(isok);
        return isok;
    }

    /**
     * 查找经营范围
     * @param a
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetFw")
    @ResponseBody
    public List<Map> GetFw(String a) throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetFw(a);
        return map;
    }

    /**
     *根据行业id查找
     * @param hyid
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetCailiao")
    @ResponseBody
    public List<Map> GetCailiao(String hyid) throws UnsupportedEncodingException {
        List<Map> map = ytjService.GetCailiao(hyid);
        System.err.println(map);
        return map;
    }

    /**
     * 保存身份证正反面
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value="/SendCailiao")
    @ResponseBody
    public int SendCailiao( HttpServletRequest request) throws UnsupportedEncodingException {
        int rtnResule = 0;
        String bsid = request.getParameter("bsid");
        String clname = request.getParameter("clname");
        String clindex = request.getParameter("clindex");
        String clsrc = request.getParameter("clsrc");
        //把base64转为图片路径
        String imgPath= "/sfzImg/"+ DateUtils.datePaths()+"/" +UUID.randomUUID().toString().replaceAll("-","").toLowerCase()+".jpg";
        if(Base64ToImage(clsrc,Global.getProfile()+imgPath)){
            clsrc="/profile"+imgPath;
            int isok = ytjService.SendCailiao(bsid, clname, clindex, clsrc);
            return isok;
        }
        return 0;
    }

    /**
     * 删除页面身份证照片
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value="/DelCailiao")
    @ResponseBody
    public int DelCailiao( HttpServletRequest request) throws UnsupportedEncodingException {
        int rtnResule = 0;
        String bsid = request.getParameter("bsid");
        String clname = request.getParameter("clname");
        String clindex = request.getParameter("clindex");

        int isok = ytjService.DelCailiao(bsid, clname, clindex);

        return isok;
    }

    /**
     * 提交所有填写信息
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value="/SubmitForm")
    @ResponseBody
    public int SubmitForm(  HttpServletRequest request) throws UnsupportedEncodingException {
        int rtnResule = 0;
        int isok = ytjService.SubmitForm(request);

        return isok;
    }

    /**
     * 查询审核中的人数
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value="/GetShenHeNum")
    @ResponseBody
    public Integer GetShenHeNum() throws UnsupportedEncodingException {
        Integer num = ytjService.GetShenHeNum();
        return num;
    }

    /**
     * 获取已经读取过得身份证信息
     * @param hm
     * @return
     * @throws UnsupportedEncodingException
     */
	@GetMapping(value="/GetCardInfo")
    @ResponseBody
    public Map GetCardInfo(String hm) throws UnsupportedEncodingException {
        Map map = ytjService.GetCardInfo(hm);
        return map;
    }

}
