<template>
  <view class="box min-page">
    <view class="container ">
      <view class="u-flex u-row-between">
        <text class="font-bold color-333">满减优惠</text>
        <button class="add" @click="addThresholds">添加</button>
      </view>

      <view
        class="u-m-t-32 u-flex u-row-between gap-40"
        v-for="(item, index) in form.thresholds"
        :key="index"
      >
        <view class="u-flex u-flex-1">
          <text class="text-tips">满</text>
          <input
            class="my-input"
            type="number"
            v-model="item.fullAmount"
            @blur="checkFullAmount(index)"
            placeholder=""
          />
          <text class="text-tips text-tips1">元</text>
        </view>
        <view class="u-flex u-flex-1">
          <text class="text-tips">减</text>
          <input
            class="my-input"
            type="number"
            v-model="item.discountAmount"
            @blur="checkDiscountAmount(index)"
            placeholder=""
          />
          <text class="text-tips text-tips1">元</text>
        </view>
        <text class="color-red" @click="deleteThreshold(index)">删除</text>
      </view>
    </view>
    <view class="container" style="padding-left: 0; padding-right: 0">
      <view class="x-padding">
        <view class="u-flex u-row-between u-m-b-32">
          <text class="font-bold color-333">活动日期</text>
        </view>
        <timeArea
          v-model:startDate="form.validStartTime"
          v-model:endDate="form.validEndTime"
        ></timeArea>
      </view>
	  <view class="u-m-t-32">
		  <up-line ></up-line>
	  </view>
      <view class="u-m-t-32 x-padding">
        <view class="u-flex u-row-between">
          <text class="font-bold color-333 u-m-b-32">可用周期</text>
        </view>
        <weekSel  v-model="form.useDays"></weekSel>
      </view>
    </view>

    <view class="u-m-t-32 container">
      <view class="u-flex u-row-between">
        <text class="font-bold color-333 u-m-b-32">指定时间段</text>
      </view>
      <hourSel
        v-model:useTimeType="form.useTimeType"
        v-model:startValue="form.useStartTime"
        v-model:endValue="form.useEndTime"
      ></hourSel>
    </view>
    <view class="u-m-t-32 container">
      <view class="u-flex u-row-between">
        <text class="font-bold color-333  u-m-b-32">可使用类型</text>
      </view>
      <my-dine-types v-model="form.useType"></my-dine-types>
    </view>
    <view class="u-m-t-32 container">
      <view class="u-flex u-row-between">
        <text class="font-bold color-333 u-m-r-38">排序值</text>
        <up-input type="number" v-model="form.sort" placeholder="默认值：0" />
      </view>
      <view class="u-m-t-16 color-red u-font-24"
        >数值越大，排序越靠前。重复时段下，按照排序值最高的活动减免</view
      >
    </view>

    <view class="container">
      <view class="u-flex u-col-center u-row-between">
        <view>
          <view class="color-333 u-font-28 font-bold">与优惠券同享</view>
          <view class="color-666 u-font-24 u-m-t-16">不能和优惠券同时使用</view>
        </view>
        <up-switch
          :activeValue="1"
          :inactiveValue="0"
          v-model="form.couponShare"
          size="18"
        ></up-switch>
      </view>
      <view class="u-flex u-m-t-32 u-col-center u-row-between">
        <view>
          <view class="color-333 u-font-28 font-bold">与限时折扣同享</view>
          <view class="color-666 u-font-24 u-m-t-16"
            >开启后，计算门槛时将会按照折扣价计算</view
          >
        </view>
        <up-switch
          :activeValue="1"
          :inactiveValue="0"
          v-model="form.discountShare"
          size="18"
        ></up-switch>
      </view>
      <view class="u-flex u-m-t-32 u-col-center u-row-between">
        <view>
          <view class="color-333 u-font-28 font-bold"
            >与会员价/会员折扣同享</view
          >
          <view class="color-666 u-font-24 u-m-t-16"
            >开启后，计算门槛时将会按照会员价/会员折扣价计算</view
          >
        </view>
        <up-switch
          :activeValue="1"
          :inactiveValue="0"
          v-model="form.vipPriceShare"
          size="18"
        ></up-switch>
      </view>
      <view class="u-flex u-m-t-32 u-col-center u-row-between">
        <view>
          <view class="color-333 u-font-28 font-bold">与积分 抵扣同享</view>
          <view class="color-666 u-font-24 u-m-t-16"
            >开启后，可和积分抵扣同时使用</view
          >
        </view>
        <up-switch
          :activeValue="1"
          :inactiveValue="0"
          v-model="form.pointsShare"
          size="18"
        ></up-switch>
      </view>
    </view>
    <bottomBtnGroup @save="save" @cancel="cancel"></bottomBtnGroup>
  </view>
</template>

