package com.eversec.kit.creator.impl.page;

import com.eversec.common.Kv;
import com.eversec.kit.creator.ICreator;
import com.eversec.kit.creator.impl.page.PageKit;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

public class VueKit extends PageKit {

    @Override
    public File getFile(String clazz) {
        return new File(filePath(clazz) + "/kit/" + ICreator.toLowerCaseFirst(clazz) + ".vue");
    }

    @Override
    public String createStr(String clazz, List<Map> fieldList, Map para) {
        List<String> sc = new ArrayList<>();
        List<Map> filterList = new ArrayList<>();
        List<String> detailsList = new ArrayList<>();
        List<Map> editsList = new ArrayList<>();
        fieldList.forEach(x -> {
            x.put("field", (x.get("field") + "").trim());
            x.put("isMust", isMust(x));
            x.put("fieldType", getFiledType(x.get("cate")+""));
            x.put("fieldV",createFiled(x));
            if ("1".equals(x.get("column"))){
                sc.add((x.get("field") + "").trim());
            }
            if ("1".equals(x.get("filter"))){
                Map map = new HashMap();
                map.put("name",(x.get("field") + "").trim());
                map.put("label",x.get("remark1"));
                map.put("col","EQUAL");
                map.put("remark","等于");
                filterList.add(map);
            }
            if ("like".equals(x.get("filter"))){
                Map map = new HashMap();
                map.put("name",(x.get("field") + "").trim());
                map.put("label",x.get("remark1"));
                map.put("col","LIKE");
                map.put("remark","LIKE");
                filterList.add(map);
            }
            if ("1".equals(x.get("xqzs"))){
                detailsList.add((x.get("field") + "").trim());
            }
            if ("1".equals(x.get("edit"))){
                Map map = new HashMap();
                if ("id".equals((x.get("field") + "").trim())){
                    map.put("col",(x.get("field") + "").trim());
                    map.put("upAttr","hidden");
                    map.put("addAttr","none");

                }else {
                    map.put("col",(x.get("field") + "").trim());
                    if ("1".equals(x.get("ck"))){
                        map.put("ck",(x.get("field") + "").trim());
                    }
                }
                editsList.add(map);
            }

        });
        Kv kv = createTplData(clazz);
        kv.set("remark", para.get("remark"));
        kv.set("fieldList", fieldList);
        kv.set("showCol",sc);
        kv.set("fs",filterList);
        kv.set("detailsList",detailsList);
        kv.set("editsList",editsList);
        kv.set("conditionLabel", getConditionLabel(fieldList, para));

        return engine.getTemplate(tplPath + "front/VueTpl.vue").renderToString(kv);
    }

    protected String createFiled(Map x){
        String res = "";
        if ("2".equals(String.valueOf(x.get("edit")))) {
            res = x.get("field") + "||" + x.get("selects");
        } else if ("SELECT".equalsIgnoreCase(String.valueOf(x.get("tag")))) {
            res = x.get("field") + "=" + x.get("field");
        } else if ("SELECT_EXT".equalsIgnoreCase(String.valueOf(x.get("tag")))) {
            res = x.get("field") + "|" + x.get("selects");
        } else if ("INPUT_DT".equalsIgnoreCase(String.valueOf(x.get("tag")))) {
            res = x.get("field") + "=dt";
        } else if ("FILE_EXT".equalsIgnoreCase(String.valueOf(x.get("tag")))) {
            res = x.get("selects") + "";
        } else {
            res = x.get("field") + "";
        }
        return res;
    }

    @Override
    public String filePath(String clazz) {
        return dirPath;
    }

    //获取设置了关键字查询的 字段“描述”
    private String getConditionLabel(List<Map> fieldList, Map para) {
        StringBuilder buf = new StringBuilder();
        if (para.get("condition") != null){
            List<String> conditions = asList((String[]) para.get("condition"));
            fieldList.forEach(m->{
                for (String str : conditions) {
                    //字段有大小写转换，这里需要使用 equalsIgnoreCase 进行比较
                    if ((m.get("field")+"").trim().equalsIgnoreCase(str)) {
                        buf.append(m.get("remark1")+"/");
                    }
                }
            });
        }
        if (buf.length() > 0){
            buf.deleteCharAt(buf.length()-1);
        }
        return buf.toString();
    }
}
