<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="reportId">
              <el-input v-model="queryParams.reportId" placeholder="请输入报表主键" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
            <el-form-item label="报表年份" prop="reportYear">
              <el-input v-model="queryParams.reportYear" placeholder="请输入报表年份" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="报表月份" prop="reportMonth">
              <el-input v-model="queryParams.reportMonth" placeholder="请输入报表月份" clearable @keyup.enter="handleQuery" />
            </el-form-item>
<!--            <el-form-item label="物料类型ID" prop="materialTypeId">-->
<!--              <el-input v-model="queryParams.materialTypeId" placeholder="请输入物料类型ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="物料类型名称" prop="materialTypeName">-->
<!--              <el-input v-model="queryParams.materialTypeName" placeholder="请输入物料类型名称" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="当月入库记录数量" prop="currentInstockCount">-->
<!--              <el-input v-model="queryParams.currentInstockCount" placeholder="请输入当月入库记录数量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="当月入库总量" prop="currentInstockQty">-->
<!--              <el-input v-model="queryParams.currentInstockQty" placeholder="请输入当月入库总量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="当月出库记录数量" prop="currentOutstockCount">-->
<!--              <el-input v-model="queryParams.currentOutstockCount" placeholder="请输入当月出库记录数量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="当月出库总量" prop="currentOutstockQty">-->
<!--              <el-input v-model="queryParams.currentOutstockQty" placeholder="请输入当月出库总量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="上月入库记录数量" prop="lastInstockCount">-->
<!--              <el-input v-model="queryParams.lastInstockCount" placeholder="请输入上月入库记录数量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="上月入库总量" prop="lastInstockQty">-->
<!--              <el-input v-model="queryParams.lastInstockQty" placeholder="请输入上月入库总量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="上月出库记录数量" prop="lastOutstockCount">-->
<!--              <el-input v-model="queryParams.lastOutstockCount" placeholder="请输入上月出库记录数量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="上月出库总量" prop="lastOutstockQty">-->
<!--              <el-input v-model="queryParams.lastOutstockQty" placeholder="请输入上月出库总量" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="入库记录数量环比差额" prop="instockCountDiff">-->
<!--              <el-input v-model="queryParams.instockCountDiff" placeholder="请输入入库记录数量环比差额" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="入库总量环比差额" prop="instockQtyDiff">-->
<!--              <el-input v-model="queryParams.instockQtyDiff" placeholder="请输入入库总量环比差额" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="出库记录数量环比差额" prop="outstockCountDiff">-->
<!--              <el-input v-model="queryParams.outstockCountDiff" placeholder="请输入出库记录数量环比差额" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="出库总量环比差额" prop="outstockQtyDiff">-->
<!--              <el-input v-model="queryParams.outstockQtyDiff" placeholder="请输入出库总量环比差额" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="月末库存物料种类数" prop="inventoryMaterialCount">-->
<!--              <el-input v-model="queryParams.inventoryMaterialCount" placeholder="请输入月末库存物料种类数" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="月末库存总量" prop="inventoryQty">-->
<!--              <el-input v-model="queryParams.inventoryQty" 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="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wms:wmsMonthlyReport:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['wms:wmsMonthlyReport:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['wms:wmsMonthlyReport:remove']">删除</el-button>
          </el-col>-->
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['wms:wmsMonthlyReport: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" :data="wmsMonthlyReportList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" align="center" v-if="columns[0].visible"/>
        <el-table-column label="报表年份" align="center" width="120"  prop="reportYear" v-if="columns[1].visible"/>
        <el-table-column label="报表月份" align="center" width="120"  prop="reportMonth" v-if="columns[2].visible"/>
        <el-table-column label="物料类型" align="center"  width="120" prop="materialTypeId" v-if="columns[3].visible"/>
        <el-table-column label="物料类型名称" align="center" width="120"  prop="materialTypeName" v-if="columns[4].visible"/>
        <el-table-column label="当月入库记录数量" align="center"  width="140" prop="currentInstockCount" v-if="columns[5].visible"/>
        <el-table-column label="当月入库总量" align="center"  width="120" prop="currentInstockQty" v-if="columns[6].visible"/>
        <el-table-column label="当月出库记录数量" align="center"  width="140" prop="currentOutstockCount" v-if="columns[7].visible"/>
        <el-table-column label="当月出库总量" align="center" width="120"  prop="currentOutstockQty" v-if="columns[8].visible"/>
        <el-table-column label="上月入库记录数量" align="center"  width="140" prop="lastInstockCount" v-if="columns[9].visible"/>
        <el-table-column label="上月入库总量" align="center" width="120"  prop="lastInstockQty" v-if="columns[10].visible"/>
        <el-table-column label="上月出库记录数量" align="center" width="140"   prop="lastOutstockCount" v-if="columns[11].visible"/>
        <el-table-column label="上月出库总量" align="center" width="120"  prop="lastOutstockQty" v-if="columns[13].visible"/>
        <el-table-column label="入库记录数量环比差额" align="center" width="160"  prop="instockCountDiff" v-if="columns[13].visible"/>
        <el-table-column label="入库总量环比差额" align="center"  width="120" prop="instockQtyDiff" v-if="columns[14].visible"/>
        <el-table-column label="出库记录数量环比差额" align="center" width="160"  prop="outstockCountDiff" v-if="columns[15].visible"/>
        <el-table-column label="出库总量环比差额" align="center" width="120"  prop="outstockQtyDiff" v-if="columns[16].visible"/>
        <el-table-column label="月末库存物料种类数" align="center"  width="140"  prop="inventoryMaterialCount" v-if="columns[17].visible"/>
        <el-table-column label="月末库存总量" align="center"  width="120"  prop="inventoryQty" v-if="columns[18].visible"/>
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:wmsMonthlyReport:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:wmsMonthlyReport:remove']"></el-button>
            </el-tooltip>
          </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="WmsMonthlyReport" lang="ts">
import { listWmsMonthlyReport, getWmsMonthlyReport, delWmsMonthlyReport, addWmsMonthlyReport, updateWmsMonthlyReport } from '@/api/wms/wmsMonthlyReport';
import { WmsMonthlyReportVO, WmsMonthlyReportQuery, WmsMonthlyReportForm } from '@/api/wms/wmsMonthlyReport/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const wmsMonthlyReportList = ref<WmsMonthlyReportVO[]>([]);
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 queryFormRef = ref<ElFormInstance>();
const wmsMonthlyReportFormRef = ref<ElFormInstance>();

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

// 列显隐信息
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: 9, label: `当月出库总量`, visible: true },
        { key: 9, label: `上月入库记录数量`, visible: true },
        { key: 10, label: `上月入库总量`, visible: true },
        { key: 11, label: `上月出库记录数量`, visible: true },
        { key: 13, label: `上月出库总量`, visible: true },
        { key: 13, label: `入库记录数量环比差额`, visible: true },
        { key: 14, label: `入库总量环比差额`, visible: true },
        { key: 16, label: `出库记录数量环比差额`, visible: true },
        { key: 16, label: `出库总量环比差额`, visible: true },
        { key: 17, label: `月末库存物料种类数`, visible: true },
        { key: 18, label: `月末库存总量`, visible: true },
]);

