<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model
        ref="form"
        :model="model"
        :rules="validatorRules"
        slot="detail"
      >
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="物品类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="goodsType"
            >
              <a-select placeholder="请选择" v-model="model.goodsType">
                <a-select-option :value="1"> 贵重物品 </a-select-option>
                <a-select-option :value="2"> 遗失物品 </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="物品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="goodsName"
            >
              <a-input
                v-model="model.goodsName"
                placeholder="请输入物品名称"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="参考价值"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="referenceValue"
            >
              <a-input
                v-model="model.referenceValue"
                placeholder="请输入参考价值"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="客人类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="customerType"
            >
              <a-select placeholder="请选择" v-model="model.customerType">
                <a-select-option :value="1"> 在住客人 </a-select-option>
                <a-select-option :value="2"> 离店客人 </a-select-option>
                <a-select-option :value="3"> 其他 </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col
            :span="24"
            v-if="model.customerType == 1 || model.customerType == 2"
          >
            <a-form-model-item
              label="房号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="roomId"
            >
              <a-select
                v-model="model.roomId"
                placeholder="请选择房号"
                @change="roomChange"
              >
                <a-select-option
                  :value="item.id"
                  v-for="(item, index) in roomsList"
                  :key="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
              <a-input
                v-show="false"
                v-model="model.roomNo"
                placeholder="请输入房号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-else>
            <a-form-model-item
              label="房号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="roomNo"
            >
              <a-input
                v-model="model.roomNo"
                placeholder="请输入房号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="客人姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="customerName"
            >
              <a-input
                v-model="model.customerName"
                placeholder="请输入客人姓名"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="电话"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="mobile"
            >
              <a-input
                v-model="model.mobile"
                placeholder="请输入电话"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="登记单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="orderNo"
            >
              <a-input
                v-model="model.orderNo"
                placeholder="请输入登记单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="remark"
            >
              <a-textarea
                v-model="model.remark"
                rows="4"
                placeholder="请输入备注"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";
export default {
  name: "BusMemberGoodsManageForm",
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        tenantId: [{ required: true, message: "请输入关联租户!" }],
        hotelId: [{ required: true, message: "请输入关联酒店!" }],
        goodsType: [{ required: true, message: "请输入物品类型!" }],
        goodsName: [{ required: true, message: "请输入物品名称!" }],
        referenceValue: [{ required: true, message: "请输入参考价值!" }],
        customerType: [{ required: true, message: "请输入客人类型!" }],
        roomNo: [{ required: true, message: "请输入房号!" }],
        roomId: [{ required: true, message: "请输入房号!" }],
        customerName: [{ required: true, message: "请输入客人姓名!" }],
        createDate: [{ required: true, message: "请输入创建时间!" }],
      },
      url: {
        add: "/business/busMemberGoodsManage/add",
        edit: "/business/busMemberGoodsManage/edit",
        queryById: "/business/busMemberGoodsManage/queryById",
      },
      roomsList: [],
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
  },
  created() {
    var _info = JSON.parse(localStorage.getItem("storeInfo"));
    if (_info) {
      this.model.hotelId = _info.id;
    }
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    getAction("/rooms/cesRooms/list", {
      pageNo: 1,
      pageSize: 100,
    }).then((res) => {
      if (res.success) {
        this.roomsList = res.result.records;
      }
    });
  },
  methods: {
    roomChange(e) {
      console.log(e);
      this.model.roomNo = this.roomsList.find((t) => t.id == e).name;
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    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";
          }
          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>