<template>
  <!-- 平均复制 -->
  <n-button @click="showImportModal">平均复制到</n-button>
  <n-modal style="width: 40%" v-model:show="showModal" title="平均复制到：" :mask-closable="false" :show-icon="false" preset="dialog">
    <n-form ref="formRef" :model="model" :rules="rules" label-placement="left" require-mark-placement="left" label-width="100">
      <n-form-item label="开始时间">
        <n-date-picker v-model:value="model.StartTime" format="yyyy-MM-dd HH:mm" disabled type="datetime" style="width: 100%;" />
      </n-form-item>
      <n-form-item label="结束时间">
        <n-date-picker v-model:value="model.EndTime" format="yyyy-MM-dd HH:mm" disabled type="datetime" style="width: 100%;" />
      </n-form-item>
      <n-form-item label="复制到时间" path="CopyTime">
        <n-date-picker v-model:value="model.CopyTime" format="yyyy-MM-dd HH:mm" type="datetime" style="width: 100%;" />
      </n-form-item>
      <n-form-item label="复制到设备" path="selectSn">
        <n-select v-model:value="T_sn_list" label-field="T_id" value-field="T_snid" :options="options">
        </n-select>
      </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 {CopyFromPosit } from '@/api';
import { reactive, ref, watch } from 'vue'
import { useMessage } 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: () => ([])
  },
  classList:{
    type:Array,
    default: () => ([])
  }
})
const options = computed(() => {
  
  let arr = [...props.classList]
  arr.forEach(item=>{
    item.T_snid = item.T_sn + ',' + item.T_id
  })
  return arr;
});

const data = reactive({
  CopySN:'',
    CopyID:'',
  T_saveT: 60,
  T_task_id: '',
  SN_List: ''
})
const T_sn_list = ref(null)
const model = reactive({
    StartTime:null,
    EndTime:null,
    CopyTime: null, 
    
})
watch(()=>T_sn_list.value,(newValue)=>{
  if(newValue!=null){
    rules.selectSn.required = false
  }else{
    rules.selectSn.required = true
  }
})

const rules = reactive({
  CopyTime: {
    type: "number",
    required: true,
    trigger: ["blur", "change"],
    message: "请选择结束时间"
  },
  selectSn:{
    required: true,
    trigger: "blur",
    message: "请选择复制设备"
  }
})

const showModal = ref(false)


const showImportModal = () => {
  Object.keys(model).forEach(item=>{
    console.log(item)
    model[item] = null
  })
  if( props.checkData==null || props.checkData.length==0){
    message.error('请选择需要复制的设备')
    return
  }else if(props.checkData.length!=2){
    message.error('平均复制到需要选择2项哦')
    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()
    console.log(model,props.queryObj,props.checkData)
    rules.selectSn.required = true
    T_sn_list.value = null
    showModal.value = true
  }
}


const formRef = ref(null);


//提交
const handleValidateButtonClick = ()=>{
  formRef.value?.validate((errors) => {
    if (!errors) {
      TaskCopyApi()
    }
  });
}
const TaskCopyApi = async()=>{
  let resIt = await dataFun()  
  CopyFromPosit(resIt).then(res=>{
    if(res.data.Code==200){
      message.success(res.data.Msg)
      showModal.value = false
    }
  })
}
const dataFun = ()=>{
  
  return new Promise(resolve=>{
    data.CopySN = T_sn_list.value.split(',')[0]
    data.CopyID = T_sn_list.value.split(',')[1]
    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>
