<template>
  <div class="AddBuildingBox">
    <PageHeader :title="houseid ? '编辑房屋' : '添加房屋'"></PageHeader>
    <div class="AddBuildingBody">
      <div class="title">基本信息</div>
      <div class="addBuildingFormBody">
        <el-form class="formBody" label-position="left" ref="formRef" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="楼栋" prop="buildingId">
            <el-select @change="handlechangeBuild" v-model="form.buildingId" class="m-2" placeholder="请选择">
              <el-option v-for="item in houseStore.currentBuildingInfoList" :key="item.id" :label="item.buildingName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="单元" prop="unit">
            <el-select v-model.number="form.unitNo" class="m-2" placeholder="请选择">
              <el-option v-for="item in units" :key="item.no" :label="item.name" :value="item.no" />
            </el-select>
          </el-form-item>
          <el-form-item label="楼层" prop="floor">
            <el-select v-model.number="form.floorNo" class="m-2" placeholder="请选择">
              <el-option v-for="item in floors" :key="item.no" :label="item.name" :value="item.no" />
            </el-select>
          </el-form-item>
          <el-form-item label="房间号" prop="houseNo">
            <el-input v-model="form.houseNo" placeholder="请输入房间号" />
          </el-form-item>
          <el-form-item label="建筑面积" prop="houseArea">
            <div class="unitBox flex align-center">
              <el-input v-model="form.houseArea" placeholder="请输入建筑面积" />
              <span>㎡</span>
            </div>
          </el-form-item>
          <el-form-item label="套内面积" prop="internalArea">
            <div class="unitBox flex align-center">
              <el-input v-model="form.internalArea" placeholder="请输入套内面积" />
              <span>㎡</span>
            </div>
          </el-form-item>
          <el-form-item label="公摊面积" prop="shareArea">
            <div class="unitBox flex align-center">
              <el-input v-model="form.shareArea" placeholder="请输入公摊面积" />
              <span>㎡</span>
            </div>
          </el-form-item>
          <el-form-item label="房屋户型">
            <div class="houseTypebox">
              <el-input v-model="housetype[0]" placeholder="室" />
              <span>室</span>
              <el-input v-model="housetype[1]" placeholder="厅" />
              <span>厅</span>
              <el-input v-model="housetype[2]" placeholder="卫" />
              <span>卫</span>
            </div>
          </el-form-item>
          <!-- <el-form-item label="车位号" prop="buildingAddress">
            <el-select v-model="form.ParkingSpaceNumber" class="m-2" placeholder="请选择车位号">
              <el-option v-for="item in building" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="储藏室" prop="buildingDescription">
            <el-select v-model="form.storeroom" class="m-2" placeholder="请选择储藏室">
              <el-option v-for="item in building" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item> -->
          <el-form-item style="margin-top: 30px">
            <el-button type="primary" @click="submitForm">确定</el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { useHouseStore } from '@/store/modules/house';
import { addHouseAPI, EditHouseApi, getHouseApi, getHouselistAPI } from '@/api/system/house';
import { addHouseType } from '@/api/system/house/type';

const router = useRouter();
const route = useRoute();
const houseStore = useHouseStore();
const formRef = ref();
const form = ref<addHouseType>({
  buildingId: '',
  unitNo: '',
  floorNo: '',
  houseNo: '', // 房间号
  houseArea: '', //建筑面积
  internalArea: '', //套内面积
  shareArea: '', // 公摊面积
  houseType: '' // 房屋户型
});
const rules = ref({
  buildingId: [{ required: true, message: '请输入楼栋', trigger: 'blur' }],
  unitNo: [{ required: true, message: '请选择单元', trigger: 'blur' }],
  floorNo: [{ required: true, message: '请选择楼层', trigger: 'blur' }],
  houseUse: [{ required: true, message: '请选择房屋类型', trigger: 'blur' }],
  buildingName: [{ required: true, message: '请输入房间号', trigger: 'blur' }],
  buildingArea: [{ required: true, message: '请输入建筑面积', trigger: 'blur' }],
  internalFloorArea: [{ required: true, message: '请输入套内面积', trigger: 'blur' }],
  CommonArea: [{ required: true, message: '请输入公摊面积', trigger: 'blur' }]
});
const housetype = ref([]);
const units = ref([]);
const floors = ref([]);

const houseid = ref(null);
if (route.query.id) {
  houseid.value = route.query.id;
  getHouseApi(houseid.value).then((res) => {
    form.value = {
      ...form.value,
      buildingId: res.data.buildingId, // 楼栋id
      houseId: res.data.houseId, // 房屋id
      unitNo: res.data.unitNo,
      floorNo: res.data.floorNo,
      houseNo: res.data.houseNo, // 房间号
      houseArea: res.data.houseArea, //建筑面积
      internalArea: res.data.internalArea, //套内面积
      shareArea: res.data.shareArea // 公摊面积
    };
    housetype.value = res.data.houseType.split('');
    handlechangeBuild(form.value.buildingId);
    console.log(form.value);
  });
}

// 选择楼栋
function handlechangeBuild(id: string) {
  getHouselistAPI(id).then((res) => {
    units.value = Array.from({ length: res.data.unitCount }).map((_, index) => {
      return {
        no: index + 1,
        name: index + 1 + '单元'
      };
    });
    floors.value = Array.from({ length: res.data.floorCount }).map((_, index) => {
      return {
        no: index + 1,
        name: index + 1 + '楼'
      };
    });
    console.log(units.value, floors.value);
  });
}
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      const data = {
        ...form.value,
        houseType: housetype.value.join(''),
        villageId: houseStore.currentBuildingInfo.villageId
      };
      if (houseid.value) {
        EditHouseApi(data).then(() => {
          ElMessage.success('修改成功');
          houseStore.switchBuilding(houseStore.currentBuildingInfo.id);
          router.push({
            path: '/house/houselist'
          });
        });
      } else {
        addHouseAPI(data).then((res) => {
          console.log(res);
          ElMessage.success('添加成功');
          houseStore.switchBuilding(houseStore.currentBuildingInfo.id);
          router.push({
            path: '/house/houselist'
          });
        });
      }
    }
  });
};
const cancelForm = () => {
  formRef?.value.resetFields();
  router.push({
    path: '/house/houselist'
  });
};
</script>

<style scoped lang="scss">
.AddBuildingBox {
  padding: 15px;
  height: 100%;
  width: 100%;
}
.AddBuildingBody {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: white;
  padding: 15px;
  .title {
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    background-color: rgba(255, 255, 255, 1);
    color: rgba(16, 16, 16, 1);
    border-bottom: 1px solid #bbbbbb;
    margin-bottom: 60px;
  }

  .addBuildingFormBody {
    .formBody {
      width: 550px;
      margin: 0 auto;

      .el-form-item__content,
      .el-select,
      .el-input {
        width: 440px;
        margin-right: 10px;
      }
      .el-form-item {
        margin-bottom: 20px;
        align-items: center;
      }
      .unitBox {
        .el-input {
          width: 440px;
          margin-right: 10px;
        }
      }
      .el-button {
        width: 80px;
        height: 35px;
        margin-right: 20px;
      }

      .houseTypebox {
        display: flex;
        align-items: center;
        .el-input {
          width: 100px;
          margin-right: 0px;
        }
        span {
          margin: 0 20px;
        }
      }
    }
    padding-bottom: 40px;
  }
}
</style>
