package com.jfinal.club.ws;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
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.Nation;
import com.jfinal.club.common.model.Wsinfo;
import com.jfinal.club.common.model.Wsman;
import com.jfinal.club.common.upload.UploadService;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club.ws
 * @ClassName: WsInfoController
 * @Author: Jzdsh
 * @Description: 文书信息
 * @Date: 2020/4/14 15:04
 * @Version: 1.0
 */
@Before(FrontAuthInterceptor.class)
public class WsInfoController extends BaseController {
    @Inject
    DocWsService docWsService;
    @Inject
    WsInfoService wsInfoService;
    @Inject
    WsmanService wsmanService;

    public void index( ) {
        Wsinfo wsinfo = wsInfoService.createWs(getloginUsersId( ));
        setAttr("wsinfo", wsinfo);
        render("ws_1.html");
    }


    public void ws1( ) {
        boolean wsType = Wsinfo.dao.findById(getInt("wsinfo.id")).set("wsType", get("wsinfo.wsType")).removeNullValueAttrs( ).update( );
        if (wsType) {
            renderJson(Ret.ok("data", getInt("wsinfo.id")));
        } else {
            renderJson(Ret.fail( ));
        }
    }

    public void editws2( ) {
        //查询原告信息
        List< Wsman > wsmanList = wsmanService.listByWsIdYgbg(getInt("id"), Cons.ONE);
        List< Nation > nations = docWsService.saveNation( );
        setAttr("wsmanList", wsmanList);
        setAttr("nations", nations);
        setAttr("wsid", getInt("id"));
        render("ws_2.html");
    }

    public void editws3( ) {
        //查询被告信息
        List< Wsman > wsmanList = wsmanService.listByWsIdYgbg(getInt("id"), Cons.TWO);
        List< Nation > nations = docWsService.saveNation( );
        setAttr("wsmanList", wsmanList);
        setAttr("nations", nations);
        setAttr("wsid", wsmanList.get(0).getWsId( ));
        render("ws_3.html");
    }

    public void editws4( ) {
        //提交文书界面
        List< String > list = null;
        Wsinfo wsinfo = Wsinfo.dao.findById(getInt("id"));
        if (StringUtils.isNotBlank(wsinfo.getImg( ))) {
            list = Arrays.asList(wsinfo.getImg( ).split(","));
        }
        setAttr("imgList", list);
        setAttr("wsinfo", wsinfo);
        render("ws_4.html");
    }

    @Before(DocWs2Validator.class)
    public void ws2( ) {
        HttpServletRequest request = getRequest( );
        String wsid = get("wsid");
        List< String > id = Arrays.asList(request.getParameterValues("wsman.id"));
        List< String > mtype = Arrays.asList(request.getParameterValues("wsman.mtype"));
        List< String > name = Arrays.asList(request.getParameterValues("wsman.name"));
        List< String > gender = Arrays.asList(request.getParameterValues("wsman.gender"));
        List< String > birthday = Arrays.asList(request.getParameterValues("wsman.birthday"));
        List< String > nation = Arrays.asList(request.getParameterValues("wsman.nation"));
        List< String > hujiAddress = Arrays.asList(request.getParameterValues("wsman.hujiAddress"));
        List< String > phone = Arrays.asList(request.getParameterValues("wsman.phone"));
        List< String > idType = Arrays.asList(request.getParameterValues("wsman.idType"));
        List< String > idNumber = Arrays.asList(request.getParameterValues("wsman.idNumber"));

        List< String > company = Arrays.asList(request.getParameterValues("wsman.company"));
        List< String > legalPerson = Arrays.asList(request.getParameterValues("wsman.legalPerson"));
        List< String > companyAddress = Arrays.asList(request.getParameterValues("wsman.companyAddress"));
        List< String > creditCode = Arrays.asList(request.getParameterValues("wsman.creditCode"));
        List< String > legalPhone = Arrays.asList(request.getParameterValues("wsman.legalPhone"));


        List< Wsman > wsmenList = new ArrayList<>( );
        if (id != null && id.size( ) > 0) {
            for (int i = 0; i < id.size( ); i++) {
                Wsman w = new Wsman( );
                w.setId(Integer.valueOf(id.get(i)));
                w.setMtype(mtype.get(i));
                w.setName(name.get(i));
                w.setGender(gender.get(i));
                w.setBirthday(birthday.get(i));
                w.setNation(nation.get(i));
                w.setHujiAddress(hujiAddress.get(i));
                w.setPhone(phone.get(i));
                w.setIdType(idType.get(i));
                w.setIdNumber(idNumber.get(i));

                w.setCompany(company.get(i));
                w.setLegalPerson(legalPerson.get(i));
                w.setCompanyAddress(companyAddress.get(i));
                w.setCreditCode(creditCode.get(i));
                w.setLegalPhone(legalPhone.get(i));
                wsmenList.add(w);
            }
        }
        Db.batchUpdate(wsmenList, 200);
        renderJson(Ret.ok("data", wsid));
    }

    public void ws22( ) {
       ws2();
    }


