<template>
  <Page>
    <!-- 查询和全局功能 -->
    <Row>
      <Col :span="22">
      <!-- 查询 -->
      <Space v-if="data.isShowQuery" :wrap="true">
        <Input v-model:value="data.query.configName" allowClear :placeholder="$t('robot.giftConfig.configName')" />
        <!-- <Input v-model:value="data.query.notes" allowClear :placeholder="$t('robot.giftConfig.notes')" />
        <InputNumber v-model:value="data.query.minWatchDuration" allowClear :placeholder="$t('robot.giftConfig.minWatchDuration')" style="width: 120px" />
        <InputNumber v-model:value="data.query.maxWatchDuration" allowClear :placeholder="$t('robot.giftConfig.maxWatchDuration')" style="width: 120px" />
        <InputNumber v-model:value="data.query.minOnlineUsers" allowClear :placeholder="$t('robot.giftConfig.minOnlineUsers')" style="width: 120px" />
        <InputNumber v-model:value="data.query.maxOnlineUsers" allowClear :placeholder="$t('robot.giftConfig.maxOnlineUsers')" style="width: 120px" />
        <InputNumber v-model:value="data.query.minAmount" allowClear :placeholder="$t('robot.giftConfig.minAmount')" style="width: 120px" />
        <InputNumber v-model:value="data.query.maxAmount" allowClear :placeholder="$t('robot.giftConfig.maxAmount')" style="width: 120px" />
        <InputNumber v-model:value="data.query.dailyAnchonum" allowClear :placeholder="$t('robot.giftConfig.dailyAnchonum')" style="width: 120px" />
        <InputNumber v-model:value="data.query.giftBatchNum" allowClear :placeholder="$t('robot.giftConfig.giftBatchNum')" style="width: 120px" />
        <Input v-model:value="data.query.creator" allowClear :placeholder="$t('robot.giftConfig.creator')" /> -->
        <!-- 查询按钮 -->
        <Button @click="handleQuery" :loading="data.loading" :disabled="data.loading"
          style="background-color: #0084CA;color: white">
          {{ $t('common.search') }}
        </Button>
        <Button @click="resetQuery" :loading="data.loading" :disabled="data.loading" danger>
          {{ $t('common.reset') }}
        </Button>
      </Space>

      <!-- 全局功能 -->
      <div style="padding: 15px 0; display: flex; justify-content: space-between; align-items: center;">
        <Button @click="handleOpenAdd" style="background-color: #0084CA;color: white">
          {{ $t('common.add') }}
        </Button>
      </div>
      </Col>
    </Row>

    <!-- 数据展示 -->
    <Table :dataSource="data.datas" :columns="data.columns" bordered :loading="data.loading"
      :pagination="data.pagination" :scroll="{ x: 2000 }" @change="handleQuery" size="small">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'">
          <Button type="link" size="small" @click="handleOpenGiftRelation(record)">指定礼物</Button>
          <Button type="link" size="small" @click="handleOpenEdit(record)">{{ $t('common.edit') }}</Button>
          <Button type="link" size="small" @click="handleDeleteConfirm(record)">{{ $t('common.delete') }}</Button>
        </template>
        <template v-else-if="column.dataIndex === 'minAmount' || column.dataIndex === 'maxAmount'">
          <span>{{ record[column.dataIndex] }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'minWatchDuration' || column.dataIndex === 'maxWatchDuration'">
          <span>{{ record[column.dataIndex] }}秒</span>
        </template>
      </template>
    </Table>

    <!-- 添加 -->
    <Add ref="addRef" @close="reload"></Add>
    <!-- 编辑 -->
    <Edit ref="editRef" @close="reload"></Edit>
    <!-- 删除 -->
    <DeleteConfirm ref="deleteRef"></DeleteConfirm>
    <!-- 礼物关联管理 -->
    <GiftRelation ref="giftRelationRef" @close="reload"></GiftRelation>
  </Page>
</template>

<script setup lang="ts">
import DeleteConfirm from "#/components/myui/deleteConfirm/index.vue";
import Add from "./components/add.vue";
import Edit from "./components/edit.vue";
import GiftRelation from "./components/giftRelation.vue";
import { reactive, onMounted, ref } from "vue";
import { $t } from '@vben/locales';
import { Page } from '@vben/common-ui';
import { Button, Input, Space, Table, Row, Col, message, InputNumber } from 'ant-design-vue';
import { giftConfig } from '#/api';
import { d } from './index.ts';

const data = reactive({
  ...d,
});

const addRef = ref();
const deleteRef = ref();
const editRef = ref();
const giftRelationRef = ref();

onMounted(() => {
  handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
})

function handleOpenAdd() {
  addRef.value._open();
}

function handleOpenEdit(item) {
  editRef.value._open(item);
}

function handleOpenGiftRelation(item) {
  giftRelationRef.value._open(item);
}

function handleDeleteConfirm(item) {
  deleteRef.value._open($t('common.deleteConfirm') + '【' + item.configName + '】?').then(() => {
    giftConfig.removeByUid(item).then((res) => {
      if (res.error) {
        return;
      }
      message.success($t('common.deleteSuccess'));
      handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
    })
  })
}

/**
 * 包装查询参数
 * @param page
 * @param filter
 * @param sorter
 */
function wrapQueryParam(page, filter, sorter) {
  let { current, pageSize } = { ...data.pagination, ...(page || {}) };
  let param = {
    pageNum: current,
    pageSize: pageSize,
  }
  //查询
  for (const key in data.query) {
    if (data.query[key] == null || data.query[key] === '') {
      continue;
    }
    param[key] = data.query[key];
  }
  //过滤
  if (filter) {
    for (const key in filter) {
      const value = Array.isArray(filter[key]) && filter[key].length > 0 ? filter[key][0] : filter[key];
      param[key] = value || param[key];
    }
  }
  //排序
  if (sorter && sorter.field && sorter.order) {
    param.sorts = [{ name: sorter.field, desc: sorter.order == 'descend' }]
  }
  return param;
}

/**
 * 重置查询
 */
function resetQuery() {
  data.query = {}
  handleQuery({ current: 1, pageSize: data.pagination.pageSize });
}

/**
 * 查询
 * @param page
 * @param filter
 * @param sorter
 */
function handleQuery(page, filter, sorter) {
  data.loading = true;
  let param = wrapQueryParam(page, filter, sorter);
  giftConfig.queryPage(param).then((res) => {
    if (res.error) {
      return;
    }
    data.datas = res.data.list || [];
    data.pagination.total = res.data.total || 0;
    data.pagination.current = res.data.current || res.data.pageNum || 1;
    data.pagination.pageSize = res.data.pageSize && res.data.pageSize > 10 ? res.data.pageSize : 10;
    
  }).finally(() => {
    data.loading = false;
  })
}

function reload() {
  handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
}
</script>