<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model
        ref="form"
        :model="model"
        :rules="validatorRules"
        slot="detail"
      >
        <a-form-model-item
          label="房间楼层"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="floorId"
        >
          <a-select
            placeholder="请选择"
            style="width: 200px"
            v-model="model.floorId"
            @change="onFloorChange($event)"
          >
            <template v-for="item in buildingTreeData">
              <a-select-opt-group v-if="item.children" :key="item.id">
                <span slot="label"><a-icon type="tags" />{{ item.name }}</span>
                <a-select-option
                  :value="child.id"
                  v-for="child in item.children"
                  :key="child.id"
                >
                  {{ child.name }}
                </a-select-option>
              </a-select-opt-group>
            </template>
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          label="房间房型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="layoutId"
        >
          <a-select
            placeholder="请选择"
            style="width: 200px"
            v-model="model.layoutId"
          >
            <a-select-option
              :value="item.id"
              v-for="item in layouts"
              :key="item.id"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          label="房间前缀"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="prefix"
        >
          <a-input
            style="width: 50%"
            v-model="model.prefix"
            placeholder="请填写房间前缀"
          />
        </a-form-model-item>

        <a-form-model-item
          label="房间房号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="name"
        >
          <a-input
            style="width: 50%"
            v-model="model.name"
            placeholder="请填写房号"
          />
        </a-form-model-item>

        <a-form-model-item
          label="房间描述"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="remark"
        >
          <a-textarea
            style="width: 50%"
            v-model="model.remark"
            placeholder="请填写房间描述"
          />
        </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
<script>
import { getRoomPlans, getSelectList } from "@/api/api";
import { buildingTree } from "@/api/roomBuildingApi";
import { getAllLayouts } from "../../../../../api/roomLayout";
import { httpAction, getAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";

export default {
  name: "BusMarketMemberForm",
  props: {
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        id: "",
        floorId: undefined,
        buildId: null,
        layoutId: undefined,
        prefix: null,
        name: null,
        remark: null,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: "请输入房型!" }],
        floorId: [{ required: true, message: "请选择楼层!" }],
        layoutId: [{ required: true, message: "请选择房型!" }],
      },
      url: {
        add: "/rooms/cesRooms/save",
        edit: "/rooms/cesRooms/modify",
        queryById: "/rooms/cesRooms/queryById",
      },
      iconChooseVisible: false,
      roomPlans: [],
      members: [],
      buildingTreeData: [],
      layouts: [],
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
  },
  created() {
    buildingTree().then((res) => {
      if (res.code == 200) {
        this.buildingTreeData = res.result;
      }
    });
    getAllLayouts().then((res) => {
      if (res.code == 200) {
        this.layouts = res.result.records;
      }
    });
    var _info = JSON.parse(localStorage.getItem("storeInfo"));
    if (_info) {
      this.model.hotelId = _info.id;
      this.initData();
    }
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    // 下拉框改变之后找到楼栋id
    onFloorChange(e) {
      this.buildingTreeData.forEach((s) => {
        if (s.children) {
          s.children.forEach((t) => {
            if (t.id == e) this.model.buildId = s.id;
          });
        }
      });
    },
    initData() {
      getRoomPlans(this.model.hotelId, null).then((res) => {
        if (res.success) {
          this.roomPlans = res.result;
        }
      });
    },
    selectIcons() {
      this.iconChooseVisible = true;
    },
    handleIconCancel() {
      this.iconChooseVisible = false;
    },
    handleIconChoose(value) {
      console.log(value);
      this.model.icon = value;
      this.iconChooseVisible = false;
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      getSelectList({ id: this.model.id }).then((res) => {
        if (res.success) {
          this.members = res.result;
        }
      });
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
          } else {
            httpurl += this.url.edit;
            method = "put";
          }
          if (this.model.payFlag == 0) {
            this.model.payAmount = 0;
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
</style>