/**
 * @description: 治安重点部位+保密要害部位监控相机编辑（二者返回结构一致）
 * @return {
 *  titleOfUpdatePositionMonitorCamera: 编辑重点监控部位/编辑要害监控部位
 *  allCameraTree: 所有相机列表
 *  dataOfPosition: 当前分组数据（即治安重点部位或保密要害部位数据）
 *  idOfPosition: 当前分组id
 *  nameOfPosition: 当前分组名称
 *  selectedCameraIdsOfPosition: 当前分组已选择的相机id列表
 * }
 */

import { ref } from 'vue';
import { defineStore } from 'pinia';
import { getCameraTreeList } from '@/api/disaster-overview';
import { PositionMonitorCameraListRes } from '@/api/security-confidentiality-position';

export const usePositionMonitorCameraEdit = defineStore('positionMonitorCameraEdit', () => {
  // 编辑重点监控部位/编辑要害监控部位（dialog标题）
  const titleOfUpdatePositionMonitorCamera = ref('编辑重点监控部位');

  // 所有相机列表
  const allCameraTree = ref();
  // 初始化所有相机列表
  const getAllCameraTree = async () => {
    const res = await getCameraTreeList();
    allCameraTree.value = res;
  };

  const dataOfPosition = ref<PositionMonitorCameraListRes>();
  const idOfPosition = ref<number>();
  const nameOfPosition = ref<string>('');

  interface SelectedCameraIds {
    id: number;
    name: string;
    code: string;
  }
  const selectedCameraIdsOfPosition = ref<SelectedCameraIds[]>([]);

  const initDataOfPosition = () => {
    // 添加
    if (!dataOfPosition.value) {
      idOfPosition.value = undefined;
      nameOfPosition.value = '';
      selectedCameraIdsOfPosition.value = [];
      return;
    }
    // 编辑
    idOfPosition.value = dataOfPosition.value.id;
    nameOfPosition.value = dataOfPosition.value.groupName;
    selectedCameraIdsOfPosition.value = dataOfPosition.value.children.map((item) => ({
      id: item.id,
      name: item.name,
      code: item.code,
    }));
  };

  const resetPositionMonitorCameraEdit = () => {
    titleOfUpdatePositionMonitorCamera.value = '编辑重点监控部位';
    dataOfPosition.value = undefined;
    idOfPosition.value = undefined;
    nameOfPosition.value = '';
    selectedCameraIdsOfPosition.value = [];
  };

  return {
    titleOfUpdatePositionMonitorCamera,
    allCameraTree,
    getAllCameraTree,
    initDataOfPosition,
    dataOfPosition,
    idOfPosition,
    nameOfPosition,
    selectedCameraIdsOfPosition,
    resetPositionMonitorCameraEdit,
  };
});

export default usePositionMonitorCameraEdit;
