<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="书屋名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入书屋名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书屋详细位置" prop="roomAddr">
        <el-input
          v-model="queryParams.roomAddr"
          placeholder="请输入书屋详细位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="RefreshRight" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col style="margin-left: 30px" :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['room:room:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['room:room:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['room:room:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['room:room:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      style="margin: 15px 30px"
      v-loading="loading"
      :data="roomList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="书屋id" align="center" prop="id" /> -->
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="书屋名称" align="center" prop="roomName" />
      <el-table-column label="书屋详细位置" align="center" prop="roomAddr" />
      <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="['room:room:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['room:room: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-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="roomRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="书屋名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入书屋名称" />
        </el-form-item>
        <el-form-item label="书屋详细位置" prop="roomAddr">
          <el-input v-model="form.roomAddr" placeholder="请输入书屋详细位置" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">书架信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddYBookShelf"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="Delete"
              @click="handleDeleteYBookShelf"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="yBookShelfList"
          :row-class-name="rowYBookShelfIndex"
          @selection-change="handleYBookShelfSelectionChange"
          ref="yBookShelf"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="书架编号" prop="shelfCode" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.shelfCode"
                placeholder="请输入书架编号"
              />
            </template>
          </el-table-column>
          <el-table-column label="详细位置" prop="addrInfo" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.addrInfo"
                placeholder="请输入详细位置"
              />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </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="Room">
import {
  listRoom,
  getRoom,
  delRoom,
  addRoom,
  updateRoom,
} from "@/api/books/room";

const { proxy } = getCurrentInstance();

const roomList = ref([]);
const yBookShelfList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedYBookShelf = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: null,
    roomAddr: null,
  },
  rules: {},
});

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

/** 查询书屋列表 */
function getList() {
  loading.value = true;
  listRoom(queryParams.value).then((response) => {
    roomList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    roomName: null,
    roomAddr: null,
    createBy: null,
    createTime: null,
    remark: null,
  };
  yBookShelfList.value = [];
  proxy.resetForm("roomRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加书屋";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getRoom(_id).then((response) => {
    form.value = response.data;
    yBookShelfList.value = response.data.yBookShelfList;
    open.value = true;
    title.value = "修改书屋";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roomRef"].validate((valid) => {
    if (valid) {
      form.value.yBookShelfList = yBookShelfList.value;
      if (form.value.id != null) {
        updateRoom(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRoom(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除书屋编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delRoom(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 书架序号 */
function rowYBookShelfIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 书架添加按钮操作 */
function handleAddYBookShelf() {
  let obj = {};
  obj.shelfCode = "";
  obj.addrInfo = "";
  obj.remark = "";
  yBookShelfList.value.push(obj);
}

/** 书架删除按钮操作 */
function handleDeleteYBookShelf() {
  if (checkedYBookShelf.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的书架数据");
  } else {
    const yBookShelfs = yBookShelfList.value;
    const checkedYBookShelfs = checkedYBookShelf.value;
    yBookShelfList.value = yBookShelfs.filter(function (item) {
      return checkedYBookShelfs.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleYBookShelfSelectionChange(selection) {
  checkedYBookShelf.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "room/room/export",
    {
      ...queryParams.value,
    },
    `room_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
