<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">编辑相机（通过rtsp添加）</span>

        <el-icon :size="16" class="mr-3" @click="updateValue(false)"><Close /></el-icon>
      </div>
    </template>
    <div class="pop-content">
      <SRSAddCamera
        :form-data="props.editData!"
        @cancel-execute="updateValue(false)"
        @confirm-execute="onUpdateSRSCamera"
      />
    </div>
  </el-card>
</template>

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

  const props = defineProps<{ modelValue: boolean; editData?: CameraSRS }>();

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

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

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

  const onUpdateSRSCamera = (data) => {
    const temp = {
      id: data.id,
      name: data.name,
      code: data.code,
      rtspUrl: data.rtspUrl,
      cameraIp: data.cameraIp,
      videoServiceType: data.videoServiceType,
      sceneTemplateList: data.sceneTemplateList,
      workshopId: data.workshopId,
      workspaceId: data.workspaceId,
      videoStandard: data.videoStandard,
      remark: data.remark,
      sourceType: data.sourceType,
    };
    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">
  .pop-card {
    position: relative;
    margin-left: 21px !important;
  }

  .pop-head {
    height: 56px;

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

    &-tabs {
      margin-top: 18px;

      :first-child {
        border-radius: 8px 0px 0px 0px;
      }

      :last-child {
        border-radius: 0px 8px 0px 0px;
      }
    }
  }

  .tab-item {
    width: 188px;
    height: 38px;
    background: #fafafa;
    border: 1px solid #d9d9d9;
    cursor: pointer;

    &-active {
      background: #e2eefe;
      border: 1px solid #1890ff;
    }
  }

  .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>
