<template>
  <div>
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam"
              :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="3">
            <a-form-item label="会员卡号">
              <a-input v-model:value="queryParam.cardNum" placeholder="请输入会员卡号"
                       allowClear="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="3">
            <a-form-item label="会员类型" name="skuCode">
              <a-select v-model:value="queryParam.skuCode" show-search allow-clear
                        placeholder="请选择">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="1001">体验卡</a-select-option>
                <a-select-option value="1002">VIP</a-select-option>
                <a-select-option value="9999">管理员</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="3">
            <a-form-item label="所属应用" name="skuCode">
              <a-select v-model:value="queryParam.programType" show-search allow-clear
                        placeholder="请选择">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="艺体志愿宝">艺体志愿宝</a-select-option>
                <a-select-option value="体育志愿宝">体育志愿宝</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="有效时长(天)">
              <a-input type="number" v-model:value="queryParam.validTime" placeholder="请输入有效时长"
                       allowClear="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
            <a-button preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
<!--        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增
        </a-button>-->
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出
        </a-button>
<!--        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">
          导入
        </j-upload-button>-->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"
                     :dropDownActions="getDropDownAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{text}">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{text}">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined"
                  size="small" @click="downloadFile(text)">下载
        </a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <YxVipCardModal @register="registerModal" @success="handleSuccess"></YxVipCardModal>
  </div>
</template>

<script lang="ts" name="yx-yxVipCard" setup>
import {ref, computed, unref,reactive} from 'vue';
import {BasicTable, useTable, TableAction} from '/@/components/Table';
import {useModal} from '/@/components/Modal';
import {useListPage} from '/@/hooks/system/useListPage'
import YxVipCardModal from './components/YxVipCardModal.vue'
import {columns, searchFormSchema} from './YxVipCard.data';
import {list, deleteOne, batchDelete, getImportUrl, getExportUrl} from './YxVipCard.api';
import {downloadFile} from '/@/utils/common/renderUtils';

const checkedKeys = ref<Array<string | number>>([]);
const queryParam = reactive<any>({});
const queryParam2 = reactive<any>({});
//注册model
const [registerModal, {openModal}] = useModal();
const labelCol = reactive({
  xs: {span: 24},
  sm: {span: 7},
});
const wrapperCol = reactive({
  xs: {span: 24},
  sm: {span: 16},
});
//注册table数据
const {prefixCls, tableContext, onExportXls, onImportXls} = useListPage({
  tableProps: {
    title: 'VIP卡密表',
    api: list,
    columns,
    canResize: false,
    useSearchForm:false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam2);
    },
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
  },
  exportConfig: {
    name: "VIP卡密表",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, {reload}, {rowSelection, selectedRowKeys}] = tableContext

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}

/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}

/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({id: record.id}, handleSuccess);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
}

/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}

/**
 * 操作栏
 */
function getTableAction(record) {
  if (!record.userId) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      }
    ]
  } else {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }
    ]
  }
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  if (!record.userId) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        }
      }]
  }
}

/**
 * 查询
 */
function searchQuery() {
  if (queryParam.cardNum) {
    queryParam2.cardNum = '*'+queryParam.cardNum+'*'
  }
  if(queryParam.programType){
    queryParam2.programType = queryParam.programType
  }
  queryParam2.skuCode = queryParam.skuCode
  queryParam2.validTime = queryParam.validTime
  reload();
}

/**
 * 重置
 */
function searchReset() {
  queryParam.cardNum=''
  queryParam2.cardNum=''
  queryParam.skuCode=''
  queryParam2.skuCode=''
  queryParam.validTime=''
  queryParam2.validTime=''
  queryParam.programType=''
  queryParam2.programType = ''
  selectedRowKeys.value = [];
  //刷新数据
  reload();
}
</script>

<style scoped>

</style>
