<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="tenantId">
              <a-input v-model="model.tenantId" placeholder="请输入关联租户"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联酒店" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hotelId">
              <a-input v-model="model.hotelId" placeholder="请输入关联酒店"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item
              label="名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="name"
            >
              <a-input v-model="model.name" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="startTime"
            >
              <j-date
                placeholder="请选择开始时间"
                v-model="model.startTime"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="结束时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="endTime"
            >
              <j-date
                placeholder="请选择结束时间"
                v-model="model.endTime"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="适用房型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="roomIds"
            >
              <a-checkbox-group
                :options="roomLayouts"
                v-model="model.roomLayouts"
              >
                <!-- <template v-for="i of roomLayouts">
                  <a-checkbox
                    class="list-check-item"
                    :key="`key-${i}`"
                    :value="i.id"
                    >{{ i.name }}</a-checkbox
                  >
                </template> -->
              </a-checkbox-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="每人可领取张数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="klqzs"
            >
              <a-input-number
                v-model="model.klqzs"
                placeholder="请输入可领取张数"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="说明"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="introduce"
            >
              <a-input
                v-model="model.introduce"
                placeholder="请输入说明"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="使用说明"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="ruleDec"
            >
              <a-input
                v-model="model.ruleDec"
                placeholder="请输入使用说明"
              ></a-input>
            </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: "BusMarketCouponsForm",
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      roomLayouts: [],
      model: { },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: "请输入名称!" }],
        startTime: [{ required: true, message: "请输入开始时间!" }],
        endTime: [{ required: true, message: "请输入结束时间!" }],
        klqzs: [{ required: true, message: "请输入每人可领取张数!" }],
        introduce: [{ required: true, message: "请输入说明!" }],
      },
      url: {
        add: "/business/busMarketCoupons/add",
        edit: "/business/busMarketCoupons/edit",
        queryById: "/business/busMarketCoupons/queryById",
      },
    };
  },
  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));
  },
  methods: {
    // onChange(e) {
    //   console.log("e", e);
    //   var ids = [];
    //   if (e && e.length > 0) {
    //     // e.forEach((item) => {
    //     //   ids.push(item.id);
    //     // });
    //     this.model.roomIds = ids.join(",");
    //   } else {
    //     this.model.roomIds = "";
    //   }
    //   this.model.roomLayouts = e;
    // },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      console.log("record", record);
      var roomLayouts = (record.roomIds || "").split(",");
      this.model = Object.assign({}, record, { roomLayouts: roomLayouts });
      this.visible = true;
      getAction("/rooms/cesRoomLayout/list", { pageNo: 1, pageSize: 100 })
        .then((res) => {
          if (res.success) {
            this.roomLayouts = res.result.records;
            var data = [];
            res.result.records.forEach((item) => {
              data.push({ label: item.name, value: item.id });
            });
            this.roomLayouts = data;
          }
        })
        .finally(() => {});
    },
    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";
          }
          this.model.roomIds = this.model.roomLayouts.join(",");
          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>