<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button
          type="primary"
          @click="router.push({ name: 'businessManagement-publishcourses', query: { type: 0 } })"
          preIcon="ant-design:plus-outlined"
        >
          新增</a-button
        >
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-button v-auth="'courses:nm_courses:deleteBatch'"
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }"> </template>
      <template #price="{ record }"> {{ record.sellingPrice }}/{{ record.originalPrice }} </template>
      <template #priceType="{ record }">
        <Switch v-model:checked="record.rackingStatus" :checked-value="0" :un-checked-value="1"></Switch>
      </template>
      <template #totalNum="{ record }">
        <div>{{ record.totalNum }}课时</div>
        {{ record.startTime }}-{{ record.endTime }}
      </template>
      <template #img="{ text }">
        <TableImg :img-list="[text]" :size="60" simpleShow></TableImg>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
  </div>
  <coursesModel @register="registerModal"></coursesModel>
</template>

<script lang="ts" name="courses" setup>
  import { Switch } from 'ant-design-vue';
  import { router } from '/@/router';
  import coursesModel from './components/coursesModel.vue';
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, TableImg, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './courses.data';
  import { list, deleteOne, editPriceRules, queryCourseList } from './courses.api';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useShopInfoStore } from '/@/store/modules/shopInfo';
  import { useRouteTabText } from '/@/hooks/web/useRouteTab';
  const queryParam = reactive<any>({});
  const userStore = useUserStore();
  const { createMessage } = useMessage();
  useShopInfoStore().getCurrentDep();

  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext } = useListPage({
    tableProps: {
      title: 'courses',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
        autoAdvancedCol: 4,
      },

      actionColumn: {
        width: 180,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  /**
   * 编辑事件
   */
  function handleView(record: Recordable) {
    router.push({ name: 'businessManagement-publishcourses', query: { type: 2, id: record.id } });
  }
  function handleEdit(record: Recordable) {
    router.push({ name: 'businessManagement-publishcourses', query: { type: 1, id: record.id } });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '查看',
        onClick: handleView.bind(null, record),
        // auth: 'courses:nm_courses:edit',
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        // auth: 'courses:nm_courses:edit',
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
        // auth: 'courses:nm_courses:delete',
      },
      {
        label: '编辑补课课表',
        onClick: handleEditCourses.bind(null, record),
        // auth: 'courses:nm_courses:delete',
      },
    ];
  }
  async function handleEditCourses(record) {
    const res = await queryCourseList({ coursesType: 1, id: record.id });
    openModal(true, {
      record,
      isUpdate: false,
      showFooter: true,
    });
  }
  useRouteTabText(['发布课程', '编辑课程', '查看课程']);
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
