<template>
  <div>
    <div style="margin-bottom: 120px">
      <el-form
        class="ip-form"
        :inline="true"
        :model="cameraIPData"
        :rules="rules"
        label-width="84px"
        label-position="left"
      >
        <el-form-item
          v-for="item in cameraIPAddForm"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        >
          <el-input
            v-if="item.type === 'input'"
            v-model="cameraIPData[item.prop]"
            :placeholder="item.placeholder"
            style="width: 200px"
            :type="item.prop === 'password' ? 'password' : ''"
            :show-password="item.prop === 'password'"
          />
          <el-select
            v-if="item.type === 'select'"
            v-model="cameraIPData[item.prop]"
            :placeholder="item.placeholder"
            style="width: 200px"
          >
            <el-option
              v-for="protocal in item.option"
              :key="protocal.value"
              :label="protocal.label"
              :value="protocal.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span class="pop-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </div>
</template>

<script setup lang="ts">
  import { computed, onBeforeMount, ref } from 'vue';
  import { CameraIPItem } from '../type';
  import { cameraIPAddForm } from '../constant';

  const props = defineProps<{ formData?: CameraIPItem | null }>();

  const emits = defineEmits(['cancel-execute', 'confirm-execute']);

  const cameraIPData = ref<CameraIPItem>({} as CameraIPItem);

  const rules = computed(() => {
    const newRule = {};
    cameraIPAddForm.forEach((item) => {
      if (item.required) {
        newRule[item.prop] = item.rule;
      }
    });
    return newRule;
  });

  const handleCancel = () => {
    emits('cancel-execute');
  };

  const handleConfirm = () => {
    emits('confirm-execute', cameraIPData.value);
  };

  onBeforeMount(() => {
    if (props.formData) {
      cameraIPData.value = props.formData;
    }
  });
</script>

<style scoped>
  .ip-form {
    width: 768px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content: space-around;
  }

  .pop-footer {
    position: absolute;
    right: 24px;
    bottom: 27px;
    display: flex;
    justify-content: flex-end;
  }

  :deep(.el-form-item__label) {
    font-size: 14px;
    color: #363636;
    padding: 0;
  }
  :deep(.el-form--inline .el-form-item) {
    display: flex;
    margin-right: 0;
    margin-bottom: 28px;
  }
</style>
