<template>
  <div class="p-2 flex storeRoomBox">
    <Pageheader title="储藏室管理"></Pageheader>
    <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 label-width="100" ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="储藏室号" prop="storeroomNo">
              <el-input v-model="queryParams.storeroomNo" 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="['storeroom:storeroom:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['storeroom:storeroom:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['storeroom:storeroom:remove']"
              >删除</el-button
            >
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table class="tablebody" v-loading="loading" border :data="storeroomList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="楼栋名" align="center" prop="buildingName" />
        <el-table-column label="单元号" align="center" prop="unitNo" />
        <el-table-column label="储藏室号" align="center" prop="storeroomNo" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="houseArea" />
        <el-table-column label="公摊面积(㎡)" align="center" prop="shareArea" />
        <el-table-column label="实用面积(㎡)" align="center" prop="internalArea" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" fixed="right" 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="['storeroom:storeroom:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['storeroom:storeroom: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="Storeroom" lang="ts">
import Pageheader from '@/components/Pageheader/index.vue';
import { listStoreroom, getStoreroom, delStoreroom, addStoreroom, updateStoreroom } from '@/api/system/storeroom/index';
import { StoreroomVO, StoreroomQuery, StoreroomForm } from '@/api/system/storeroom/types';
import { getBuildinglists } from '@/api/system/house';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const router = useRouter();
const storeroomList = ref<StoreroomVO[]>([]);
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 storeroomFormRef = ref<ElFormInstance>();

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

const initFormData: StoreroomForm = {
  id: undefined,
  buildingId: undefined,
  unitNo: undefined,
  storeroomNo: undefined,
  houseArea: undefined,
  shareArea: undefined,
  internalArea: undefined,
  houseId: undefined,
  status: undefined,
  remark: undefined
};
const data = reactive<PageData<StoreroomForm, StoreroomQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    buildingId: undefined,
    unitNo: undefined,
    storeroomNo: undefined,
    houseArea: undefined,
    shareArea: undefined,
    internalArea: undefined,
    houseId: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    storeroomNo: [{ required: true, message: '储藏室号不能为空', trigger: 'blur' }]
  }
});

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

const buildinglist = ref([]);
function getbudilinglist() {
  getBuildinglists().then((res) => {
    buildinglist.value = res.rows;
  });
}
getbudilinglist();

/** 查询储藏室管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStoreroom(queryParams.value);
  storeroomList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/house/addStoreRoom'
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: StoreroomVO) => {
  router.push({
    path: '/house/EditStoreRoom',
    query: {
      id: row.id
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: StoreroomVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('该信息删除后无法恢复，确定要删除吗？').finally(() => (loading.value = false));
  await delStoreroom(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

<style lang="scss" scoped>
.storeRoomBox {
  flex-direction: column;
  height: 100%;
  & > .el-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    .tablebody {
      flex: 1;
      display: flex;
      flex-direction: column;
      height: calc(100% - 80px);
    }
  }
}
</style>
