<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="12">
            <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="12">
            <a-form-model-item
              label="选择类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="type"
            >
              <a-radio-group v-model="model.type">
                <a-radio :value="0">会员折扣</a-radio>
                <a-radio :value="1">房价方案</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.type == 0">
            <a-form-model-item
              label="会员折扣"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="discount"
            >
              <a-input-number
                v-model="model.discount"
                :min="1"
                :max="100"
                style="width: 20%"
                :precision="0"
              />%
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.type == 1">
            <a-form-model-item
              label="房价方案"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="schemeId"
            >
              <a-select
                v-model="model.schemeId"
                placeholder="请选择"
                style="width: 120px"
                :allowClear="true"
              >
                <a-select-option
                  :value="item.value"
                  v-for="(item, index) in roomPlans"
                  :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="payFlag"
            >
              <a-radio-group v-model="model.payFlag">
                <a-radio :value="0">否</a-radio>
                <a-radio :value="1">是</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.payFlag == 1">
            <a-form-model-item
              label="支付费用"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="payAmount"
              :rules="[
                { required: true, message: '请输入支付费用', trigger: 'blur' },
              ]"
            >
              <a-input-number
                v-model="model.payAmount"
                placeholder="请输入支付费用"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item
              label="积分购买"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="payIntegral"
            >
              <a-input-number
                v-model="model.payIntegral"
                placeholder="购买会员卡所需积分"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item
              label="是否可以储值"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="payStored"
            >
              <a-radio-group v-model="model.payStored">
                <a-radio :value="0">否</a-radio>
                <a-radio :value="1">是</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item
              label="等级"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="level"
            >
              <a-input-number
                v-model="model.level"
                placeholder="请输入等级"
                :precision="0"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="会员优惠"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="hyyh"
              >每一积分可抵扣金额
              <a-input-number
                v-model="model.hyyh"
                style="width: 20%"
                :min="0"
                :max="1000000"
              />元
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="自动续期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="renewal"
            >
              <a-checkbox
                @change="(e) => (model.renewal = e.target.checked)"
                :checked="model.renewal"
              >
                充值自动续期
              </a-checkbox>
              <a-input-number
                v-model="model.renewalDay"
                style="width: 20%"
                :min="1"
                :max="1000000"
                :precision="0"
              />天
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="积分设置"
              :labelCol="{
                xs: { span: 24 },
                sm: { span: 3},
              }"
              :wrapperCol="wrapperCol"
              prop="tenantPointsChecked"
            >
              <a-checkbox
                @change="(e) => (model.tenantPointsChecked = e.target.checked)"
                :checked="model.tenantPointsChecked"
              >
                客房积分模式
              </a-checkbox>
              <block v-show="model.tenantPointsChecked">
                <a-radio-group v-model="model.tenantPoints">
                  <a-radio :value="1">所有的消费转成积分</a-radio>
                  <a-radio :value="2">只房费转成积分</a-radio> </a-radio-group
                ><br />
                每消费
                <a-input-number
                  v-model="model.tenantRoomIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />元，积
                <a-input-number
                  v-model="model.addTenantRoomIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />分</block
              ><br />
              <a-checkbox
                @change="(e) => (model.roomIntegralChecked = e.target.checked)"
                :checked="model.roomIntegralChecked"
              >
                按储值卡支付积分
              </a-checkbox>
              <block v-show="model.roomIntegralChecked">
                每消费
                <a-input-number
                  v-model="model.roomIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />元，积
                <a-input-number
                  v-model="model.addRoomIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />分</block
              ><br />
              <a-checkbox
                @change="(e) => (model.dayIntegralChecked = e.target.checked)"
                :checked="model.dayIntegralChecked"
              >
                按入住天数积分
              </a-checkbox>
              <block v-show="model.dayIntegralChecked">
                每入住
                <a-input-number
                  v-model="model.dayIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />天，积
                <a-input-number
                  v-model="model.addDayIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />分</block
              ><br />
              <a-checkbox
                @change="(e) => (model.rechargeChecked = e.target.checked)"
                :checked="model.rechargeChecked"
              >
                启用储值赠送
              </a-checkbox>
              <block v-show="model.rechargeChecked">
                每充值
                <a-input-number
                  v-model="model.rechargeIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />元，积
                <a-input-number
                  v-model="model.addRechargeIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />分</block
              ><br />
              <a-checkbox
                @change="(e) => (model.upLevelChecked = e.target.checked)"
                :checked="model.upLevelChecked"
              >
                积分满足
              </a-checkbox>
              <block v-show="model.upLevelChecked">
                <a-input-number
                  v-model="model.upLevelIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
                />分，自动升级为
                <a-select
                  v-model="model.upLevel"
                  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></block
              ><br />
              <a-checkbox
                @change="(e) => (model.subLevelChecked = e.target.checked)"
                :checked="model.subLevelChecked"
              >
                升级扣除相应积分
              </a-checkbox>
              <block v-show="model.subLevelChecked">
                ，扣减相应积分为
                <a-input-number
                  v-model="model.subLevelIntegral"
                  style="width: 20%"
                  :min="1"
                  :max="1000000"
                  :precision="0"
              /></block>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="会员图标"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="icon"
            >
              <!-- <a-input
                v-model="model.icon"
                placeholder="请输入会员图标"
              ></a-input> -->
              <a-input
                placeholder="点击选择图标"
                v-model="model.icon"
                :readOnly="true"
              >
                <a-icon slot="addonAfter" type="setting" @click="selectIcons" />
              </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="卡片背景"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="bgImage"
            >
              <j-image-upload
                class="avatar-uploader"
                text="上传"
                v-model="model.bgImage"
              ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="卡片描述"
              :labelCol="{
                xs: { span: 24 },
                sm: { span: 3},
              }"
              :wrapperCol="wrapperCol"
              prop="content"
            >
              <a-textarea
                v-model="model.content"
                rows="4"
                placeholder="请输入卡片描述"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>

      <icons
        @choose="handleIconChoose"
        @close="handleIconCancel"
        :iconChooseVisible="iconChooseVisible"
      ></icons>
    </j-form-container>
  </a-spin>
</template>

<script>
import { getRoomPlans, getSelectList } from "@/api/api";
import { httpAction, getAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";
import Icons from "./icon/Icons";

export default {
  name: "BusMarketMemberForm",
  components: { Icons },
  props: {
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        id: "",
        hotelId: 0,
        type: 0,
        discount: 1,
        payFlag: 0,
        payIntegral: 0,
        payStored: 0,
        level: 1,
        renewal: false,
        tenantPointsChecked: false,
        roomIntegralChecked: false,
        dayIntegralChecked: false,
        rechargeChecked: false,
        upLevelChecked: false,
        subLevelChecked: false,
        tenant_points: 1,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: "请输入会员名称!" }],
        // payAmount: [{ required: true, message: "请输入支付费用!" }],
        level: [{ required: true, message: "请输入等级!" }],
        icon: [{ required: true, message: "请上传会员图标!" }],
      },
      url: {
        add: "/org.jeecg.modules.business/busMarketMember/add",
        edit: "/org.jeecg.modules.business/busMarketMember/edit",
        queryById: "/org.jeecg.modules.business/busMarketMember/queryById",
      },
      iconChooseVisible: false,
      roomPlans: [],
      members: [],
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
  },
  created() {
    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: {
    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>