<template>
  <div class="app-container">
    <div class="form-container">
      <el-form label-width="120px" ref="form" :model="form" :rules="rules">
        <el-form-item label="楼盘：" prop="propertiesForSaleId">
          <!-- <el-input v-model="form.name"></el-input> -->
          <el-select v-model="form.propertiesForSaleId">
            <el-option
              v-for="item in buildingOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="类型：" prop="type">
          <!-- <el-input v-model="form.type"></el-input> -->
          <!-- 0 住房, 1 车位, 2 商铺 -->
          <el-select v-model="form.type">
            <el-option label="住房" :value="0"></el-option>
            <el-option label="车位" :value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="楼栋：" v-if="form.type === 0" prop="building">
          <el-input v-model="form.building"></el-input>
        </el-form-item>

        <el-form-item label="户型：" v-if="form.type === 0" prop="houseType">
          <el-input v-model="form.houseType"></el-input>
        </el-form-item>

        <el-form-item label="楼层：" prop="floor">
          <el-input v-model="form.floor"></el-input>
        </el-form-item>

        <el-form-item label="房号/车位号:" prop="roomNumber">
          <el-input v-model="form.roomNumber"></el-input>
        </el-form-item>

        <el-form-item label="面积（㎡）：" prop="area">
          <el-input v-model="form.area"></el-input>
        </el-form-item>

        <el-form-item label="价格：" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-button v-if="type === 'create'" type="primary" @click="handleCreate"
      >新建房源</el-button
    >
    <el-button v-if="type === 'update'" type="primary" @click="handleUpdate"
      >修改</el-button
    >
  </div>
</template>

<script>
import { add, update } from "@/api/housing";
import { list } from "@/api/building";
import { housingStatusOptions } from "@/utils";
export default {
  data() {
    return {
      type: "create",
      form: {},
      buildingOptions: [],
      housingStatusOptions,
      disabled: false,
      rules: {
        propertiesForSaleId: {
          required: true,
          message: "不能为空",
          trigger: "change",
        },
        type: { required: true, message: "不能为空", trigger: "change" },
        houseType: { required: true, message: "不能为空", trigger: "blur" },
        area: { required: true, message: "不能为空", trigger: "blur" },
        building: { required: true, message: "不能为空", trigger: "blur" },
        floor: { required: true, message: "不能为空", trigger: "blur" },
        roomNumber: { required: true, message: "不能为空", trigger: "blur" },
        price: { required: true, message: "不能为空", trigger: "blur" },
      },
    };
  },
  created() {
    const { query, name } = this.$route;
    if (name === "HousingCreate") {
      this.type = "create";
    } else if (name === "HousingUpdate") {
      this.type = "update";
      this.getInfo();
    } else {
      this.type = "detail";
      this.disabled = true;
      this.getInfo();
    }
    this.getBuildings();
  },
  methods: {
    getInfo() {
      this.form = Object.assign(
        {},
        JSON.parse(localStorage.getItem("housing"))
      );
    },
    getBuildings() {
      list({ current: 1, size: 99999 }).then((res) => {
        this.buildingOptions = res.d.records;
      });
    },
    handleCreate() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          add(this.form).then((res) => {
            if (res.c === 200) {
              this.$message.success("创建成功");
              this.$router.back();
            }
          });
        }
      });
    },
    handleUpdate() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          update(this.form).then((res) => {
            this.$message.success("修改成功");
            this.$router.back();
          });
        }
      });
    },
    create(formData) {
      let fn = add;
      if (this.type === "update") fn = update;
      fn(formData).then((res) => {
        console.log(res);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        this.$router.back();
      });
    },
    formChange(e) {
      const { secondaryType } = e;
      this.showInfo = !!secondaryType;
    },
  },
};
</script>

<style lang="scss" scoped>
// .info-title {
//   margin-top: 40px;
//   margin-left: 40px;
// }
// .create-btn {
//   margin-top: 20px;
//   margin-bottom: 120px;
//   margin-left: 240px;
// }
.form-container {
  width: 50%;
  .el-input {
    width: 202px;
  }
}
</style>
