<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="申请单编号" prop="applicationCode">
              <el-input v-model="queryParams.applicationCode" placeholder="请输入申请单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="礼品名称" prop="giftName">
              <el-input v-model="queryParams.giftName" placeholder="请输入礼品名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="领取人" prop="recipientName">
              <el-input v-model="queryParams.recipientName" placeholder="请输入领取人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发放时间" prop="issueDate">
              <el-date-picker clearable v-model="queryParams.issueDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oa/crm:crmGiftIssueRecord:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="crmGiftIssueRecordList">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column label="申请单编号" align="center" prop="applicationCode" min-width="140" v-if="columns[0].visible" />
        <el-table-column label="礼品编码" align="center" prop="giftCode" min-width="100" v-if="columns[1].visible" />
        <el-table-column label="礼品名称" align="center" prop="giftName" min-width="120" v-if="columns[2].visible" />
        <el-table-column label="规格型号" align="center" prop="specification" min-width="100" v-if="columns[3].visible" />
        <el-table-column label="发放数量" align="center" prop="issueQuantity" width="90" v-if="columns[4].visible" />
        <el-table-column label="发放时间" align="center" prop="issueDate" width="160" v-if="columns[5].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="领取人" align="center" prop="recipientName" min-width="100" v-if="columns[6].visible" />
        <el-table-column label="发放人" align="center" prop="issueByName" min-width="100" v-if="columns[7].visible" />
        <el-table-column label="发放部门" align="center" prop="issueDeptName" min-width="120" v-if="columns[8].visible" />
        <el-table-column label="发放前库存" align="center" prop="beforeInventory" width="100" v-if="columns[9].visible" />
        <el-table-column label="发放后库存" align="center" prop="afterInventory" width="100" v-if="columns[10].visible" />
        <el-table-column label="备注" align="center" prop="remark" min-width="150" show-overflow-tooltip v-if="columns[11].visible" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- P1-5: 发放记录通过申请单发放功能生成，列表页移除新增/修改/删除对话框 -->
  </div>
</template>

<script setup name="CrmGiftIssueRecord" lang="ts">
import { listCrmGiftIssueRecord } from '@/api/oa/crm/crmGiftIssueRecord';
import { CrmGiftIssueRecordQuery } from '@/api/oa/crm/crmGiftIssueRecord/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const crmGiftIssueRecordList = ref<CrmGiftIssueRecordVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `申请单编号`, visible: true },
  { key: 1, label: `礼品编码`, visible: true },
  { key: 2, label: `礼品名称`, visible: true },
  { key: 3, label: `规格型号`, visible: true },
  { key: 4, label: `发放数量`, visible: true },
  { key: 5, label: `发放时间`, visible: true },
  { key: 6, label: `领取人`, visible: true },
  { key: 7, label: `发放人`, visible: true },
  { key: 8, label: `发放部门`, visible: true },
  { key: 9, label: `发放前库存`, visible: true },
  { key: 10, label: `发放后库存`, visible: true },
  { key: 11, label: `备注`, visible: true }
]);

const queryParams = reactive<CrmGiftIssueRecordQuery>({
  pageNum: 1,
  pageSize: 10,
  applicationCode: undefined,
  giftName: undefined,
  recipientName: undefined,
  issueDate: undefined
});

/** 查询礼品发放记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCrmGiftIssueRecord(queryParams);
  crmGiftIssueRecordList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oa/crm/crmGiftIssueRecord/export',
    {
      ...queryParams.value
    },
    `crmGiftIssueRecord_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
