<template>
  <el-dialog
      v-model="props.visible"
      title="选择文章"
      :close-on-click-modal="false"
      :before-close="dialogClose"
      style="margin-top:20px;"
  >
  <el-card  class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
  <el-card >
      <BasicTable
        :columns="type==1?columns:columns2"
        :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 { getNoticeList } from '@/api/data/notice';
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 { columns2 } from './columns2';
import { schemas } from './querySchemas';
import { schemas2 } from './querySchemas2';

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

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  type: {
    type: Number,
    required: true,
    default: 1
  },
});
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) => {
    let result = []
    if(props.type==1) {
      result = await getArticleList({ ...formParams, ...res });
    } else {
      result = await getNoticeList({ ...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:props.type==1?schemas:schemas2
  });
  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>
