<template>
  <el-dialog
      v-model="props.visible"
      title="选择文章"
      :close-on-click-modal="false"
      :before-close="dialogClose"
  >
  <el-card  class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
  <el-card >
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        ref="tableRef"
        highlight-current-row
        @row-click="rowClick"
      >
      </BasicTable>
    </el-card>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button  type="primary" @click="handleSubmits">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { getArticleList } from '@/api/content/article';
import {onMounted, reactive,ref} from "vue";
import { useForm } from '@/components/Form/index';
import { ColProps } from 'element-plus';
import {message} from "@/utils/auth";
import { columns } from './columns';
import { schemas } from './querySchemas';

const emit = defineEmits(["success", "update:visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  }
});
const selectRow = ref({})
const tableRef = ref();
  const formParams = reactive({
    title:'',
    status:''
  });
const handleSubmits = async () => {
   if(!selectRow.value.id) {
    return message("请选择文字文章",'error');
   }
    emit("update:visible", false);
    emit("success",selectRow.value);
};

const dialogClose = () => {
  emit("update:visible", false);
};

const loadDataTable = async (res: any) => {
    const result = await getArticleList({ ...formParams, ...res });
    return result;
  };
  function reloadTable(noRefresh='') {
    tableRef.value.reload(noRefresh?{}:{pageNo:1});
  }
  const [register, {}] = useForm({
    labelWidth: 80,
    layout: 'horizontal',
    colProps: { span: 8 } 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 rowClick = (row)=>{
    selectRow.value = row
  }
onMounted(() => {

});

</script>