<script setup>
import timeArea from "./components/time-area.vue";
import weekSel from "./components/week-sel.vue";
import hourSel from "./components/hour-area.vue";
import bottomBtnGroup from "./components/bottom-btn-group.vue";
import { reactive } from "vue";
import * as discountActivityApi from "@/http/api/market/discountActivity.js";
import {
  onLoad,
  onReady,
  onShow,
  onPageScroll,
  onReachBottom,
  onBackPress,
} from "@dcloudio/uni-app";

const form = reactive({
  thresholds: [],
  validStartTime: "",
  validEndTime: "",
  useDays: ['周一','周二','周三','周四','周五','周六','周日'],
  useTimeType: "all",
  useStartTime: "",
  useEndTime: "",
  useType: [],
  sort: 0,
  couponShare: 0,
  discountShare: 0,
  vipPriceShare: 0,
  pointsShare: 0,
});
function addThresholds() {
  form.thresholds.push({
    fullAmount: 0,
    discountAmount: 0,
  });
}

function checkFullAmount(index){
  if(form.thresholds[index].fullAmount<=0){
   form.thresholds[index].fullAmount = 0;
  }
  console.log(form.thresholds[index].fullAmount);
}
function checkDiscountAmount(index){
  const { fullAmount, discountAmount } = form.thresholds[index];
  if(discountAmount<=0){
   form.thresholds[index].discountAmount = 0;
  }
  if(discountAmount>=fullAmount){
   form.thresholds[index].discountAmount = fullAmount;
  }
}

async function save() {
  console.log(form);
  const submitData = {
    ...form,
    useDays: form.useDays.join(","),
    useType: form.useType.join(","),
    validStartTime:form.validStartTime,
    validEndTime:form.validEndTime
    // validStartTime:'',
    // validEndTime:'',
  };
  if(!submitData.thresholds.length){
    uni.showToast({
      title: "请添加门槛",
      icon: "none",
      duration: 2000,
    });
    return
  }
  if(!submitData.useDays.length){
    uni.showToast({
      title: "请选择可使用的周几",
      icon: "none",
      duration: 2000,
    });
    return
  }
  if(!submitData.useTimeType){
    uni.showToast({
      title: "请选择可使用的时间类型",
      icon: "none",
      duration: 2000,
    });
    return
  }
  if(!submitData.validStartTime || !submitData.validEndTime){
    uni.showToast({
      title: "请选择活动日期",
      icon: "none",
      duration: 2000,
    });
    return
  }
  if(submitData.useTimeType!='all'){
    if(!submitData.useStartTime || !submitData.useEndTime){
      uni.showToast({
        title: "请选择可使用的时间",
        icon: "none",
        duration: 2000,
      });
      return
    }
  }
  const res = !form.id
    ? await discountActivityApi.add(submitData)
    :await discountActivityApi.update(submitData);
  uni.showToast({
    title: submitData.id ? "更新成功" : "添加成功",
    icon: "none",
    duration: 2000,
    success: function () {
      setTimeout(() => {
        uni.navigateBack();
      }, 2000);
    },
  });
}
function cancel() {
  uni.navigateBack();
}

function deleteThreshold(index) {
  form.thresholds.splice(index, 1);
}
function setForm(data) {
  data.useDays = data.useDays.split(",");
  data.useType = data.useType.split(",");
  data.validStartTime = data.validStartTime.split(" ")[0];
  data.validEndTime = data.validEndTime.split(" ")[0];
  Object.assign(form, data);
  console.log(form);
}
onLoad((opt) => {
    if(!opt.id){
        return 
    }
  const data = uni.getStorageSync("discountActivity");
  if (data) {
    setForm(data);
  }
});
</script>

<style lang="scss" scoped>
.x-padding {
  padding: 0 24rpx;
}
.text-tips {
  color: #999999;
  font-size: 14px;
  padding: 0 28rpx;
  border-radius: 6rpx 0 0 6rpx;
  border: 2rpx solid #d9d9d9;
  background-color: #f7f7fa;
  line-height: 60rpx;
}
.text-tips1 {
  border-radius: 0 6rpx 6rpx 0;
}
.gap-40 {
  gap: 40rpx;
}
.my-input {
  border: 2rpx solid #d9d9d9;
  height: 60rpx;
  border-left: none;
  border-right: none;
  flex: 1;
  text-align: center;
  padding: 0 2px;
  font-size: 14px;

}
.box {
  padding: 32rpx 28rpx 0 28rpx;
  font-size: 28rpx;
  background: #f7f7f7;
}
.container {
  background: #fff;
  padding: 32rpx 24rpx;
  margin-top: 32rpx;
  border-radius: 16rpx;
  overflow: hidden;
}
.add {
  padding: 0 32rpx;
  border-radius: 60rpx;
  background: #318afe;
  color: #ffffff;
  font-size: 28rpx;
  line-height: 56rpx;
  margin: 0;
}
.color-red {
  color: #ff2f2f;
}
</style>