<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="60px">
      <el-form-item label="餐别" prop="mealType">
        <el-select v-model="queryParams.mealType" placeholder="请选择餐别" clearable>
          <el-option
            v-for="dict in dict.type.hrm_meal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用餐日期" prop="reportDate">
        <el-date-picker
          v-model="queryParams.reportDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择用餐日期"
          clearable
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <DictSelect dictType="hrm_department" v-model="queryParams.deptName" placeholder="请选择部门名称"></DictSelect>
      </el-form-item>
      <el-form-item label="报餐人" prop="reportUserName">
        <DictSelect dictType="hrm_leave_employee" v-model="queryParams.reportUserName" placeholder="请选择报餐人姓名"></DictSelect>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- ✅ 新增：表格上方的人数合计统计组件 el-description -->
    <el-descriptions size="small" border class="mb8" :column="3">
      <el-descriptions-item label="堂食人数合计">{{ totalDineIn }}</el-descriptions-item>
      <el-descriptions-item label="打包人数合计">{{ totalTakeout }}</el-descriptions-item>
      <el-descriptions-item label="用餐总人数合计">{{ totalAll }}</el-descriptions-item>
    </el-descriptions>

    <el-table v-loading="loading" :data="mealReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="reportId" v-if="false"/>
      <el-table-column label="用餐日期" align="center" prop="reportDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="餐别" align="center" prop="mealType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hrm_meal_type" :value="scope.row.mealType"/>
        </template>
      </el-table-column>
      <el-table-column label="部门名称" align="center" prop="deptName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hrm_department" :value="scope.row.deptName"/>
        </template>
      </el-table-column>
      <el-table-column label="用餐总人数" align="center" prop="totalPeople" />
      <el-table-column label="堂食人数" align="center" prop="dineInPeople" />
      <el-table-column label="打包人数" align="center" prop="takeoutPeople" />
      <el-table-column label="报餐人姓名" align="center" prop="reportUserName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hrm_leave_employee" :value="scope.row.reportUserName"/>
        </template>
      </el-table-column>
       <el-table-column label="报餐时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</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 slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改部门报餐主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用餐日期" prop="reportDate">
          <el-date-picker clearable
            v-model="form.reportDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择用餐日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="餐别" prop="mealType">
          <el-select v-model="form.mealType" placeholder="请选择餐别">
            <el-option
              v-for="dict in dict.type.hrm_meal_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <DictSelect dictType="hrm_department" v-model="form.deptName" placeholder="请选择部门名称"></DictSelect>
        </el-form-item>
        <!-- 优化：增加number类型，禁止输入非数字，限制最小值0 -->
        <el-form-item label="堂食人数" prop="dineInPeople">
          <el-input v-model="form.dineInPeople" placeholder="请输入堂食人数" type="number" min="0" />
        </el-form-item>
        <el-form-item label="打包人数" prop="takeoutPeople">
          <el-input v-model="form.takeoutPeople" placeholder="请输入打包人数" type="number" min="0" />
        </el-form-item>
        <el-form-item label="用餐总人数" prop="totalPeople">
          <el-input v-model="form.totalPeople" placeholder="请输入用餐总人数" disabled />
        </el-form-item>
        <el-form-item label="报餐人姓名" prop="reportUserName">
          <DictSelect dictType="hrm_leave_employee" v-model="form.reportUserName" placeholder="请选择报餐人姓名"></DictSelect>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMealReport, getMealReport, delMealReport, addMealReport, updateMealReport } from "@/api/wms/mealReport";
import DictSelect from "@/components/DictSelect";

