<template>
  <div>
    <div>
      <el-form
        ref="NVRFormRef"
        class="ip-form"
        :inline="true"
        :model="cameraNVRData"
        :rules="rules"
        label-width="124px"
        label-position="left"
      >
        <el-form-item label="NVR设备：" prop="nvrId" style="width: 766px">
          <el-select v-model="cameraNVRData.nvrId" placeholder="请选择NVR设备" style="width: 200px">
            <el-option
              v-for="item in nvrDevice"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
          <div>
            <el-icon><InfoFilled /></el-icon>
            选择已添加在【NVR设备管理】中的设备编号
          </div>
        </el-form-item>
        <el-form-item label="名称：" prop="name">
          <el-input v-model="cameraNVRData.name" placeholder="请输入名称" style="width: 200px" />
        </el-form-item>
        <el-form-item label="设备ID：" prop="code">
          <el-input
            v-model="cameraNVRData.code"
            placeholder="自定义ID，不能重复"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="NVR平台通道号：" prop="nvrChannel">
          <el-input
            v-model="cameraNVRData.nvrChannel"
            placeholder="请输入NVR平台通道号"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="服务类型：" prop="videoServiceType">
          <el-select
            v-model="cameraNVRData.videoServiceType"
            placeholder="请选择服务类型"
            style="width: 200px"
          >
            <el-option
              v-for="protocal in serviceTypeSelect"
              :key="protocal.value"
              :label="protocal.label"
              :value="protocal.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="业务场景：" prop="sceneTemplateList" style="width: 766px">
          <sysConfigItemOfAddCamera
            :origin-scene-checked-list="cameraNVRData.sceneTemplateList"
            @update:scene-checked-list="handleUpdateSceneTemplateList"
          />
        </el-form-item>
        <el-form-item label="所属工位：" prop="workspaceId">
          <el-tree-select
            v-model="cameraNVRData.workspaceId"
            :data="workspaceInfo"
            :render-after-expand="false"
            :default-expand-all="props.formData ? true : false"
            check-strictly
            placeholder="请选择工位"
            style="width: 200px"
            @node-click="handleTreeSelect"
          />
        </el-form-item>
        <el-form-item label="工位负责人：" prop="principal">
          <el-input
            v-model="cameraNVRData.principal"
            placeholder="请选择工位，该项自动填充"
            disabled
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="协议类型：" prop="cameraType">
          <el-select
            v-model="cameraNVRData.cameraType"
            placeholder="请选择协议类型"
            style="width: 200px"
          >
            <el-option
              v-for="protocal in protocalTypeSelect"
              :key="protocal.value"
              :label="protocal.label"
              :value="protocal.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="视频编码标准：" prop="videoStandard">
          <el-select
            v-model="cameraNVRData.videoStandard"
            placeholder="请选择视频编码标准"
            style="width: 200px"
          >
            <el-option
              v-for="protocal in standardTypeSelect"
              :key="protocal.value"
              :label="protocal.label"
              :value="protocal.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="cameraNVRData.remark" placeholder="请输入备注" style="width: 200px" />
        </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 { onBeforeMount, ref } from 'vue';
  import { cloneDeep } from 'lodash-es';
  import { ElIcon } from 'element-plus';
  import { InfoFilled } from '@element-plus/icons-vue';
  import { CameraDetailServer } from '@/types/camera/type';
  import {
    protocalTypeSelect,
    serviceTypeSelect,
    standardTypeSelect,
  } from '@/types/camera/constant';
  import useNvrDevice from '../hooks/useNvrDevice';
  import useTreeWorkspaceInfo from '../hooks/useTreeWorkspaceInfo';
  import sysConfigItemOfAddCamera from './sysConfigItemOfAddCamera.vue';

  const props = defineProps<{ formData?: CameraDetailServer | null }>();
  const emits = defineEmits(['cancel-execute', 'confirm-execute']);

  const nvrDeviceInfo = useNvrDevice();
  const { nvrDevice, getNvrDevice } = nvrDeviceInfo;

  const treeWorkspaceInfo = useTreeWorkspaceInfo();
  const { workspaceInfo, getWorkspaceFromTreeInfo, getWorkspaceIdFromTreeNode } = treeWorkspaceInfo;

  const NVRFormRef = ref();
  const cameraNVRData = ref<CameraDetailServer>({} as CameraDetailServer);

  const rules = {
    nvrId: [{ required: true, message: '请选择NVR设备', trigger: 'blur' }],
    name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    code: [{ required: true, message: '请输入设备ID', trigger: 'blur' }],
    nvrChannel: [{ required: true, message: '请输入NVR平台通道号', trigger: 'blur' }],
    sceneTemplateList: [{ required: true, message: '请选择至少一个业务场景', trigger: 'blur' }],
    workspaceId: [{ required: true, message: '请选择工位', trigger: 'blur' }],
    cameraType: [{ required: true, message: '请选择协议类型', trigger: 'blur' }],
    videoStandard: [{ required: true, message: '请选择视频编码标准', trigger: 'blur' }],
  };

  const handleUpdateSceneTemplateList = (data) => {
    cameraNVRData.value.sceneTemplateList = data;
  };

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

  const handleConfirm = () => {
    NVRFormRef.value.validate((valid) => {
      if (valid) {
        const copyData = cloneDeep(cameraNVRData.value);
        delete (copyData as any).principal;
        copyData.sourceType = 'NVR';
        emits('confirm-execute', copyData);
      }
    });
  };

  const handleTreeSelect = (node) => {
    if (node.disabled) return;
    cameraNVRData.value.workspaceId = Number(getWorkspaceIdFromTreeNode(node.value));
    cameraNVRData.value.principal = node.principalName ? node.principalName : '该工位暂无负责人';
    cameraNVRData.value.workshopId = node.workshopId;
  };

  onBeforeMount(() => {
    getNvrDevice();
    getWorkspaceFromTreeInfo();
    if (props.formData) {
      console.log('props.formData', props.formData);
      cameraNVRData.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>
