<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="事件编号" v-bind="validateInfos.eventSerialNum">
            <a-input v-model:value="formData.eventSerialNum" placeholder="请输入事件编号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="事件类型" v-bind="validateInfos.eventType">
            <!-- <a-input-number v-model:value="formData.eventType" placeholder="请输入事件类型" style="width: 100%"
              :disabled="disabled" /> -->
            <j-dict-select-tag
              v-model:value="formData.eventType"
              dictCode="ms_warn_kind"
              :string-to-number="true"
              placeholder="请输入事件类型"
              :disabled="disabled"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="信息来源" v-bind="validateInfos.infoOrigin">
            <j-dict-select-tag
              v-model:value="formData.infoOrigin"
              dictCode="ms_warn_type"
              :string-to-number="true"
              placeholder="请选择信息来源"
              :disabled="disabled"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="信息内容" v-bind="validateInfos.infoContent">
            <a-input v-model:value="formData.infoContent" placeholder="请输入信息内容" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="相机id" v-bind="validateInfos.cameraId">
            <a-input v-model:value="formData.cameraId" placeholder="请输入相机id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="相机名称" v-bind="validateInfos.cameraName">
            <a-input v-model:value="formData.cameraName" placeholder="请输入相机名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="发生地点" v-bind="validateInfos.cameraLoc">
            <a-input v-model:value="formData.cameraLoc" placeholder="请输入发生地点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="照片" v-bind="validateInfos.imgUrl">
            <!-- <j-upload v-model:value="formData.imgUrl"  :disabled="disabled" ></j-upload> -->
            <j-image-upload v-model:value="formData.imgUrl" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="录像" v-bind="validateInfos.videoUrl">
            <a-input v-model:value="formData.videoUrl" placeholder="请输入录像" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../MsWarnRelevantInfo.api';
  import { Form } from 'ant-design-vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';

  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => {} },
    formBpm: { type: Boolean, default: true },
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    eventSerialNum: '',
    eventType: undefined,
    infoOrigin: undefined,
    infoContent: '',
    cameraId: '',
    cameraName: '',
    cameraLoc: '',
    imgUrl: '',
    videoUrl: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    eventSerialNum: [{ required: true, message: '请输入事件编号!' }],
    eventType: [{ required: true, message: '请输入事件类型!' }],
    infoContent: [{ required: true, message: '请输入信息内容!' }],
    infoOrigin: [{ required: true, message: '请选择信息来源!' }],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(() => {
    if (props.formBpm === true) {
      if (props.formData.disabled === false) {
        return false;
      } else {
        return true;
      }
    }
    return props.formDisabled;
  });

  /*
   * 生成一个由日期，时间和4位随机数组成的代码
   */
  function dateAndRandomNum() {
    var day2 = new Date();
    // 获取年份
    var s2 = day2.getFullYear() + '';
    // 获取两位数月份
    if (day2.getMonth() + 1 < 10) {
      s2 = s2 + '0' + (day2.getMonth() + 1) + '';
    } else {
      s2 = s2 + (day2.getMonth() + 1) + '';
    }
    // 获取两位数日期
    if (day2.getDate() < 10) {
      s2 = s2 + '0' + day2.getDate() + '';
    } else {
      s2 = s2 + day2.getDate() + '';
    }
    // 获取两位数小时
    if (day2.getHours() < 10) {
      s2 = s2 + '0' + day2.getHours() + '';
    } else {
      s2 = s2 + day2.getHours() + '';
    }
    // 获取两位数分钟
    if (day2.getMinutes() < 10) {
      s2 = s2 + '0' + day2.getMinutes() + '';
    } else {
      s2 = s2 + day2.getMinutes() + '';
    }
    // 获取两位数秒
    if (day2.getSeconds() < 10) {
      s2 = s2 + '0' + day2.getSeconds() + '';
    } else {
      s2 = s2 + day2.getSeconds() + '';
    }
    // 生成4位随机数
    for (let i = 0; i < 4; i++) {
      s2 = s2 + Math.floor(Math.random() * 10);
    }
    return s2;
  }
  /**
   * 新增
   */
  function add() {
    edit({
      eventSerialNum: dateAndRandomNum(),
    });
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }

  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