    @Before(DocWs3Validator.class)
    public void ws3( ) {
        HttpServletRequest request = getRequest( );
        String wsid = get("wsid");
        List< String > id = Arrays.asList(request.getParameterValues("wsman.id"));
        List< String > mtype = Arrays.asList(request.getParameterValues("wsman.mtype"));
        List< String > name = Arrays.asList(request.getParameterValues("wsman.name"));
        List< String > gender = Arrays.asList(request.getParameterValues("wsman.gender"));
        List< String > birthday = Arrays.asList(request.getParameterValues("wsman.birthday"));
        List< String > nation = Arrays.asList(request.getParameterValues("wsman.nation"));
        List< String > hujiAddress = Arrays.asList(request.getParameterValues("wsman.hujiAddress"));
        List< String > nowAddress = Arrays.asList(request.getParameterValues("wsman.nowAddress"));
        List< String > phone = Arrays.asList(request.getParameterValues("wsman.phone"));
        List< String > idType = Arrays.asList(request.getParameterValues("wsman.idType"));
        List< String > idNumber = Arrays.asList(request.getParameterValues("wsman.idNumber"));

        List< String > company = Arrays.asList(request.getParameterValues("wsman.company"));
        List< String > legalPerson = Arrays.asList(request.getParameterValues("wsman.legalPerson"));
        List< String > companyAddress = Arrays.asList(request.getParameterValues("wsman.companyAddress"));
        List< String > creditCode = Arrays.asList(request.getParameterValues("wsman.creditCode"));
        List< String > legalPhone = Arrays.asList(request.getParameterValues("wsman.legalPhone"));
        List< Wsman > wsmenList = new ArrayList<>( );
        if (id != null && id.size( ) > 0) {
            for (int i = 0; i < id.size( ); i++) {
                Wsman w = new Wsman( );
                w.setId(Integer.valueOf(id.get(i)));
                w.setMtype(mtype.get(i));
                w.setName(name.get(i));
                w.setGender(gender.get(i));
                w.setBirthday(birthday.get(i));
                w.setNation(nation.get(i));
                w.setHujiAddress(hujiAddress.get(i));
                w.setNowAddress(nowAddress.get(i));
                w.setPhone(phone.get(i));
                w.setIdType(idType.get(i));
                w.setIdNumber(idNumber.get(i));

                w.setCompany(company.get(i));
                w.setLegalPerson(legalPerson.get(i));
                w.setCompanyAddress(companyAddress.get(i));
                w.setCreditCode(creditCode.get(i));
                w.setLegalPhone(legalPhone.get(i));
                wsmenList.add(w);
            }
        }
        Db.batchUpdate(wsmenList, 200);
        renderJson(Ret.ok("data", wsid));
    }

    /**
     * 不加表单验证而已
     */
    public void ws33( ) {
        ws3( );
    }



    @Before(DocWs4Validator.class)
    public void ws4( ) {

        String subImg = null;
        String img = Arrays.toString(getRequest( ).getParameterValues("img"));
        if(StringUtils.isNotBlank(img) && !img.equals("null") && !img.equals("[]")){
            subImg = img.substring(1, img.lastIndexOf("]"));
        }

        Wsinfo createAt = getModel(Wsinfo.class).set("createAt", new Date( )).set("status",Cons.ONE);
        if(StringUtils.isNotBlank(subImg)){
            createAt.setImg(subImg.replace(" ",""));
        }
        boolean update = createAt.removeNullValueAttrs( ).update( );
        if (update) {
            renderJson(Ret.ok( ));
        } else {
            renderJson(Ret.fail( ));
        }
    }
    public void ws44(){
        ws4();
    }


    //原告add
    public void ygadd( ) {
        Integer wsid = getInt("wsid");
        Wsman wsman = wsmanService.defaultAdd(wsid, Cons.ONE);
        if (wsman != null) {
            renderJson(Ret.ok("msg", "添加成功！"));
        } else {
            renderJson(Ret.fail("msg", "添加失败"));
        }
    }

    //被告add
    public void bgadd( ) {
        Integer wsid = getInt("wsid");
        Wsman wsman = wsmanService.defaultAdd(wsid, Cons.TWO);
        if (wsman != null) {
            renderJson(Ret.ok("msg", "添加成功！"));
        } else {
            renderJson(Ret.fail("msg", "添加失败"));
        }
    }


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

    /**
     * 文件上传
     */
    @Clear
    public void fileUpload( ) {
        List< UploadFile > file1 = getFiles("file");

        if (file1 == null) {
            renderJson(Ret.fail( ));
        } else {
            for (UploadFile uploadFile : file1) {
                Integer wsid = getInt("wsid");
                Wsinfo wsinfo = Wsinfo.dao.findById(wsid);
                String img1 = wsinfo.getImg( );
                Ret ret = UploadService.uploadAvatar(getloginUsersId( ), uploadFile, Cons.WS_IMG);
                String path = ret.getStr("img");
                //等空表示是非图片上传 不保存
                if (StringUtils.isNotBlank(path)) {
                    if (StringUtils.isNotBlank(img1)) {
                        wsinfo.setImg(img1 + "," + path);
                        wsinfo.removeNullValueAttrs( ).update( );
                    } else {
                        wsinfo.setImg(path);
                        wsinfo.removeNullValueAttrs( ).update( );
                    }
                }
            }
            renderJson(Ret.ok( ));
        }
    }

    /**
     * 原告类型 个人
     */
    public void geren(){
        Wsman wsman = wsmanService.getOneById(getInt("id"));
        if(wsman.getMtype().equals("个人") || wsman.getMtype().equals("0")){
            renderJson(Ret.fail());
        }else{
            wsman.setMtype("个人");
            wsman.removeNullValueAttrs().update();
            renderJson(Ret.ok());
        }
    }
    /**
     * 原告类型 非个人
     */
    public void feigeren(){
        Wsman wsman = wsmanService.getOneById(getInt("id"));
        if(wsman.getMtype().equals("非个人")){
            renderJson(Ret.fail());
        }else{
            wsman.setMtype("非个人");
            wsman.removeNullValueAttrs().update();
            renderJson(Ret.ok());
        }
    }

}
