<template>
  <div>
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container" v-if="!props.isNoSearch">
      <a-form @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="8">
            <a-form-item label="星期">
              <j-dict-select-tag placeholder="请选择星期" v-model:value="queryParam.week" dictCode="week"/>
            </a-form-item>
          </a-col>
          <a-col :lg="8">
            <a-form-item label="节次">
              <j-dict-select-tag placeholder="请选择节次" v-model:value="queryParam.hh" dictCode="skjc"/>
            </a-form-item>
          </a-col>
          <a-col :lg="8">
            <a-form-item label="课程性质">
              <j-dict-select-tag v-model:value="queryParam.kcxz" dictCode="kcxz" placeholder="请选择课程性质"/>
            </a-form-item>
          </a-col>
          <a-col :lg="8">
            <a-form-item label="课程名称或教师名">
              <a-input placeholder="请输入课程名称或教师名" v-model:value="queryParam.searchInput"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <vxe-toolbar>
      <template #buttons>
        <a-button type="primary" class="pl1r" @click="handleAdd" preIcon="ant-design:plus-outlined" v-if="!props.isNoSearch"> 新增</a-button>
        <a-button type="primary" class="pl1r" @click="handleYichu" preIcon="ant-design:plus-outlined" v-if="!props.isNoSearch"> 移除</a-button>
      </template>
    </vxe-toolbar>
    <JVxeTable
      ref="tableRef"
      row-number
      keep-source
      bordered
      :loading="loading"
      :dataSource="dataSource"
      :columns="columns"
      :pagination="pagination"
      style="margin-top: 8px"
      @page-change="handlePageChange"
    >
      <!--操作栏-->
    <template #myAction="props">
      <Popconfirm title="确定移除吗？" @confirm="handleDelete(props)">
        <a>移除</a>
      </Popconfirm>
    </template>
    </JVxeTable>

    <!-- 表单区域 -->
    <XzKechengbiaoList ref="registerModal" @success="handleSuccess"></XzKechengbiaoList>
    <YcKechengbiaoListModal ref="ycregisterModal" @success="handleSuccess"></YcKechengbiaoListModal>
  </div>
</template>

<script lang="ts" name="ktgl-kcKechengbiao" setup>
  import { ref, reactive, onMounted, watch } from 'vue';
  import { Popconfirm } from 'ant-design-vue';
  // import { useListPage } from '/@/ho   oks/system/useListPage';
  import { propTypes } from '/@/utils/propTypes';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { columns } from './KcKechengbiao.data';
  import { list, batchDelete,deleteOne, saveOrUpdate } from './KcKechengbiao.api';
  import XzKechengbiaoList from './components/XzKechengbiaoListModal.vue'
  import YcKechengbiaoListModal from './components/YcKechengbiaoListModal.vue'
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';

  import { JVxeTableInstance } from '/@/components/jeecg/JVxeTable/types';
  import { filterObj } from '/@/utils/common/compUtils';

  //--------------------------------------变量区---------------------------------------------------------------------------

  const props = defineProps({
    searchParam: propTypes.object.def({}),
    isNoSearch: propTypes.bool.def(false)
  });

  const tableRef = ref<JVxeTableInstance>();
  const loading = ref(false);

  const selectedRows = ref([]);
  const selectedRowKeys = ref([]);

  const dataSource = ref<any[]>([]);
  const pagination = reactive({
    current: 1,
    pageSize: 10,
    pageSizeOptions: ['10', '20', '30', '100', '200'],
    total: 0,
  });

  const queryParam = ref<any>({ });
  // const filterInfo = ref<any>({ xnxq: '2022-2023-2' });
  const filterInfo = ref<any>({ });
  const registerModal = ref();
  const ycregisterModal = ref();
  
  const labelCol = reactive({ xs: { span: 24 }, sm: { span: 7 }, });
  const wrapperCol = reactive({ xs: { span: 24 }, sm: { span: 16 }, });

  const { createMessage } = useMessage();

  //--------------------------------------变量区END------------------------------------------------------------------------
  //--------------------------------------初始化---------------------------------------------------------------------------


  onMounted(() => {
    loadData(1);
  })

  watch(() => props.searchParam, async (newRow, oldRow) => {
    reload();
  });
  //--------------------------------------初始化END---------------------------------------------------------------------------
  //--------------------------------------与后台交互---------------------------------------------------------------------------
  function loadData(pageNo?){
    loading.value = true;
    if(pageNo == 1){
      pagination.current = 1;
    }
    //参数
    let { current, pageSize } = pagination;
    let param = {szkc:'1', column: 'createTime', order: 'desc', pageNo: current, pageSize, ...queryParam.value, ...filterInfo.value, ...props.searchParam }
    list(filterObj(param)).then(res => {
      dataSource.value = res?.records??[];
      pagination.total = res?.total??0;
      //如果是隐藏模式，则直接选中
      if(props.isNoSearch){
        selectedRows.value = Array<never>(dataSource.value[0]);
        console.log('tableRef =>',tableRef);

      }

    }).finally(() => {
      // 这里是无论成功或失败都会执行的方法，在这里关闭loading
      loading.value = false;
    });
  }

  //--------------------------------------与后台交互---------------------------------------------------------------------------
  //--------------------------------------表格功能事件---------------------------------------------------------------------------
  function handlePageChange(event) {
    // 重新赋值,分页
    pagination.current = event.current;
    pagination.pageSize = event.pageSize;
    //查询数据
    loadData();
  }
  //--------------------------------------表格功能事件---------------------------------------------------------------------------
  //--------------------------------------按钮以及其他功能---------------------------------------------------------------------------

  /**
   * 查询
   */
   function searchQuery() {
    reload();
  }

  /**
   * 重置
   */
  function searchReset() {
    queryParam.value = {};
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }

  //刷新列表数据
  function reload() {
    loadData(1);
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }
  
  /**
   * 移除事件
   */
   function handleYichu() {
    ycregisterModal.value.title = "移除";
    ycregisterModal.value.disableSubmit = false;
    ycregisterModal.value.add();
  }


  /**
   * 删除事件
   */
   async function handleDelete(record) {
    var info =  record.row
    info.szkc = '0'
    await saveOrUpdate(info, true)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          reload();
        } else {
          createMessage.warning(res.message);
        }
      })
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  //--------------------------------------按钮以及其他功能---------------------------------------------------------------------------

</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
  }
  .pl1r {
    margin-left: 1rem;
  }
</style>
