<!-- eslint-disable vue/no-mutating-props -->
<!--
 * @Author: Devin
 * @Date: 2022-11-15 11:30:12
 * @LastEditors: Devin
 * @LastEditTime: 2023-02-17 16:25:12
 * @Description: ts工程开发
-->
<template>
  <el-form-item
    ref="ruleFormRef"
    class="aiot-el-form-item"
    :prop="formItem?.name || ''"
    :rules="formItem?.rules || []"
    :label="formItem?.label || ''"
  >
    <!-- input -->
    <el-input
      v-if="formItem.type === 'input'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></el-input>
    <el-input
      v-if="formItem.type === 'textarea'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      type="textarea"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></el-input>
    <!-- select -->
    <common-select
      v-if="formItem.type === 'select'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></common-select>
    <!-- number -->
    <el-input-number
      v-if="formItem.type === 'input-num'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></el-input-number>
    <!-- radio -->
    <el-radio
      v-if="formItem.type === 'radio'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      @change="changeEvent(formItem)"
    ></el-radio>
    <!-- cascader -->
    <el-cascader
      v-if="formItem.type === 'cascader'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></el-cascader>
    <!--switch-->
    <el-switch
      v-if="formItem.type === 'switch'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      @change="changeEvent(formItem)"
    ></el-switch>
    <!-- checkbox -->
    <el-checkbox
      v-if="formItem.type === 'checkbox'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      @change="changeEvent(formItem)"
    ></el-checkbox>
    <!-- date-picker -->
    <el-date-picker
      v-if="formItem.type === 'date-picker'"
      v-bind="formItem?.attrs"
      v-model="formItem.value"
      class="__common-form-item"
      @change="changeEvent(formItem)"
    ></el-date-picker>
  </el-form-item>
</template>

<script setup lang="ts">
import { defineEmits, ref } from 'vue';

defineProps({
  formItem: {
    type: Object,
    default: () => {}
  }
});
const emits = defineEmits(['changeEvent']);

// 触发change事件
const changeEvent = (form: any) => {
  emits('changeEvent', form);
};

// 校验规则
const ruleFormRef = ref(null);

function checkRules() {
  ruleFormRef.value!.validate((valid: any) => {
    if (valid) {
      return true;
    }
    return false;
  });
}

defineExpose({
  ruleFormRef,
  checkRules
});
</script>

<style lang="scss">
.aiot-el-form-item {
  width: calc(100% - 24px);
  // margin-bottom: 10 !important;
  margin-bottom: 0 !important;
  margin-top: 18px !important;
}
.__common-form-item {
  width: 100%;
  display: block;
  .el-input__wrapper {
    width: 100%;
  }
}
</style>
