<template>
  <!-- 补漏 -->
  <n-button @click="showImportModal">补漏</n-button>
</template>

<script setup>
import { TaskRepairSen } from '@/api';

import { reactive } from 'vue'
import { useMessage, useDialog } from 'naive-ui';
import { TimeDate } from '@/plugin/timeFun.js'
const message = useMessage();

const props = defineProps({
  queryObj: {
    type: Object,
    default: () => ({}),
  },
  checkData: {
    type: Array,
    default: () => ([])
  }
})


const data = reactive({
  T_saveT: 60,
  T_task_id: '',
  SN_List: ''
})

const model = reactive({
  StartTime: null,
  EndTime: null,
})

const dialog = useDialog();
const showImportModal = () => {
  if (props.checkData == null) {
    message.error('请选择需要补漏的设备')
    return
  } else if (props.queryObj.Time_start == '' && props.queryObj.Time_end == '') {
    message.error('请选择补漏的时间')
    return
  } else {
    data.T_task_id = props.queryObj.T_task_id
    model.StartTime = props.queryObj.Time_start
    model.EndTime = props.queryObj.Time_end

    dialog.warning({
      title: '提示',
      content: `确定对${props.queryObj.Time_start}至${props.queryObj.Time_end}进行数据补漏吗？`,
      positiveText: '立即补漏',
      negativeText: '取消',
      onPositiveClick: () => {
        TaskCopyApi()
      },
      onNegativeClick: () => {
        message.error('已取消补漏')
      }
    })
  }
}




const TaskCopyApi = async () => {
  let resIt = await dataFun()
  TaskRepairSen(resIt).then(res => {
    if (res.data.Code == 200) {
      message.success(res.data.Msg)
    }
  })
}
const dataFun = () => {
  return new Promise(resolve => {
    let models = { ...model }
    let snList = [...props.checkData]
    //处理时间戳转时间格式
    Object.keys(models).forEach(item => {
      models[item] = TimeDate(models[item]) + ':00'
    })

    //处理sn List
    const arr1 = snList.map(item => item.T_sn + ',' + item.T_id)
    data.SN_List = arr1.join('|') + '|'
    console.log(data, models)
    resolve({
      ...data, ...models
    })
  })
}
</script>

<style lang="scss" scoped></style>
