<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约日期" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预约日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最大人数" prop="maxPeople">
        <el-input
          v-model="queryParams.maxPeople"
          placeholder="请输入最大人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已预约人数" prop="currentPeople">
        <el-input
          v-model="queryParams.currentPeople"
          placeholder="请输入已预约人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者id" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建者id"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col>-->
    </el-row>
    <br>

    <el-table v-loading="loading" :data="reservationTimeSlotList">
      <el-table-column label="时段id" align="center" prop="id" />
      <el-table-column label="预约日期" align="center" prop="date" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.date, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最大人数" align="center" prop="maxPeople" />
      <el-table-column label="已预约人数" align="center" prop="currentPeople" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="创建者id" align="center" prop="creator" />
      <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)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryParams.pageSize"
        layout="->, total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 添加或修改预约时段对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reservationTimeSlotRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择预约日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最大人数" prop="maxPeople">
          <el-input v-model="form.maxPeople" placeholder="请输入最大人数" />
        </el-form-item>
        <el-form-item label="已预约人数" prop="currentPeople">
          <el-input v-model="form.currentPeople" placeholder="请输入已预约人数" />
        </el-form-item>
        <el-form-item label="创建者id" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建者id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ReservationTimeSlot">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { listReservationTimeSlot, getReservationTimeSlot, delReservationTimeSlot, addReservationTimeSlot, updateReservationTimeSlot } from "@/api/reservationTimeSlot";

const { proxy } = getCurrentInstance();

const reservationTimeSlotList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const isAdd = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    date: null,
    startTime: null,
    endTime: null,
    maxPeople: null,
    currentPeople: null,
    status: null,
    creator: null
  },
  rules: {
    date: [
      { required: true, message: "预约日期不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    maxPeople: [
      { required: true, message: "最大人数不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询预约时段列表 */
function getList() {
  loading.value = true;
  listReservationTimeSlot(queryParams.value).then(response => {
    reservationTimeSlotList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

// 改变每页条数大小
function handleSizeChange(val) {
  queryParams.value.pageSize = val;
  getList();
}

// 换页
function handleCurrentChange(val) {
  queryParams.value.pageNum = val;
  getList();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    date: null,
    startTime: null,
    endTime: null,
    maxPeople: null,
    currentPeople: null,
    status: null,
    createTime: null,
    creator: null
  };
  const reservationTimeSlotRef = proxy.$refs["reservationTimeSlotRef"]
  if (reservationTimeSlotRef) {
    reservationTimeSlotRef.resetFields();
  }
}

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

/** 重置按钮操作 */
function resetQuery() {
  const queryRef = proxy.$refs["queryRef"]
  if (queryRef) {
    queryRef.resetFields();
  }
  handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预约时段";
  isAdd.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getReservationTimeSlot(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预约时段";
    isAdd.value = false;
  });
}

/** 表单提交按钮 */
function submitForm() {
  proxy.$refs["reservationTimeSlotRef"].validate(valid => {
    if (valid) {
      if (isAdd.value) {  // 添加
        addReservationTimeSlot(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      } else {  // 更新
        updateReservationTimeSlot(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$confirm('是否确认删除预约时段编号为"' + row.id + '"的数据项？').then(function() {
    return delReservationTimeSlot(row.id);
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/reservationTimeSlot/export', {
//     ...queryParams.value
//   }, `reservationTimeSlot_${new Date().getTime()}.xlsx`)
// }

getList();
</script>
