  <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.TemperatureMax" :validator="(x) => x >= 0" style="width: 100%;">
          <template #suffix>
            °C
          </template>
        </n-input-number>
      </n-form-item>
      <n-form-item label="最小温度">
        <n-input-number v-model:value="data.TemperatureMin" :validator="(x) => x >= 0" style="width: 100%;">
          <template #suffix>
            °C
          </template>
        </n-input-number>
      </n-form-item>
      <n-form-item label="最大湿度">
        <n-input-number v-model:value="data.HumidityMax" :validator="(x) => x >= 0" style="width: 100%;">
          <template #suffix>
            %
          </template>
        </n-input-number>
      </n-form-item>
      <n-form-item label="最小湿度">
        <n-input-number v-model:value="data.HumidityMin" :validator="(x) => x >= 0" 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 {TaskUpdateRand } 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: () => ({}),
  },
  checkData:{
    type:Array,
    default: () => ([])
  }
})


const data = reactive({
  TemperatureMin: 0,
  TemperatureMax: 0,
  HumidityMin: 0,
  HumidityMax: 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
  })
  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
    
    showModal.value = true
  }
}



//提交
const handleValidateButtonClick = ()=>{
  
  TaskCopyApi()
}
const TaskCopyApi = async()=>{
  let resIt = await dataFun()  
  TaskUpdateRand(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>
