<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">
            <el-form-item label="单据编号" prop="orderRevenueCode">
              <el-input v-model="queryParams.orderRevenueCode" placeholder="请输入单据编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单据日期" prop="createTime">
              <el-date-picker v-model="queryParams.createTime" placeholder="请选择单据日期" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="关键字" prop="searchValue">
              <el-input v-model="queryParams.searchValue" placeholder="供应商名称/编号" clearable @keyup.enter="handleQuery" />
            </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="22"> 费用单信息列表 </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="revenueHeaderList" @selection-change="handleSelectionChange">
        <el-table-column label="单据日期" align="center" prop="createTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单据编号" align="center" prop="orderRevenueCode" />
        <el-table-column label="供应商" align="center" prop="supplierName" />
        <el-table-column label="产品经理" align="center" prop="" />
        <el-table-column label="采购员" align="center" prop="" />
        <el-table-column label="含税金额" align="center" prop="totalAmount">
          <template #default="scope">
            <span>{{ Number(scope.row.totalAmount || 0).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:revenueHeader:edit']">编辑</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:revenueHeader:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改收入单主对话框 -->
  </div>
</template>

<script setup name="RevenueHeader" lang="ts">
import { listRevenueHeader, delRevenueHeader } from '@/api/order/revenueHeader';
import { RevenueHeaderVO, RevenueHeaderQuery, RevenueHeaderForm } from '@/api/order/revenueHeader/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_platform_yes_no, check_status, revenue_type } = toRefs<any>(proxy?.useDict('sys_platform_yes_no', 'check_status', 'revenue_type'));

const revenueHeaderList = ref<RevenueHeaderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const router = useRouter();
const queryFormRef = ref<ElFormInstance>();
const revenueHeaderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: RevenueHeaderForm = {
  id: undefined,
  revenueType: undefined,
  orderRevenueCode: undefined,
  incomeOrderCode: undefined,
  otherRevenueType: undefined,
  customerId: undefined,
  supplierId: undefined,
  isPrwTax: undefined,
  currencyId: undefined,
  pushStatus: undefined,
  orderFile: undefined,
  status: undefined,
  remark: undefined
};
const data = reactive<PageData<RevenueHeaderForm, RevenueHeaderQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    revenueType: '1',
    orderRevenueCode: undefined,
    incomeOrderCode: undefined,
    otherRevenueType: undefined,
    customerId: undefined,
    supplierId: undefined,
    isPrwTax: undefined,
    currencyId: undefined,
    pushStatus: undefined,
    orderFile: undefined,
    status: undefined,
    platformCode: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询收入单主列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRevenueHeader(queryParams.value);
  revenueHeaderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  revenueHeaderFormRef.value?.resetFields();
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: RevenueHeaderVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: RevenueHeaderVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  router.push({
    path: '/order-manage/order-revenue-add',
    query: { revenueType: row.revenueType, id: _id }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: RevenueHeaderVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除收入单主编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delRevenueHeader(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

onMounted(() => {
  getList();
});

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