<template>
  <div class="camera-share">
    <div class="flex items-center query-head">
      <el-space alignment="center" :size="50">
        <div style="display: flex">
          <div>公司名称：</div>
          <el-input
            v-model="queryName"
            clearable
            placeholder="请输入公司名称"
            class="query-content"
          />
        </div>
        <div>
          <span>公司账号：</span>
          <el-input
            v-model="queryAccount"
            clearable
            placeholder="请输入公司账号"
            class="query-content"
          />
        </div>
      </el-space>
      <div class="flex-1 flex justify-end">
        <el-button type="primary" @click="queryCameraItems"> 查询 </el-button>
        <el-button @click="resetSearch"> 重置 </el-button>
      </div>
    </div>
    <div class="camera-list">
      <BasicTable
        :columns="getColumns(flattendWorkspaces)"
        :data-source="cameraShareList"
        :row-key="(row) => row.cameraIp"
        :action-column="actionColumn"
        :tableSetting="{
          size: false,
          redo: false,
          fullscreen: false,
          striped: false,
          setting: false,
        }"
        :striped="true"
        ref="tableRef"
      />
    </div>
    <CameraEditshared
      class="add-popover"
      v-model="shareEditedPopover"
      :edit-data="handleEditData"
    />
  </div>
</template>

<script setup lang="ts">
  import { h, reactive, ref, onMounted, watch, onBeforeMount } from 'vue';
  import { getColumns } from '../sharedColumns';
  import editIcon from '@/assets/images/table/table-edit.png';
  import deleteIcon from '@/assets/images/table/table-delete.png';
  import { BasicColumn, TableActionIconsWords, BasicTable } from '@/components/Table';
  import CameraEditshared from '../components/CameraEditshared.vue';
  import useCameraShare from '../stores/useCameraShare';
  import useSceneInfos from '@/hooks/useSceneInfos';
  import { CameraShareType } from '@/api/camera/camera-share';
  import { ElMessageBox } from 'element-plus';

  const useShare = useCameraShare();
  const {
    cameraShareList,
    queryName,
    queryAccount,
    isAddState,
    queryToTenantId,
    conditionSearch,
    handleEdit,
    handleDel,
  } = useShare;

  const sceneInfos = useSceneInfos();
  const { flattendWorkspaces, getScenesTree } = sceneInfos;

  const props = defineProps<{ addCameraType: string }>();
  const emits = defineEmits(['update']);
  onMounted(() => {
    isAddState.value = props.addCameraType === 'incomplete' ? false : true;
    console.log('isAddState', isAddState.value);
    queryToTenantId.value = -10;
    conditionSearch();
    console.log(cameraShareList.value);
  });

  const shareEditedPopover = ref<boolean>(false);

  const handleEditData = ref<CameraShareType>({} as CameraShareType);

  const handleEditDetail = (row) => {
    shareEditedPopover.value = true;
    row.cameraWorkspaceName = flattendWorkspaces.value?.find(
      (item) => item.id === Number(row.cameraWorkspace),
    )?.code;
    handleEditData.value = row;
  };

  //添加按钮
  const handleChangeAdd = (row) => {
    if (!row.cameraWorkspaceName) {
      ElMessageBox.alert('该共享相机未添加场景，请先添加相机场景', 'Warning', {
        confirmButtonText: 'OK',
        type: 'warning',
      });
      return;
    }
    const changeAddData = {
      id: row.id,
      isAdd: row.isAdd ? false : true,
    };
    emits('update');
    handleEdit(changeAddData);
  };

  const handleDelete = (row) => {
    // console.log('row', row);
    emits('update');
    handleDel(row.id);
  };

  //操作列
  const actionColumn: BasicColumn = reactive({
    width: 200,
    title: '操作',
    prop: 'action',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableActionIconsWords as any, {
        space: 20,
        color: '#629bf9',
        style: 'img',
        size: 16,
        actionIcons: [
          {
            label: '添加',
            type: 'words' + props.addCameraType,
            onClick: handleChangeAdd.bind(null, record.row),
          },
          {
            label: '编辑',
            type: 'img',
            icon: editIcon,
            onClick: handleEditDetail.bind(null, record.row),
          },
          {
            label: '删除',
            type: 'img',
            icon: deleteIcon,
            onClick: handleDelete.bind(null, record.row),
          },
        ],
      });
    },
  });

  //查询
  const queryCameraItems = () => {
    isAddState.value = props.addCameraType === 'incomplete' ? false : true;
    conditionSearch();
  };
  //重置
  const resetSearch = () => {
    queryName.value = '';
    queryAccount.value = '';
    queryCameraItems();
  };

  watch(
    () => props.addCameraType,
    (_val) => {
      resetSearch();
      isAddState.value = props.addCameraType === 'incomplete' ? false : true;
      console.log('isAddState', isAddState.value);
      conditionSearch();
    },
  );

  onBeforeMount(() => {
    getScenesTree({ level: 3, valueKey: 'code', labelKey: 'name', disabled: true });
  });
</script>

<style scoped>
  .type-select {
    width: 100px;
  }

  .query-content {
    width: 160px;
    margin-top: -6px;
  }

  .protocal-select {
    width: 160px;
  }

  .query-head {
    padding: 24px 57px 18px 21px;
  }

  .camera-share {
    position: relative;
    height: calc(100vh - 64px - 12px);
    background-color: #ffffff;
  }

  .camera-list {
    padding: 0 21px;
  }

  .add-popover {
    position: absolute;
    width: calc(100%);
    height: 622px;
    top: -57px;
    left: -20px;
    margin: auto;
    z-index: 99;
  }
</style>
