package com.jfinal.club.ws;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.interceptor.FrontAuthInterceptor;
import com.jfinal.club.common.model.Docws;
import com.jfinal.club.common.model.Nation;
import com.jfinal.club.my.doc.UsersDocService;
import com.jfinal.kit.Ret;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.sun.tools.javac.jvm.ByteCodes.ret;

/**
 * @ClassName DocWs
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/24 15:10
 */
@Before(FrontAuthInterceptor.class)
public class DocWsController extends BaseController {

    @Inject
    DocWsService docWsService;


    public void index( ) {
        int usersId = getloginUsersId( );
        Docws docws = docWsService.getDocwsByFinish(usersId, 0);
        if (docws == null) {
            docws = new Docws( );
            docws.setYtype("0");
            docws.setBtype("0");
            docws.setUsersid(usersId);
            docws.setStatus(0);
            docws.setFinish(0);
            docws.removeNullValueAttrs( ).save( );
        }
        setAttr("docwsById", docws);
        render("ws_1.html");
    }

    public void editws2( ) {
        comEditws( );
        render("ws_2.html");
    }

    public void editws3( ) {
        comEditws( );
        render("ws_3.html");
    }

    public void editws4( ) {
        comEditws( );
        render("ws_4.html");
    }

    private void comEditws( ) {
        setAttr("id", getParaToInt("id"));
        Integer id = getParaToInt("id");
        //民族
        setAttr("nations", docWsService.saveNation( ));
        if (id == null) {
            redirect("/ws");
            return;
        }
        setAttr("docwsById", docWsService.getDocwsById(id));
    }

    public void ws1( ) {
        getModel(Docws.class).set("levels", getModel(Docws.class).getLevels( ).intValue( ) == 1 ? null : 1).removeNullValueAttrs( ).update( );
        renderJson(Ret.ok("data", get("id")));
    }

    @Before(DocWs2Validator.class)
    public void ws2( ) {
        Docws docws = getModel(Docws.class);
        Docws d = docWsService.getDocwsById(docws.getId( ));
        Integer levels = d.getLevels( );
        if(levels == 1){
            docws.setLevels(2);
        }else{
            docws.setLevels(null);
        }
        docws.removeNullValueAttrs( ).update( );
        renderJson(Ret.ok("data", get("id")));
    }

    @Before(DocWs3Validator.class)
    public void ws3( ) {
        Docws docws = getModel(Docws.class);
        Docws d = docWsService.getDocwsById(docws.getId( ));
        Integer levels = d.getLevels( );
        if(levels == 2){
            docws.setLevels(3);
        }else{
            docws.setLevels(null);
        }
        docws.removeNullValueAttrs( ).update( );
        renderJson(Ret.ok("data", get("id")));
    }


    @Before(DocWs4Validator.class)
    public void ws4( ) {
        getModel(Docws.class).set("finish", 1).set("addtime", new Date( )).removeNullValueAttrs( ).set("levels", 4).update( );
        renderJson(Ret.ok("data", get("id")));
    }

    public void ws4s(){
        getModel(Docws.class).removeNullValueAttrs( ).set("levels", 4).update( );
    }


    /**
     * @return
     * @Author Cc
     * @Description //第一页
     * @Date 2020/3/24
     * @Param
     **/
    public void wsPro( ) {
        Integer docwsid = getParaToInt("id");
        Docws docwsById = docWsService.getDocwsById(docwsid);
        setAttr("docwsById", docwsById);
        render("ws_1.html");
    }

}
