package com.yem.em.form;

import com.yem.em.utils.BaseDataFilterUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.form.control.Label;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * @author zhouc
 * @date 2023/7/24 17:06
 * @className EtcmanualHomeEdit
 * @description 设备客户首页
 */
public class EtcmanualHomeEdit  extends AbstractFormPlugin implements ClickListener {
    /**
     * 单据创建后触发，可完善单据数据，修改页面显示
     */
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        initData();
    }

    /**
     * 加载数据 1、随机选择当前组织下四个电子目录展示 2、加载面板背景图片
     *
     * @author longh
     */
    private void initData() {
        setPicset("yem_indexpic", "yem_indexpic");
        setPicset("yem_indexpic1", "yem_indexpic1");
        setPicset("yem_indexpic2", "yem_indexpic2");
        setPicset("yem_indexpic3", "yem_indexpic3");
        setPicset("yem_indexpic4", "yem_indexpic4");


        setPicset("yem_carpic1", "yem_carpic1");
        setPicset("yem_carpic2", "yem_carpic2");
        setPicset("yem_carpic3", "yem_carpic3");
        setPicset("yem_carpic4", "yem_carpic4");
//        /**
//         * 车型-设备电子目录展示
//         */
//        List<QFilter> qFilters = new ArrayList<>();
//        qFilters.add(new QFilter("enable", QCP.equals, "1"));// 可用
//        qFilters.add(new QFilter("status", QCP.equals, "C"));// 审核
//        qFilters.add(BaseDataFilterUtils.getBaseDataFilter("tpv_em_etcmanual", RequestContext.get().getOrgId()));// 当前登录用户组织
//        DynamicObject[] bills = BusinessDataServiceHelper.load("tpv_em_etcmanual", "id,tpv_brand",
//                qFilters.toArray(new QFilter[qFilters.size()]));
//        Map<String, String> brand = new HashMap<>();
//        for (DynamicObject bill : bills) {
//            DynamicObject tp_brand = bill.getDynamicObject("tpv_brand");
//            if (tp_brand != null && !brand.containsKey(tp_brand.getPkValue().toString())) {
//                brand.put(tp_brand.getPkValue().toString(), "");
//            }
//        }
//        DynamicObject[] brands = BusinessDataServiceHelper.load("tpv_em_brand", "id,number,name",
//                null);
//        setLabel("tp_tp_labelap8", "" + bills.length);// 车型-数量显示
//        setLabel("tp_labelap8", "" + brands.length);// 品牌-数量显示
////		setLabel("tp_labelap8", "" + brand.size());// 品牌-数量显示
////		bills = createRandoms(bills, 4);
//        bills = BusinessDataServiceHelper.load("tpv_em_etcmanual", "id,tpv_brand",
//                new QFilter[]{new QFilter("number", QCP.in, new String[] {"SD32","QY50KC","CLG6630E","EC220D"})});
//        for (int i = 0; i < bills.length; i++) {
//            DynamicObject bill = bills[i];
//            bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), "tpv_em_etcmanual");
//            setCarInfo(bill, i + 1);
//        }
//        deleteCarLap(bills.length);
//        /**
//         * 设备档案-数量显示
//         */
//        qFilters.clear();
//        qFilters.add(new QFilter("enable", QCP.equals, "1"));// 可用
//        qFilters.add(new QFilter("status", QCP.equals, "C"));// 审核
//        qFilters.add(BaseDataFilterUtils.getBaseDataFilter("tpv_em_devicefiles", RequestContext.get().getOrgId()));// 当前登录用户组织
//        DynamicObject[] tpv_em_devicefiles = BusinessDataServiceHelper.load("tpv_em_devicefiles", "id",
//                qFilters.toArray(new QFilter[qFilters.size()]));
//        setLabel("tp_tp_labelap82", "" + tpv_em_devicefiles.length);// 设备档案-数量显示
    }

    /**
     * 设置常用图片
     *
     * @param key
     * @param picNum
     *
     * @author longh
     */
    private void setPicset(String key, String picNum) {
        DynamicObject pic = BusinessDataServiceHelper.loadSingle("yem_em_picset", "id,yem_picture,number,name",
                new QFilter[] { new QFilter("number", QCP.equals, picNum) });
        if (pic != null) {
            this.getModel().setValue(key, pic.getString("yem_picture"));
        }
    }
    /**
     * 标签赋值
     *
     * @param key
     * @param value
     *
     * @author longh
     */
    private void setLabel(String key, String value) {
        Label label = this.getControl(key);
        if (value != null) {
            label.setText(value);
        }
    }
}
