<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="6">
            <a-form-item label="订单编号">
              <a-input v-model:value="queryParam.orderCode" 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="orderStatus">
              <a-select v-model:value="queryParam.orderStatus" show-search allow-clear
                        placeholder="请选择">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="1">{{getStatusText("1")}}</a-select-option>
                <a-select-option value="2">{{getStatusText("2")}}</a-select-option>
                <a-select-option value="-1">{{getStatusText("-1")}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <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>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template v-if="false" #tableTitle>
<!--        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>-->
<!--        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>-->
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-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 #bodyCell="{ column, record }">
        <a-tag v-if="column.key === 'paymentType' && record.paymentType==='3'" color="success">
          <template #icon>
            <wechat-outlined/>
          </template>
          微信支付
        </a-tag>
        <!--        <AlipayCircleOutlined />-->

        <Tag v-if="column.key === 'orderStatus'" :color="getStatusColor(record.orderStatus)">
          {{ getStatusText(record.orderStatus) }}
        </Tag>

      </template>
      <!--      <template #category="{ record }">
              <Tag color="green">
                {{ record.orderStatus }}
              </Tag>
            </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>
    <!-- 表单区域 -->
    <YxOrderModal @register="registerModal" @success="handleSuccess"></YxOrderModal>
  </div>
</template>

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

const checkedKeys = ref<Array<string | number>>([]);
const queryParam = 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: {
    api: list,
    columns,
    canResize: false,
    useSearchForm: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
    bordered: true,
    showTableSetting: false,
    showActionColumn: false,//是否显示操作
    /*actionColumn: {
      width: 120,
      fixed: 'right'
    },*/
  },
  exportConfig: {
    name: "订单表",
    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) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    }
  ]
}

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

/**
 * 判断 状态的颜色
 */
function getStatusColor(orderStatus) {
  if (orderStatus === '2') {
    return 'success'
  }
  return 'default'
}

function getStatusText(orderStatus) {
  if (orderStatus === '1') {
    return '未付款';
  } else if (orderStatus === '2') {
    return '已付款';
  } else {
    return '未知';
  }
}

/**
 * 查询
 */
function searchQuery() {
  /*if (queryParam.title) {
    queryParam.title = '*'+queryParam.title+'*'
  }*/
  reload();
}

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

</script>

<style scoped>

</style>
