<template>
  <div class="overlay" v-if="props.modelValue"></div>
  <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">
      <IPAddCamera
        :form-data="props.editData"
        @cancel-execute="updateValue(false)"
        @confirm-execute="onEditCamera"
      />
    </div>
  </el-card>
</template>

<script setup lang="ts">
  import { ElMessage } from 'element-plus';
  import { Close } from '@element-plus/icons-vue';
  import { CameraDetailServer } from '@/types/camera/type';
  import useCameraOverview from '../stores/useCameraOverview';
  import IPAddCamera from './AddCameraByIP.vue';

  const props = defineProps<{
    modelValue: boolean;
    editData?: CameraDetailServer | 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,
      code: data.code,
      cameraIp: data.cameraIp,
      cameraPort: data.cameraPort,
      username: data.username,
      password: data.password,
      cameraType: data.cameraType,
      videoServiceType: data.videoServiceType,
      sceneTemplateList: data.sceneTemplateList,
      workshopId: data.workshopId,
      workspaceId: data.workspaceId,
      videoStandard: data.videoStandard,
      remark: data.remark,
      sourceType: data.sourceType,
      nvrId: data.nvrId,
    };
    editCamera(temp)
      .then((res) => {
        if (res.code === 200) {
          updateValue(false);
          ElMessage({
            message: '修改成功',
            type: 'success',
          });
          getCameraItems();
        } else {
          return Promise.reject(res.msg || res.message);
        }
      })
      .catch((error) => {
        ElMessage.error(error);
      });
  };
</script>

<style scoped lang="scss">
  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
  }
  .pop-card {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1000;
    width: calc(100% - 800px);
    height: 622px;
  }

  .pop-head {
    height: 56px;

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

  .pop-content {
    height: 480px;
    display: flex;
    justify-content: center;
    padding: 35px;
    overflow: auto;
  }

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