<template>
  <div class="container">
    <div class="title">活动设置</div>
    <van-cell-group class="all">
      <van-cell title="活动标题" style="font-size:14px;">
        <template slot="default">
          <van-field v-model="title" placeholder="请输入活动标题" class="right" style="font-size:14px;" />
        </template>
      </van-cell>
      <van-cell title="活动开始时间" style="font-size:14px;">
        <template slot="default">
          <div class="nobr">
            <van-field
              class="right"
              v-model="startTime1"
              placeholder="请选择开始时间"
              readonly="readonly"
              @click="startShow = true"
            />
            <van-popup v-model="startShow" position="bottom" :overlay="true">
              <van-datetime-picker
                v-model="startTime"
                type="date"
                @cancel="startShow = false"
                @confirm="handleStartTime"
                @change="startTimeChange"
              />
            </van-popup>
          </div>
        </template>
      </van-cell>
      <van-cell title="活动结束时间" style="font-size:14px;">
        <template slot="default">
          <div class="jpsl">
            <van-field
              class="right"
              v-model="endTime1"
              placeholder="请选择结束时间"
              readonly="readonly"
              @click="endShow = true"
            />
            <van-popup v-model="endShow" position="bottom" :overlay="true">
              <van-datetime-picker
                v-model="endTime"
                type="date"
                @cancel="endShow = false"
                @confirm="handleEndTime"
                @change="endTimeChange"
              />
            </van-popup>
          </div>
        </template>
      </van-cell>
      <van-cell title="背景图片" style="font-size: 14px">
        <van-uploader v-model="bg_imgs" :max-count="1" :after-read="afterBGRead" />
      </van-cell>
      <van-cell title="活动Logo" style="font-size: 14px">
        <van-uploader :max-count="1" v-model="logo_imgs" :after-read="afterLogoRead"></van-uploader>
      </van-cell>
      <van-cell title="单日最大发放数量">
        <template slot="default">
          <div class="jpsl">
            <van-field
              class="right noborder"
              readonly
              clickable
              :value="maxNumber"
              placeholder="请输入限制次数"
              @touchstart.native.stop="maxNumber_show = true"
            />
            <van-number-keyboard
              v-model="maxNumber"
              :show="maxNumber_show"
              :maxlength="6"
              @blur="maxNumber_show = false"
            />
          </div>
        </template>
      </van-cell>
      <van-cell title="总发放数量">
        <template slot="default">
          <div class="jpsl">
            <van-field
              class="right noborder"
              readonly
              clickable
              :value="totalLimit"
              placeholder="请输入限制次数"
              @touchstart.native.stop="totalLimit_show = true"
            />
            <van-number-keyboard
              v-model="totalLimit"
              :show="totalLimit_show"
              :maxlength="6"
              @blur="totalLimit_show = false"
            />
          </div>
        </template>
      </van-cell>
      <van-cell title="选择优惠券" value="1" is-link @click="showCoupon = true" center />
    </van-cell-group>
    <!-- 活动描述 -->
    <div class="detail">
      <div class="border">
        <div class="des">活动描述</div>
        <van-field
          class="area"
          v-model="message"
          rows="2"
          autosize
          type="textarea"
          placeholder="请输入描述"
          show-word-limit
        />
      </div>
    </div>
    <!-- 活动图片 -->
    <div class="detail" style="margin-top:0;">
      <div class="des">活动图片</div>
      <van-uploader v-model="fileList" multiple :max-count="9" />
    </div>
    <div class="creat" @click="handleCreate">创建活动</div>
    <van-action-sheet v-model="showCoupon" :actions="coupons" cancel-text="取消" />
  </div>
</template>

