<!--
@ 作者: 秦东
@ 时间: 2024-09-12 10:25:52
@ 备注: 分值
-->
<script lang="ts" setup>
import {
  formatNumber,
  objectToArray,
  constControlChange,
  constSetFormOptions,
  constFormProps,
  constAiEffect,
  constGetControlByName,
} from "@/api/DesignForm/utils";
import {
  AnalysisCss,
  AnalysisInputCss,
} from "@/components/DesignForm/public/form/calculate/cssInfo.ts";

const props = withDefaults(
  defineProps<{
    data?: Object;
    modelValue?: string;
    disabled?: boolean;
  }>(),
  {}
);
const getControlByName = inject(constGetControlByName) as any;
const emits = defineEmits<{
  (e: "update:modelValue", value: string): void;
}>();
const value = computed({
  get: () => {
    return props.modelValue;
  },
  set: (newVal: any) => {
    emits("update:modelValue", newVal);
  },
});
const config = computed(() => {
  return props.data.config || {};
});

//----------by han2015: support AI event emitting----------------
const changeEvent = inject(constAiEffect, '') as any
function onValueChange(){
  changeEvent &&
    changeEvent({key:props.data.name,value:props.modelValue, field:props.data.item.label})
}
//--------------------------------------
/**
@ 作者: 秦东
@ 时间: 2024-03-01 09:07:11
@ 功能: 布局处理
*/
const getFormItemLableStyle = (ele: any) => {
  if (ele?.labelStyle) {
    // console.log("返回栅格宽度3",AnalysisCss(ele?.labelStyle))
    return AnalysisCss(ele?.labelStyle);
  }
};
const getFormItemInputStyle = (ele: any, sty: number) => {
  if (ele?.inputStyle) {
    //console.log("返回栅格宽度4",AnalysisInputCss(ele?.inputStyle,sty))
    return AnalysisInputCss(ele?.inputStyle, sty);
  }
};
const getInputSlot = (key?: string) => {
  const slot = key === "p" ? config.value.prepend : config.value.append;
  const has = slot.indexOf("key:") === 0;
  if (!has) {
    return false;
  }
  const slotKey = slot.replace("key:", "");
  const control = getControlByName(slotKey);
  if (!control || Object.keys(control)?.length === 0) {
    return false;
  }
  return control;
};

/**
@ 作者: 秦东
@ 时间: 2024-07-27 15:11:42
@ 功能: 判断是否禁用
*/
const judgeIsDisabled = (key: string) => {
  if (type.value === 3) {
    return true; // 查看模式，为不可编辑状态
  }
  if (type.value === 1 && config.value.addDisabled) {
    return true;
  }
  if (type.value === 2 && config.value.editDisabled) {
    return true; // 编辑模式
  }
  if (
    props.nodeKey != undefined &&
    props.purview != undefined &&
    props.purview != null &&
    props.purview != null &&
    props.purview != "" &&
    props.purview != ""
  ) {
    if (props.purview.length < 1) {
      return false;
    } else {
      let isShow = true;
      props.purview.forEach((item) => {
        if (item.nodeKey == props.nodeKey) {
          if (item.powerAry && item.powerAry.length > 0) {
            item.powerAry.forEach((itm) => {
              if (itm.id == key) {
                console.log(
                  "判断此组件是否禁用",
                  itm,
                  itm.id == key,
                  "--------->",
                  itm.isLook
                );
                isShow = !itm.isEdit;
              }
            });
          }
        }
      });
      return isShow;
    }
  } else {
    return false;
  }
};
</script>
<template>
  <el-input
    v-bind="$props"
    v-model="value"
    :style="getFormItemInputStyle(configStyle, 2)"
    :input-style="getFormItemInputStyle(configStyle, 3)"
    @change="onValueChange"
    oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^\./g, '')"
    placeholder="请输入"
  >
    <template #append v-if="config.append">
      <div v-if="getInputSlot()">
        <AKSelect
          :data="getInputSlot()"
          :disabled="judgeIsDisabled(data.name)"
          :transform-option="transformOption"
          type="slot"
        />
      </div>
      <span v-else>{{ config.append }}</span>
    </template>
  </el-input>
</template>
<style lang="scss" scoped></style>
