<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="4">
            <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-option value="2002">体育志愿宝-VIP</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="3">
            <a-form-item label="操作平台" name="provider">
              <a-select v-model:value="queryParam.provider" show-search allow-clear
                        placeholder="请选择">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="wxpay">微信</a-select-option>
                <a-select-option value="toutiao">抖音</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="3">
            <a-form-item label="支付方式" name="paymentType">
              <a-select v-model:value="queryParam.paymentType" show-search allow-clear
                        placeholder="请选择">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="3">微信</a-select-option>
                <a-select-option value="4">抖音</a-select-option>
                <a-select-option value="10">支付宝</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="8">{{getStatusText("8")}}</a-select-option>
                <a-select-option value="-1">{{getStatusText("-1")}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <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 }">
        <Tag v-if="column.key === 'provider' &&record.provider==='wxpay'" color="success">
          微信
        </Tag>
        <Tag v-if="column.key === 'provider' && record.provider==='toutiao'" color="default">
          抖音
        </Tag>

        <a-tag v-if="column.key === 'paymentType' && record.paymentType==='3'" color="success">
          <template #icon>
            <wechat-outlined/>
          </template>
          微信支付
        </a-tag>
        <a-tag v-if="column.key === 'paymentType' && record.paymentType==='4'" color="success">
          <template #icon>
            <alipay-circle-outlined/>
          </template>
          支付宝支付
        </a-tag>
        <a-tag v-if="column.key === 'paymentType' && record.paymentType==='10'" color="success">
          抖音支付
        </a-tag>



        <Tag v-if="column.key === 'orderStatus'" :color="getStatusColor(record.orderStatus)">
          {{ getStatusText(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>

    <a-modal v-model:open="refundInfo.refundModalVisible" title="订单退款" @ok="handleRefundOk()">
      <div style="padding:10px 30px">
        <p>金额将原路退回，退款后用户的会员身份将移除。</p>
        <a-input-number v-model:value="refundInfo.refundAmount" addon-before="退款金额" addon-after="￥"></a-input-number>
        <a-textarea style="margin: 5px 0" placeholder="退款原因" v-model:value="refundInfo.refundReason" show-count :maxlength="200" />
      </div>
    </a-modal>
  </div>
</template>

<script lang="ts" name="yx-yxOrder" setup>
import {ref, computed, unref, reactive} from 'vue';
import {
  WechatOutlined,AlipayCircleOutlined,
} 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,refund, 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 refundInfo = reactive({
  id:'' as any,
  refundReason:'' as String,
  refundAmount:0 as Number,
  refundModalVisible:false as boolean,
})
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: true,//是否显示操作
    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 handleRefund(record:Recordable){
  console.log(record)
  refundInfo.id = record.id
  refundInfo.refundReason = '不想要了。'
  if(record.totalAmount){
    refundInfo.refundAmount = record.totalAmount/100;
  }
  refundInfo.refundModalVisible = true
}

// 确认退款
async function handleRefundOk(){
  await refund(refundInfo)
  refundInfo.refundModalVisible = false
}

/**
 * 详情
 */
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.provider==='toutiao' && record.orderStatus==='2'){
    return [
      {
        label: '退款',
        onClick: handleRefund.bind(null, record),
      }
      /*{
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      }*/
    ]
  }
  return []

}

/**
 * 下拉操作栏
 */
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 if (orderStatus === '8') {
    return '已退款';
  } else {
    return '未知';
  }
}

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

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

</script>

<style scoped>

</style>