export default {
  name: "MealReport",
  dicts: ['hrm_meal_type', 'hrm_leave_employee', 'hrm_department'],
  components: { DictSelect },
  data() {
    // 用餐日期默认选中今天
    // 格式化日期为yyyy-MM-dd
    function addZero(num) {
      return num < 10 ? '0' + num : num;
    }
    const now = new Date()
    const nowYear = now.getFullYear()
    const nowMonth = addZero(now.getMonth() + 1)
    const nowDay = addZero(now.getDate())
    const nowTime = `${nowYear}-${nowMonth}-${nowDay}`
    return {
      buttonLoading: false,
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      mealReportList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        reportDate: nowTime,
        mealType: undefined,
        deptName: undefined,
        reportUserName: undefined,
        status: undefined,
      },
      form: {},
      // 合计统计的三个变量 ✅新增
      totalDineIn: 0,  // 堂食合计
      totalTakeout: 0, // 打包合计
      totalAll: 0,     // 总人数合计
      // 表单校验规则
      rules: {
        reportDate: [{ required: true, message: '用餐日期不能为空', trigger: 'change' }],
        mealType: [{ required: true, message: '餐别不能为空', trigger: 'change' }],
        deptName: [{ required: true, message: '部门名称不能为空', trigger: 'change' }],
        dineInPeople: [{ required: true, message: '堂食人数不能为空', trigger: 'blur' }],
        takeoutPeople: [{ required: true, message: '打包人数不能为空', trigger: 'blur' }],
        totalPeople: [{ required: true, message: '用餐总人数不能为空', trigger: 'blur' }],
        reportUserName: [{ required: true, message: '报餐人姓名不能为空', trigger: 'change' }]
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    'form.dineInPeople': {
      handler(val) {
        this.calcTotalPeople();
      },
      immediate: true
    },
    'form.takeoutPeople': {
      handler(val) {
        this.calcTotalPeople();
      },
      immediate: true
    }
  },
  methods: {
    /** 查询部门报餐主列表 */
    getList() {
      this.loading = true;
      listMealReport(this.queryParams).then(response => {
        this.mealReportList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.calcTableSum(); // ✅新增：获取表格数据后，立即计算合计
      });
    },
    // ✅ 核心新增：计算表格中堂食、打包、总人数的合计方法
    calcTableSum(){
        let dineIn = 0;
        let takeout = 0;
        let all = 0;
        // 遍历表格数据累加，处理空值/非数字，避免NaN
        this.mealReportList.forEach(item => {
            dineIn += item.dineInPeople ? Number(item.dineInPeople) : 0;
            takeout += item.takeoutPeople ? Number(item.takeoutPeople) : 0;
            all += item.totalPeople ? Number(item.totalPeople) : 0;
        });
        // 赋值到统计变量
        this.totalDineIn = dineIn;
        this.totalTakeout = takeout;
        this.totalAll = all;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: undefined,
        reportDate: undefined,
        mealType: undefined,
        deptName: undefined,
        totalPeople: undefined,
        dineInPeople: undefined,
        takeoutPeople: undefined,
        reportUserName: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加部门报餐主";
      const now = new Date();
      const year = now.getFullYear();
      const month = (now.getMonth() + 1).toString().padStart(2, '0');
      const day = now.getDate().toString().padStart(2, '0');
      const hour = now.getHours().toString().padStart(2, '0');
      const minute = now.getMinutes().toString().padStart(2, '0');
      const second = now.getSeconds().toString().padStart(2, '0');
      this.form.reportDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const reportId = row.reportId || this.ids
      getMealReport(reportId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改部门报餐主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.reportId != null) {
            updateMealReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMealReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除部门报餐主编号为"' + reportIds + '"的数据项？').then(() => {
        this.loading = true;
        return delMealReport(reportIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/mealReport/export', { ...this.queryParams }, `mealReport_${new Date().getTime()}.xlsx`)
    },
    // 计算单行总人数
    calcTotalPeople() {
      const dine = this.form.dineInPeople ? Number(this.form.dineInPeople) : 0;
      const take = this.form.takeoutPeople ? Number(this.form.takeoutPeople) : 0;
      this.form.totalPeople = dine + take;
    }
  }
};
</script>