<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="orderNo"
            >
              <a-input
                disabled
                v-model="model.orderNo"
                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
                disabled
                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="theme"
            >
              <a-input
                disabled
                v-model="model.theme"
                placeholder="请输入会议主题"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="挂房账"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="payType"
            >
              <a-radio-group v-model="model.payType">
                <a-radio :value="1">否</a-radio>
                <a-radio :value="2">是</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="amount"
            >
              <a-input-number
                v-model="model.amount"
                placeholder="请输入金额"
                style="width: 100%"
                :min="0"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.payType == 2">
            <a-form-model-item
              label="入账人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="settleRoomOrderNo"
            >
              <!-- <a-input
                v-model="model.settleRoomOrderNo"
                placeholder="请输入入账人"
                style="width: 100%"
              /> -->
              <a @click="handleRoomOrder">{{selectRoomOrder? `房间:${selectRoomOrder.roomName},姓名:${selectRoomOrder.customerName}` : `请选择`}}</a>
            </a-form-model-item>
          </a-col>

          <template v-if="model.payType==1">
            <a-col :span="24">
            <a-divider orientation="left"> 支付方式 </a-divider>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="结账优惠"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="preferentialAmount"
            >
              <a-input-number
                v-model="model.preferentialAmount"
                placeholder="请输入结账优惠"
                style="width: 100%"
                :min="0"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="结账应收"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              {{ receiptsAmount }}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="输入合计"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              {{ sumAmount }}
            </a-form-model-item>
          </a-col>
          <a-col
            :span="24"
            v-for="(item, index) in paymentMethodList"
            :key="index"
          >
            <a-form-model-item
              :label="item.name"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              :prop="`paymentMethodList[${index}].amount`"
            >
              <a-input-number
                v-model="item.amount"
                style="width: 100%"
                :min="0"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          </template>

          
        </a-row>
      </a-form-model>
    </j-form-container>
    <select-check-in-room-order-modal
      ref="modalSelectCheckInRoomOrderModal"
      @ok="modalSelectCheckInRoomOrderFormOk"
    ></select-check-in-room-order-modal>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { validateDuplicateValue } from "@/utils/util";
import SelectCheckInRoomOrderModal from '../../../pos/modules/SelectCheckInRoomOrderModal.vue'



export default {
  name: "BusMeetingRoomForm",
  components: {
    SelectCheckInRoomOrderModal
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: { detailList: [{}] },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 7 },
        sm: { span: 10 },
      },
      wrapperCol2: {
        xs: { span: 7 },
        sm: { span: 11 },
      },
      confirmLoading: false,
      validatorRules: {
        payType: [{ required: true, message: "请输入挂房账!" }],
        amount: [{ required: true, message: "请输入金额!" }],
        // settleRoomOrderNo: [{ required: true, message: "请输入入账人!" }],
      },
      url: {
        add: "/business/busMeetingRoomSchedule/create",
        edit: "/business/busMeetingRoomSchedule/edit",
        queryById: "/business/busMeetingRoomSchedule/queryById",
        settle: "/business/busMeetingRoomSchedule/settle",
      },
      meetingRoomList: [],
      timeSpanList: [
        { id: 1, name: "上午" },
        { id: 2, name: "下午" },
        { id: 3, name: "晚上" },
      ],
      paymentMethodList: [],
      //入账人
      selectRoomOrder:null
    };
  },
  computed: {
    formDisabled() {
      return this.disabled;
    },
    receiptsAmount() {
      var sum = this.model.amount - this.model.preferentialAmount;

      return sum;
    },
    sumAmount() {
      if (this.paymentMethodList && this.paymentMethodList.length > 0) {
        return this.paymentMethodList.reduce(function (total, v) {
          return total + v.amount;
        }, 0);
      } else {
        return 0;
      }
    },
  },
  created() {
    getAction("/business/busMeetingRoom/list", {
      pageNo: 1,
      pageSize: 100,
      stauts: 1,
    }).then((res) => {
      if (res.success) {
        this.meetingRoomList = res.result.records;
      }
    });
    getAction("/business/busRoomPayType/queryList", {
      pageNo: 1,
      pageSize: 100,
    }).then((res) => {
      if (res.success) {
        if (res.result && res.result.length > 0) {
          res.result.forEach((item) => {
            Object.assign(item, { amount: 0 });
          });
          this.paymentMethodList = res.result;
          console.log("  this.paymentMethodList", this.paymentMethodList);
        }
      }
    });
    var _info = JSON.parse(localStorage.getItem("storeInfo"));
    if (_info) {
      this.model.hotelId = _info.id;
    }
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    handleRoomOrder() {
      // this.$message.warning("房间订单功能暂未实现,请先选择挂房账否操作");
      this.$refs.modalSelectCheckInRoomOrderModal.add();
      this.$refs.modalSelectCheckInRoomOrderModal.title = "选择转帐人";
      this.$refs.modalSelectCheckInRoomOrderModal.disableSubmit = false;
    },
    modalSelectCheckInRoomOrderFormOk(e) {
      console.log(e);
      this.selectRoomOrder = e
    },
    puls() {
      this.model.detailList.push({});
    },
    remove(index) {
      this.model.detailList.splice(index, 1);
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record, { preferentialAmount: 0 });
      delete this.model.createTime;
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.receiptsAmount > this.model.amount) {
            this.$message.error("输入优惠金额不能大于订单金额");
            return;
          }
          if (this.receiptsAmount != this.sumAmount && this.model.payType == 1) {
            this.$message.error("输入必须等于结账应收金额");
            return;
          }
          if (this.model.payType == 2 && !this.selectRoomOrder) {
            this.$message.error("请选择入账人");
            return
          }
          this.model.settleRoomOrderNo = this.selectRoomOrder.livingOrderId;
          this.model.paymentMethodList = this.paymentMethodList;
          that.confirmLoading = true;
          let httpurl = this.url.settle;
          let method = "post";
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok", this.model);
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>
<style scoped>
.dynamic-delete-button {
  cursor: pointer;
  position: relative;
  top: 10px;
  margin-left: 5px;
  font-size: 18px;
  color: #1890ff;
  transition: all 0.3s;
}
.dynamic-delete-button:hover {
  color: #777;
}
.dynamic-delete-button[disabled] {
  cursor: not-allowed;
  opacity: 0.5;
}
</style>