<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="livingOrderId"
            >
              <a-select v-model="model.livingOrderId" placeholder="请选择房号">
                <a-select-option
                  :value="item.livingOrderId"
                  v-for="(item, index) in roomsList"
                  :key="item.id"
                >
                  {{ item.roomName }}/{{ item.customerName }}
                </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="userName"
            >
              <a-input
                v-model="model.userName"
                placeholder="请输入姓名"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="物品"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="goodsId"
            >
              <a-select v-model="model.goodsId" placeholder="请选择物品">
                <a-select-option
                  :value="item.id"
                  v-for="(item, index) in busDictItemList"
                  :key="item.id"
                >
                  {{ item.itemText }}
                </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="num"
            >
              <a-input-number
                v-model="model.num"
                placeholder="请输入数量"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="押金"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="deposit"
            >
              <a-input-number
                v-model="model.deposit"
                placeholder="请输入押金"
                style="width: 100%"
              />
            </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: "CesOrderLeaseGoodsForm",
  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: {
        livingOrderId: [{ required: true, message: "请选择房间!" }],
        goodsId: [{ required: true, message: "请输入物品平台字典项目ID!" }],
        num: [{ required: true, message: "请输入数量!" }],
        deposit: [{ required: true, message: "请输入押金!" }],
        createDate: [{ required: true, message: "请输入创建时间!" }],
        orderNo: [{ required: true, message: "请输入单据号!" }],
      },
      url: {
        add: "/order/cesOrderLeaseGoods/add",
        edit: "/order/cesOrderLeaseGoods/edit",
        queryById: "/order/cesOrderLeaseGoods/queryById",
      },
      busDictItemList: [],
      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("/business/busDictItem/list", {
      dictId: "1659108818476990465",
    }).then((res) => {
      if (res.success) {
        this.busDictItemList = res.result.records;
      }
    });

    getAction("/business/busRoomBookingOrders/living-orders?status=-1", {
      pageNo: 1,
      pageSize: 99999,
    }).then((res) => {
      if (res.success) {
        this.roomsList = res.result.records;
      }
    });
  },
  methods: {
    add(livingOrderId) {
      this.modelDefault.livingOrderId = livingOrderId;
      if (this.modelDefault.livingOrderId == null) {
        this.modelDefault.livingOrderId = undefined;
      }
      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) {
          var find = this.roomsList.find(
            (t) => t.livingOrderId === this.model.livingOrderId
          );
          this.model.roomId = find.roomId;
          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>