<template>
   <el-drawer
      v-model="props.visible"
      title="调度日志"
      direction="rtl"
      :size="size"
      :close-on-click-modal="false"
      :before-close="dialogClose"
  >
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :actionColumn="actionColumn"
         @selection-change="onSelectionChange"
      >
        <template #tableTitle>
          <el-button type="danger" @click="handleDelete()" :disabled="!selectionData.length">
              <template #icon>
                <el-icon class="el-input__icon">
                  <Delete />
                </el-icon>
              </template>
              删除
            </el-button>
        </template>
      </BasicTable>
    </el-card>
    <editDialog
        v-if="editVisible"
        :logId="logId"
        v-model:visible="editVisible"
    >
    </editDialog>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { reactive, ref, h,nextTick,defineAsyncComponent } from 'vue';
  import { ColProps } from 'element-plus';
  import { schemas } from './querySchemas';
  import { useForm } from '@/components/Form/index';
  import { TableAction } from '@/components/Table';
  import { getJobLogList,jobLogDelete,jobLogBatchDelete } from '@/api/monitor/job';
  import { columns } from './columns';
  import {message,confirm} from "@/utils/auth";
  const editDialog = defineAsyncComponent(() =>
    import('./edit.vue')
)
  const emit = defineEmits(["update:visible"]);
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  jobId: {
    type: Number,
    required: true,
    default: 0
  }
});
const editVisible = ref(false)
const logId = ref(0)
const size = document.body.clientWidth - 500;
const selectionData = ref([])
  const tableRef = ref();
  const formParams = reactive({
    jobName:"",
    status:''
  });
  const actionColumn = reactive({
    width: 250,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction, {
        style: 'button',
        actions: [
        {
            label: '详情',
            icon:'View',
            type: 'warning',
            onClick: handleDetail.bind(null, record),
            ifShow: () => {
              return true;
            },
            // auth: ['sys:smsLog:detail'],
          },
          {
            label: '删除',
            icon:'Delete',
            type: 'danger',
            onClick: handleDelete.bind(null, record),
            // 根据业务控制是否显示 isShow 和 auth 是并且关系
            ifShow: () => {
              return true;
            },
            // 根据权限控制是否显示: 有权限，会显示，支持多个
            // auth: ['basic_list'],
          },
        ],
      });
    },
  });

  const loadDataTable = async (res: any) => {
    const result = await getJobLogList({ ...formParams, jobId:props.jobId, ...res });
    return result;
  };

  function reloadTable(noRefresh='') {
    tableRef.value.reload(noRefresh?{}:{pageNo:1});
  }
  const [register, {}] = useForm({
    labelWidth: 100,
    layout: 'horizontal',
    colProps: { span: 6 } as ColProps,
    submitOnReset:true,
    schemas
  });
  function handleSubmit(values: Recordable) {
    handleReset()
    for (const key in values) {
      formParams[key] = values[key]
    }
    reloadTable();
  }

  function handleReset() {
    for (const key in formParams) {
        formParams[key] ='';
    }
  }
  const dialogClose = () => {
  emit("update:visible", false);
};
const handleDetail = async (record: Recordable) => {
  logId.value=record.row.id
  await nextTick();
  editVisible.value=true
};
async function handleDelete(record: Recordable) {
    let ids = []
    if(!record){
      ids = selectionData.value.map(({id}) => id);
    }
    await confirm('确定要删除？');
    record? await jobLogDelete(record.row.id):await jobLogBatchDelete(ids);
    message("删除成功");
    reloadTable()
  }
function onSelectionChange(value){
    selectionData.value = value
  }
</script>

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