<template>
  <!-- 偏移随机 -->
  <n-button @click="showImportModal">平滑</n-button>
  <n-modal v-model:show="showModal" title="平滑" :mask-closable="false" :show-icon="false" preset="dialog">
    <n-form label-placement="left" label-width="auto" style="margin-top: 20px;">
      <n-form-item label="温度">
        <n-input-number v-model:value="data.tRange" style="width: 100%;">
          <template #suffix>
            °C
          </template>
        </n-input-number>
      </n-form-item>
      
      <n-form-item label="湿度">
        <n-input-number v-model:value="data.hRange" style="width: 100%;">
          <template #suffix>
            %
          </template>
        </n-input-number>
      </n-form-item>
      <div style="display: flex; justify-content: flex-end">
        <n-button @click="showModal = false" style="margin-right: 10px;">取消</n-button>
        <n-button type="primary" @click="handleValidateButtonClick">立即提交</n-button>
      </div>
    </n-form>
  </n-modal>
</template>

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

import { reactive, ref, watch } from 'vue'
import { useMessage } from 'naive-ui';
import {TimeDate} from '@/plugin/timeFun.js'
const message = useMessage();
const value = ref(0)
const props = defineProps({
  queryObj: {
    type: Object,
    default: () => ({}),
  },
  pickleveTime:{
    type:Array,
    default: () => ([])
  },
  checkData:{
    type:Array,
    default: () => ([])
  }
})


const data = reactive({
  tRange: 0,
  hRange: 0,
  T_task_id: '',
  SN_List: ''

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



const showModal = ref(false)
const showImportModal = () => {
  Object.keys(model).forEach(item=>{
    console.log(item)
    model[item] = null
  })
  console.log('1111',props.checkData)
  
  if( props.checkData==null || props.checkData.length==0){
    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 = new Date(props.pickleveTime[0]).getTime()
    model.EndTime = new Date(props.pickleveTime[1]).getTime()
    
    showModal.value = true
  }
}



//提交
const handleValidateButtonClick = ()=>{
  
  TaskCopyApi()
}
const TaskCopyApi = async()=>{
  let resIt = await dataFun()  
  TaskSmooth(resIt).then(res=>{
    if(res.data.Code==200){
      message.success(res.data.Msg)
      showModal.value = false
    }
  })
}
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('|')+'|'
    resolve({
      ...data,...models
    })
  })
}
</script>

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