<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"
            >
              {{ sumPrice
              }}<a-icon
                @click="editBatchRoomPrice()"
                type="edit"
                style="color: rgba(255, 141, 26, 1); font-size: 18px"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="入住类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="liveType"
            >
              <a-select
                placeholder="入住类型"
                v-model="model.liveType"
                @change="bookingTypeChange"
              >
                <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" v-if="model.liveType == 2">
            <a-form-model-item
              label="时长"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="fangAnId"
            >
              <a-select
                placeholder="时长"
                v-model="model.fangAnId"
                @change="hourRoomIdChange"
              >
                <a-select-option
                  :value="item.id"
                  v-for="(item, index) in hourRoomRuleList"
                  :key="item.id"
                >
                  {{ item.hourRoomName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <edit-batch-room-price-modal
      ref="modalEditBatchRoomPriceModal"
      @ok="modalFormOk"
    ></edit-batch-room-price-modal>
  </a-spin>
</template>

<script>
import moment from "moment";
import { httpAction, getAction, postAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";
import EditBatchRoomPriceModal from "./EditBatchRoomPriceModal.vue";
export default {
  name: "BusMemberCardForm",
  components: { EditBatchRoomPriceModal },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: { payType: 1, livingOrderId: "", certType: 1, gender: 1 },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        mobile: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入手机号!",
          },
        ],
        liveType: [{ required: true, message: "请输入入住类型!" }],
        gradeId: [{ required: true, message: "请输入等级类型!" }],
        payType: [{ required: true, message: "请输入付款类型!" }],
        paymentMethod: [{ required: true, message: "请输入付款方式!" }],
        cusName: [{ required: true, message: "请输入会员姓名!" }],
        sex: [{ required: true, message: "请输入性别!" }],
        certificateType: [{ required: true, message: "请输入证件类型!" }],
        validity: [{ required: true, message: "请输入有效期!" }],
      },
      url: {
        add: "/business/busRoomBookingOrders/booking-to-live",
        edit: "/business/busMemberCard/edit",
        queryById: "/business/busMemberCard/queryById",
      },
      gradeList: [],
      paymentMethodList: [],
      staffList: [],
      customerList: [],
      oldcustomerList: [],
      hourRoomRuleList: [],
      currentDate: moment(new Date()).format("YYYY-MM-DD"),
      roomPrice: 0,
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
    sumPrice() {
      var sum = 0;
      // if (this.model.liveType == 1) {
      (this.model.livingDayPrices || []).forEach((item) => {
        sum += item.price;
      });
      // } else if (this.model.liveType == 2) {
      //   var find = this.hourRoomRuleList.find(
      //     (t) => t.id == this.model.fangAnId
      //   );
      //   if (find) {
      //     sum = find.afterOpenRoomPay;
      //   }
      // }
      return sum;
    },
  },
  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/cesHourRoomRule/list", {
      pageNo: 1,
      pageSize: 100,
    }).then((res) => {
      if (res.success) {
        this.hourRoomRuleList = res.result.records;
      }
    });
  },
  methods: {
    moment,
    modalFormOk(e) {
      console.log("e", e);
    },
    editBatchRoomPrice() {
      this.$refs.modalEditBatchRoomPriceModal.edit(this.model);
      this.$refs.modalEditBatchRoomPriceModal.title = "修改";
      this.$refs.modalEditBatchRoomPriceModal.disableSubmit = false;
    },
    bookingTypeChange(e) {
      if (this.model.liveType == 1) {
        this.model.fangAnId = "";
        if (this.model.oldliveType == 1) {
          this.model.livingDayPrices = this.model.oldlivingDayPrices;
        } else if (this.model.oldliveType == 2) {
          //钟点转全天，每日房价要重新赋值
          this.model.livingDayPrices = this.model.oldlivingDayPrices;
          var item = JSON.parse(
            JSON.stringify(this.model.oldlivingDayPrices[0])
          );
          item.dayTime = this.currentDate;
          item.price = this.roomPrice;
          this.model.livingDayPrices = [item];
        }
      } else {
        var hourRoomRule = this.hourRoomRuleList[0];
        if (hourRoomRule) {
          this.model.fangAnId = hourRoomRule.id;
          this.bindLivingDayPrices();
        }
      }
    },
    bindLivingDayPrices() {
      var find = this.hourRoomRuleList.find((t) => t.id == this.model.fangAnId);
      var item = JSON.parse(JSON.stringify(this.model.oldlivingDayPrices[0]));
      item.dayTime = this.currentDate;
      item.price = 0;
      if (find) {
        item.price = find.afterOpenRoomPay;
      }
      this.model.livingDayPrices = [item];
    },
    hourRoomIdChange() {
      this.bindLivingDayPrices();
    },
    add(livingOrderId, roomId) {
      this.modelDefault.livingOrderId = livingOrderId;
      this.modelDefault.roomId = roomId;
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.model.oldlivingDayPrices = JSON.parse(
        JSON.stringify(this.model.livingDayPrices)
      );
      this.model.oldliveType = JSON.parse(JSON.stringify(this.model.liveType));
      this.visible = true;

      getAction(
        "/rooms/cesRooms/get-room-mkt-price?roomId=" + this.model.roomId,
        {}
      ).then((res) => {
        if (res.success) {
          this.roomPrice = res.result;
        }
      });
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        console.log(this.model);
        // return;
        if (valid) {
          that.model.prices = {
            livingOrderId: that.model.orderId,
            dayPrices: that.model.livingDayPrices,
          };
          that.confirmLoading = true;
          postAction(
            "/business/busRoomBookingOrders/update-orders?type=2",
            this.model
          )
            .then((res) => {
              if (res.success) {
                that.$message.success("修改成功");
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>