package com.jfinal.club._admin.ws;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Docws;
import com.jfinal.club.common.model.Wsinfo;
import com.jfinal.club.common.model.Wsman;
import com.jfinal.club.util.Cons;
import com.jfinal.club.ws.DocWsService;
import com.jfinal.club.ws.DocWsValidator;
import com.jfinal.club.ws.WsmanService;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club._admin.ws
 * @ClassName: WsAdminController
 * @Author: Jzdsh
 * @Description: 文书后台管理
 * @Date: 2020/4/2 12:00
 * @Version: 1.0
 */
public class WsAdminController extends BaseController {

    @Inject
    WsAdminService wsAdminService;
    @Inject
    DocWsService docWsService;

    @Inject
    WsmanService wsmanService;

    public void index( ) {
        Page< Wsinfo > page = wsAdminService.page(getInt("p", 1), Cons.ADMIN_PAGE_SIZE, get("keyword"));
        List< Wsinfo > list = page.getList( );
        if (list != null && list.size() > 0){
            list.forEach(a->{
                if(StringUtils.isNotBlank(a.getWsRequest())){
                    if(a.getWsRequest().length() > 60){
                        a.setWsRequest(a.getWsRequest().substring(0,60)+"......");
                    }
                }
            });
        }
        setAttr("wsPage", page);
        render("index.html");
    }

    public void add( ) {
        setAttr("nations", docWsService.saveNation( ));
        render("add_edit.html");
    }

    @Before(DocWsValidator.class)
    public void save( ) {
        boolean save = getBean(Docws.class).set("status", 0).set("finish", 1).set("usersid", 0).removeNullValueAttrs( ).save( );
        if (save) {
            renderJson(Ret.ok("msg", "添加成功!"));
        } else {
            renderJson(Ret.fail("msg", "添加失败!"));
        }
    }

    public void edit( ) {
        List< String > imgList = null;
        Wsinfo ws = Wsinfo.dao.findById(getInt("id"));
        setAttr("wsinfo", ws);
        if(StringUtils.isNotBlank(ws.getImg())){
            imgList = new ArrayList<>(Arrays.asList(ws.getImg( ).split(",")));
        }
        setAttr("imgList",imgList);
        setAttr("nations", docWsService.saveNation( ));
        render("add_edit.html");
    }

    @Before(WsAdminValidator.class)
    public void update( ) {
        String subImg;
        String img = Arrays.toString(getRequest( ).getParameterValues("img"));
        Wsinfo wsinfo = getBean(Wsinfo.class);
        if (StringUtils.isNotBlank(img) && !img.equals("null") && !img.equals("[]")) {
            subImg = img.substring(1, img.lastIndexOf("]"));
            wsinfo.setImg(subImg.replace(" ", ""));
        }
        boolean update = wsinfo.removeNullValueAttrs( ).update( );
        if (update) {
            renderJson(Ret.ok("msg", "修改成功!"));
        } else {
            renderJson(Ret.fail("msg", "修改失败!"));
        }
    }

    /**
     * 文书详情,个人类型 信息
     */
    public void gr_detail_index(){
        List< Wsman > wsmanList = wsmanService.listBymType(getInt("id"),"个人");
        setAttr("wsmanList",wsmanList);
        setAttr("wsId",getInt("id"));
        render("gr_detail_index.html");
    }

    /**
     * 文书详情,非个人类型 信息
     */
    public void fgr_detail_index(){
        List< Wsman > wsmanList = wsmanService.listBymType(getInt("id"),"非个人");
        setAttr("wsmanList",wsmanList);
        render("fgr_detail_index.html");
    }

    /**
     * 跳转到原告被告编辑页面
     */
    public void detail_edit(){
        Wsman wsman = wsmanService.getOneById(getInt("id"));
        setAttr("wsman",wsman);
        setAttr("nations", docWsService.saveNation( ));
        render("detail_edit.html");
    }

    /**
     * 添加原告被告
     */
    public void addygbg(){
        Wsman wsman = wsmanService.defaultAdd(getInt("wsId"), Cons.ONE);
        setAttr("wsman",wsman);
        setAttr("nations", docWsService.saveNation( ));
        render("detail_edit.html");
    }

    /**
     * 更新原告被告信息
     */
    @Before(DetailUpdateValidator.class)
    public void detail_update(){
        boolean update = getModel(Wsman.class).removeNullValueAttrs( ).update( );
        if(update){
            renderJson(Ret.ok("mtype",get("wsman.mtype")));
        }else{
            renderJson(Ret.fail());
        }
    }

    /**
     * 不加验证
     */
    public void detail_updates(){
        detail_update();
    }





    /**
     * 删除原告被告
     */
    public void delete(){
        boolean delete = Wsman.dao.deleteById(getInt("id"));
        if(delete){
            renderJson(Ret.ok("msg","删除成功"));
        }else{
            renderJson(Ret.fail("msg","删除失败"));
        }
    }

}
