<template>
  <tablePage 
    ref="tablePageRef"
    :columnWidth="150"
    :apiName="state.apiName"
    :searchOptions="state.searchOptions"
    :searchFilter="state.searchFilter"
    :rightOperation="state.rightOperation"
    :showApiRightOperation="state.showApiRightOperation"
    :apiEditFormRules="state.apiEditFormRules"
    @rightOperationHadel="rightOperationHadel"
  ></tablePage>
</template>

<script setup>
defineOptions({ name: 'taskConifgure' })
import { reactive, ref, onMounted,nextTick  } from 'vue'
import tablePage from '@/components/tablePage/index.vue'
import { getSetisautoByUid } from '@/api/system/taskConifgureApi'
import { ElMessageBox,ElMessage } from 'element-plus'
  

const state = reactive({
  apiName:'taskconifgure',
  searchFilter: {
    tableName: null,
    creationTime:null,
  },
  searchOptions:[
    {type:'input',prop:'tableName',label:'表名'},
    {type:'datetimerange',prop:'creationTime',label:'创建时间'},
  ],
  rightOperation:[],
  showApiRightOperation:['apiUpdate'],
  apiEditFormRules:{
    tableName: [{ required: true, message: '必填项', trigger: 'blur' }],
    taskName: [{ required: true, message: '必填项', trigger: 'blur' }],
    api: [{ required: true, message: '必填项', trigger: 'blur' }],
    url: [{ required: true, message: '必填项', trigger: 'blur' }],
    corn: [{ required: true, message: '必填项', trigger: 'blur' }],
  }
})

state.rightOperation = [
  {
    label:'启动',
    name:'setisautoOk',
    link:true,
    type:'success',
    auth:state.apiName+':setisautoOk',
    hide:(row,scope) => {return row.isAuto}
  },
  {
    label:'关闭',
    name:'setisautoNo',
    link:true,
    type:'danger',
    auth:state.apiName+':setisautoNo',
    hide:(row,scope) => {return !row.isAuto}
  },
]

const tablePageRef = ref(null)

// 自动执行-启动/关闭  接口执行
function changeSetisautoHttp(row,btn){
  ElMessageBox.confirm(`是否确定${btn}？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    tablePageRef.value.state.loading = true
    getSetisautoByUid({uid:row.uId,isauto:!row.isAuto})
    .then(res=>{
      ElMessage.success('操作成功！')   
      tablePageRef.value.getTableData()
    })
    .finally(()=>{tablePageRef.value.state.loading = false})
  })
}

// 右侧按钮操作
function rightOperationHadel(btn,scope){
  // 自动执行-启动
  if(btn.name == 'setisautoOk'){
    changeSetisautoHttp(scope.row,btn.label)
  }
  // 自动执行-关闭
  if(btn.name == 'setisautoNo'){
    changeSetisautoHttp(scope.row,btn.label)
  }
}

</script>