const initFormData: WmsMonthlyReportForm = {
}
const data = reactive<PageData<WmsMonthlyReportForm, WmsMonthlyReportQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    reportId: undefined,
    reportYear: undefined,
    reportMonth: undefined,
    materialTypeId: undefined,
    materialTypeName: undefined,
    currentInstockCount: undefined,
    currentInstockQty: undefined,
    currentOutstockCount: undefined,
    currentOutstockQty: undefined,
    lastInstockCount: undefined,
    lastInstockQty: undefined,
    lastOutstockCount: undefined,
    lastOutstockQty: undefined,
    instockCountDiff: undefined,
    instockQtyDiff: undefined,
    outstockCountDiff: undefined,
    outstockQtyDiff: undefined,
    inventoryMaterialCount: undefined,
    inventoryQty: undefined,
    params: {
    }
  },
  rules: {
  }
});

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

/** 查询WMS月度报列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWmsMonthlyReport(queryParams.value);
  wmsMonthlyReportList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

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

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

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加WMS月度报";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: WmsMonthlyReportVO) => {
  reset();
  const _reportId = row?.reportId || ids.value[0]
  const res = await getWmsMonthlyReport(_reportId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改WMS月度报";
}

/** 提交按钮 */
const submitForm = () => {
  wmsMonthlyReportFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.reportId) {
        await updateWmsMonthlyReport(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addWmsMonthlyReport(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: WmsMonthlyReportVO) => {
  const _reportIds = row?.reportId || ids.value;
  await proxy?.$modal.confirm('是否确认删除WMS月度报编号为"' + _reportIds + '"的数据项？').finally(() => loading.value = false);
  await delWmsMonthlyReport(_reportIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

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

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