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

<script setup>
import { TaskDataSensor } 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: () => ({}),
  },
  pickleveTime:{
    type:Array,
    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 || props.checkData.length==0) {
    message.error('请选择需要趋势的设备')
    return
  } else if (props.pickleveTime==null) {
    message.error('请选择趋势的时间')
    return
  } else {
    data.T_task_id = props.queryObj.T_task_id
    model.StartTime = new Date(props.pickleveTime[0]).getTime()
    model.EndTime = new Date(props.pickleveTime[1]).getTime()

    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()
  TaskDataSensor(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>
