<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="billRefund"
            >
              <a-input
                style="width: 100px"
                disabled
                v-model="model.billRefund"
                placeholder="请输入结账应退"
              ></a-input
              >元
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="押金应退"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="deposit"
            >
              <a-input
                style="width: 100px"
                disabled
                v-model="model.deposit"
                placeholder="请输入押金应退"
              ></a-input
              >元
              <a-select
                v-model="model.payType"
                style="width: 200px"
                placeholder="支付类型"
              >
                <a-select-option
                  :value="item.id"
                  v-for="item in payTypeList"
                  :key="item.id"
                  :disabled="item.delFlag == 99"
                  >{{ 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="roomFee"
            >
              <a-input
                style="width: 100px"
                disabled
                v-model="model.roomFee"
                placeholder="请输入房费应退"
              ></a-input
              >元
              <a-select
                v-model="model.payType"
                style="width: 200px"
                placeholder="支付类型"
              >
                <a-select-option
                  :value="item.id"
                  v-for="item in payTypeList"
                  :key="item.id"
                  :disabled="item.delFlag == 99"
                  >{{ 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="money"
            >
              <a-input
                style="width: 100px"
                disabled
                v-model="model.money"
                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: "Refund",
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        payType: "",
        payType2: "",
        livingOrderId: "",
        certType: 1,
        gender: 1,
        bookingOrderId: "",
      },
      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: "请输入手机号!",
          },
        ],
        cardNo: [{ required: true, message: "请输入会员卡号!" }],
        gradeId: [{ required: true, message: "请输入等级类型!" }],
        payType: [{ required: true, message: "请输入付款类型!" }],
        paymentMethod: [{ required: true, message: "请输入付款方式!" }],
        customerName: [{ 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: [],
      payTypeList: [],
    };
  },
  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));
    this.getbusCustomer();
  },
  methods: {
    getbusRoomPayType() {
      getAction("/business/busRoomPayType/list", {
        pageSize: 99999,
        pageNo: 1,
      }).then((res) => {
        if (res.success) {
          this.payTypeList = res.result.records;
          if (this.payTypeList && this.payTypeList.length > 0) {
            if (
              !this.model.vipCustomerId ||
              this.model.vipCustomerId.length == 0
            ) {
              var index = this.payTypeList.findIndex((t) =>
                t.name.includes("会员")
              );
              if (index >= 0) {
                this.payTypeList[index].delFlag = 99;
              }
            }
            if (
              !this.model.contractTeamId ||
              this.model.contractTeamId.length == 0 ||
              !this.model.contractTeamProtocolId ||
              this.model.contractTeamProtocolId.length == 0
            ) {
              var index = this.payTypeList.findIndex((t) =>
                t.name.includes("单位")
              );
              if (index >= 0) {
                this.payTypeList[index].delFlag = 99;
              }
            }
            this.$set(this.model, "payType", this.payTypeList[0].id);
          }
        }
      });
    },
    handleSearch(value) {
      let result;
      if (!value) {
        result = this.oldcustomerList;
      } else {
        result = this.oldcustomerList.filter((t) => t.name.includes(value));
      }
      this.customerList = result;
    },
    handleSelectMember(e) {
      var find = this.customerList.find((t) => t.id === e);
      this.model.phone = find.phone;
      this.model.customerName = find.name;
      this.model.customerId = find.id;
    },
    getbusCustomer() {
      getAction("/bus/busCustomer/list", {}).then((res) => {
        if (res.success) {
          this.customerList = res.result.records;
          this.oldcustomerList = JSON.parse(JSON.stringify(this.customerList));
        }
      });
    },
    add(livingOrderId, roomId) {
      this.modelDefault.livingOrderId = livingOrderId;
      this.modelDefault.roomId = roomId;
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);

      this.getbusRoomPayType();
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          var fees = [];
          fees.push({
            feeType: this.model.feeType,
            money: this.model.money,
            payType: this.model.payType,
            custorerOrderRemark: "结账退款",
            isPreferential: false,
            livingOrderId: this.model.livingOrderId,
          });
          var url =
            "/business/busRoomBookingOrders/settle-checkout?bookingOrderId=" +
            this.model.bookingOrderId;
          if (this.model.livingOrderId) {
            url =
              "/business/busRoomBookingOrders/living-settle-checkout?livingOrderId=" +
              this.model.livingOrderId;
          }
          httpAction(url, fees, "post")
            .then((res) => {
              if (res.success) {
                that.$message.success("结账成功");
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>