<template>
  <el-card v-if="props.modelValue" class="pop-card">
    <template #header>
      <div class="flex justify-between items-center pop-head">
        <span class="pop-head-name">编辑相机</span>
        <el-icon :size="16" class="mr-3" @click="updateValue(false)"><Close /></el-icon>
      </div>
    </template>
    <div class="pop-content flex justify-center items-center">
      <NVRAddCamera
        :form-data="props.editData"
        @cancel-execute="updateValue(false)"
        @confirm-execute="onEditCamera"
      />
    </div>
  </el-card>
</template>

<script setup lang="ts">
  import { Close } from '@element-plus/icons-vue';
  import NVRAddCamera from './AddCameraByNVR.vue';
  import { CameraNVRItem } from '../type';
  import useCameraOverview from '../stores/useCameraOverview';
  import { ElMessage } from 'element-plus';

  const props = defineProps<{
    modelValue: boolean;
    editData?: CameraNVRItem | null;
    confirmEvent?: (data) => any;
  }>();

  const emits = defineEmits(['update:modelValue']);

  const cameraOverview = useCameraOverview();
  const { editCamera, getCameraItems } = cameraOverview;

  const updateValue = (value) => {
    emits('update:modelValue', value);
  };

  const onEditCamera = (data) => {
    const temp = {
      id: data.id,
      name: data.name,
      nvrIp: data.nvrIp,
      cameraPort: data.cameraPort,
      cameraType: data.cameraType,
      nvrUsername: data.nvrUsername,
      nvrPassword: data.password,
      workspaceId: data.workspaceId,
      code: data.code,
      remark: data.remark,
      nvrChannel: data.nvrChannel,
      videoServiceType: data.videoServiceType,
      videoStandard: data.videoStandard,
    };
    editCamera(temp)
      .then((res) => {
        if (res.code === 200) {
          updateValue(false);
          ElMessage({
            message: '修改成功',
            type: 'success',
          });
          getCameraItems();
        } else {
          return Promise.reject(res.msg);
        }
      })
      .catch((error) => {
        ElMessage.error(error);
      });
    // props.confirmEvent && props.confirmEvent(data);
    // updateValue(false);
  };
</script>

<style scoped lang="scss">
  .pop-card {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
  }

  .pop-head {
    height: 56px;

    &-name {
      margin-left: 24px;
      font-size: 16px;
      font-weight: 500;
      color: #252525;
    }
  }

  .pop-content {
    height: 566px;
  }

  :deep(.el-card__header) {
    padding: 0;
  }
  :deep(.el-card__body) {
    padding: 0;
  }
</style>