<script>
import * as API_Active from "@/api/active";
export default {
  data() {
    return {
      logo_imgs: [],
      bg_imgs: [],
      title: "送券",
      title2: "",
      coupon: "",
      newCustomer: "",
      condition: "",
      type: "",
      show: false,
      show2: false,
      cashCoupon: "",
      limit: "",
      startTime: new Date(),
      startTime1: "2020-04-09 08:25:25",
      endTime: new Date(),
      endTime1: "2020-04-10 08:25:25",
      startShow: false,
      endShow: false,
      message: "123",
      fileList: [],
      showCoupon: false,
      coupons: [
        { name: "选项" },
        { name: "选项" },
        { name: "选项", subname: "描述信息" }
      ],
      maxNumber: "1",
      maxNumber_show: false,
      totalLimit: "2",
      totalLimit_show: false
    };
  },
  methods: {
    getCoupons() {
      let id = sessionStorage.getItem("oyStallCode") || 1;
      API_Active.getAllCouponsByOyStallCode(id).then(res => {
        this.coupons = res.data;
      });
    },
    async afterLogoRead(file) {
      let logo_img = await this.getImgUrl(file.file);
      if (logo_img.result === "success") {
        let item = {
          url: bg_img.data.imgPath,
          status: "done",
          message: "上传成功"
        };
        this.logo_imgs.push(item);
      }
    },
    async afterBGRead(file) {
      let bg_img = await this.getImgUrl(file.file);
      if (bg_img.result === "success") {
        let item = {
          url: bg_img.data.imgPath,
          status: "done",
          message: "上传成功"
        };
        this.bg_imgs.push(item);
      }
    },
    // 上传图片
    async getImgUrl(file) {
      let params = new FormData();
      params.append("file", file);
      let url = "http://139.155.48.151:8084/admin/auth/util/saveImg";
      const res = await axios.post(url, params);
      return res;
    },
    handleCreate() {
      let params = {
        activityInfo: {
          activityName: this.title,
          startTime: this.startTime1,
          endTime: this.endTime1,
          backImage: this.bg_imgs[0] ? this.bg_imgs[0].url : "",
          logo: this.logo_imgs[0] ? this.logo_imgs[0].url : "",
          joinLimit: this.maxNumber,
          totalLimit: this.totalLimit,

          des: this.message,
          image: JSON.stringify(this.fileList)
        }
      };
      API_Active.createActive(params).then(res => {
        if (res.result === "fail") {
          this.$toast(res.errorMsg);
          return;
        }
        Toast.success("创建成功");
        setTimeout(() => {
          this.$router.push("/createSuccess");
        }, 200);
      });
    },
    timeFormat(time) {
      let year = 1900 + time.getYear();
      let month = "0" + (time.getMonth() + 1);
      let date = "0" + time.getDate();
      let hour = "0" + time.getHours();
      let minute = "0" + time.getMinutes();
      return (
        year +
        "-" +
        month.substring(month.length - 2, month.length) +
        "-" +
        date.substring(date.length - 2, date.length) +
        " " +
        hour.substring(hour.length - 2, hour.length) +
        ":" +
        minute.substring(minute.length - 2, minute.length) +
        ":00"
      );
    },
    startTimeChange(e) {
      let startTimeArr = e.getValues();
      this.startTime1 = `${startTimeArr[0]}-${startTimeArr[1]}-${startTimeArr[2]}`;
    },
    handleStartTime(value) {
      this.startTime1 = this.timeFormat(value);
      this.startShow = false;
    },
    handleEndTime(value) {
      this.endTime1 = this.timeFormat(value);
      this.endShow = false;
    },
    endTimeChange(e) {
      let endTimeArr = e.getValues();
      this.endTime1 = `${endTimeArr[0]}-${endTimeArr[1]}-${endTimeArr[2]}`;
    }
  },
  mounted() {}
};
</script>
<style></style>

<style scoped>
.condition {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.phone {
  margin-right: 10px;
}
.all >>> input {
  text-align: right;
}
.noborder >>> .van-cell:not(:last-child)::after {
  display: none !important;
}
.npbr .van-cell:not(:last-child)::after {
  display: none !important;
}
.creat {
  margin-top: 80px;
  width: 96%;
  height: 40px;
  background: rgba(117, 178, 253, 1);
  border-radius: 10px;
  text-align: center;
  line-height: 40px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  margin-left: 2%;
  margin-bottom: 16px;
}
.container {
  height: auto;
  display: flex;
  flex-direction: column;
  background: rgba(248, 248, 248, 1);
  min-height: 100%;
}
.area >>> .van-field__control {
  background: #f8f8f8;
  padding: 12px;
}
.des {
  padding: 5px 18px 0px;
  font-size: 14px;
  color: #2d476a;
}

.title {
  background-color: #f8f8f8;
  height: 36px;
  line-height: 36px;
  font-size: 12px;
  padding-left: 16px;
  color: #2d476a;
}
[data-v-08d4afe1] .van-cell {
  height: 100%;
  font-size: 14px;
}
.right {
  padding: 0;
}
.van-cell__title {
  color: #2d476a !important;
}
</style>