<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="eventId">
              <a-input v-model="model.eventId" placeholder="请输入关联事件"  ></a-input>
            </a-form-model-item>
          </a-col> -->

          <a-col :span="24">
            <a-form-model-item
              label="类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="type"
            >
              <a-radio-group v-model="model.type" @change="typeChange">
                <a-radio :value="1">优惠券</a-radio>
                <a-radio :value="3">免房券</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="会员等级"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="levelId"
            >
              <a-select
                v-model="model.levelId"
                placeholder="请选择"
                style="width: 120px"
                :allowClear="true"
              >
                <a-select-option
                  :value="item.value"
                  v-for="(item, index) in members"
                  :key="index"
                  >{{ item.label }}</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="couponsCashId"
            >
              <a-select
                v-model="model.couponsCashId"
                placeholder="请选择"
                style="width: 120px"
                :allowClear="true"
              >
                <a-select-option
                  :value="item.id"
                  v-for="(item, index) in coupons"
                  :key="index"
                  >{{ item.name }}</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="count"
            >
              <a-input-number
                v-model="model.count"
                :min="1"
                :max="1000000"
                :precision="0"
                style="width: 20%"
              />
            </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";
import {
  getSelectList,
  getBusMarketCouponsList,
  getBusMarketCouponsCashList,
} from "@/api/api";
export default {
  name: "BusMarketCouponsEventDetailForm",
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: { type: 1, eventId: "", couponsCashId: undefined },
      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: "请输入关联酒店!" }],
        // eventId: [{ required: true, message: "请输入关联事件!" }],
        type: [{ required: true, message: "请选择类型!" }],
        levelId: [{ required: true, message: "请选择会员等级!" }],
        couponsCashId: [{ required: true, message: "请选择券!" }],
        count: [{ required: true, message: "请输入数量!" }],
      },
      url: {
        add: "/business/busMarketCouponsEventDetail/add",
        edit: "/business/busMarketCouponsEventDetail/edit",
        queryById: "/business/busMarketCouponsEventDetail/queryById",
      },
      members: [],
      coupons: [],
    };
  },
  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));
    getSelectList({ id: "" }).then((res) => {
      if (res.success) {
        this.members = res.result;
      }
    });
  },
  methods: {
    typeChange(e) {
      this.coupons = [];
      this.model.couponsCashId = undefined;
      if (e.target.value == 3) {
        getBusMarketCouponsList({ pageNo: 1, pageSize: 100 }).then((res) => {
          if (res.success) {
            this.coupons = res.result.records;
          }
        });
      } else {
        getBusMarketCouponsCashList({ pageNo: 1, pageSize: 100 }).then(
          (res) => {
            if (res.success) {
              this.coupons = res.result.records;
            }
          }
        );
      }
    },
    add(eventId) {
      console.log("eventId2", eventId);
      this.modelDefault.eventId = eventId;
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      if (this.model.type == 3) {
        getBusMarketCouponsList({ pageNo: 1, pageSize: 100 }).then((res) => {
          if (res.success) {
            this.coupons = res.result.records;
          }
        });
      } else {
        getBusMarketCouponsCashList({ pageNo: 1, pageSize: 100 }).then(
          (res) => {
            if (res.success) {
              this.coupons = res.result.records;
            }
          }
        );
      }
    },
    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>