<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约id" prop="reservationId">
        <el-input
          v-model="queryParams.reservationId"
          placeholder="请输入预约id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游客真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入游客真实姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游客身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入游客身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游客手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入游客手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入场验证时间" prop="verifyTime">
        <el-date-picker clearable
          v-model="queryParams.verifyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择入场验证时间">
        </el-date-picker>
      </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="reservationVisitorList">
      <el-table-column label="明细ID" align="center" prop="id" />
      <el-table-column label="预约id" align="center" prop="reservationId" />
      <el-table-column label="游客真实姓名" align="center" prop="realName" />
      <el-table-column label="游客身份证号" align="center" prop="idCard" />
      <el-table-column label="游客手机号" align="center" prop="phone" />
      <el-table-column label="游客个人入场二维码" align="center" prop="visitorQrCode" />
      <el-table-column label="游客入场验证状态" align="center" prop="verifyStatus" />
      <el-table-column label="入场验证时间" align="center" prop="verifyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.verifyTime, 'yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <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="reservationVisitorRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约id" prop="reservationId">
          <el-input v-model="form.reservationId" placeholder="请输入预约id" />
        </el-form-item>
        <el-form-item label="游客真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入游客真实姓名" />
        </el-form-item>
        <el-form-item label="游客身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入游客身份证号" />
        </el-form-item>
        <el-form-item label="游客手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入游客手机号" />
        </el-form-item>
        <el-form-item label="游客个人入场二维码" prop="visitorQrCode">
          <el-input v-model="form.visitorQrCode" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="入场验证时间" prop="verifyTime">
          <el-date-picker clearable
            v-model="form.verifyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择入场验证时间">
          </el-date-picker>
        </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="ReservationVisitor">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { listReservationVisitor, getReservationVisitor, delReservationVisitor, addReservationVisitor, updateReservationVisitor } from "@/api/reservationVisitor";

const { proxy } = getCurrentInstance();

const reservationVisitorList = 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,
    reservationId: null,
    realName: null,
    idCard: null,
    phone: null,
    visitorQrCode: null,
    verifyStatus: null,
    verifyTime: null
  },
  rules: {
    reservationId: [
      { required: true, message: "预约id不能为空", trigger: "blur" }
    ],
    realName: [
      { required: true, message: "游客真实姓名不能为空", trigger: "blur" }
    ],
    idCard: [
      { required: true, message: "游客身份证号不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "游客手机号不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询游客预约明细列表 */
function getList() {
  loading.value = true;
  listReservationVisitor(queryParams.value).then(response => {
    reservationVisitorList.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,
    reservationId: null,
    realName: null,
    idCard: null,
    phone: null,
    visitorQrCode: null,
    verifyStatus: null,
    verifyTime: null
  };
  const reservationVisitorRef = proxy.$refs["reservationVisitorRef"]
  if (reservationVisitorRef) {
    reservationVisitorRef.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
  getReservationVisitor(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改游客预约明细";
    isAdd.value = false;
  });
}

/** 表单提交按钮 */
function submitForm() {
  proxy.$refs["reservationVisitorRef"].validate(valid => {
    if (valid) {
      if (isAdd.value) {  // 添加
        addReservationVisitor(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      } else {  // 更新
        updateReservationVisitor(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 delReservationVisitor(row.id);
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

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

getList();
</script>
