<template>
  <div>
    <LHeader :text="text"></LHeader>
    <van-form
      @submit="onSubmit"
      :show-error="false"
      :scroll-to-error="true"
      validate-trigger="onSubmit"
    >
    <van-field
        readonly
        clickable
        name="applyDate"
        :value="delayTime"
        label="延期时间"
        placeholder="点击选择日期"
        @click="showDelayTime = true; currentDate = new Date()"
        :rules="[{ required: true, message: '延期时间不能为空' }]"
      />
      <van-popup v-model="showDelayTime" position="bottom">
        <van-datetime-picker
          type="datetime"
          v-model="currentDate"
          @confirm="onConDelayTime"
          @cancel="showDelayTime = false"
        />
      </van-popup>

      <van-field
        v-model="delayApply"
        label="延期理由"
        name="applyReason"
        rows="3"
        type="textarea"
        placeholder="请输入"
        :rules="[{ required: true, message: '延期理由不能为空' }]"
      />

      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit"
          >保存</van-button
        >
      </div>
    </van-form>
    <div style="margin:10px 16px 0px;padding-bottom:16px">
      <van-button round block type="warning" @click.native="cancel"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import LHeader from "@/components/header.vue";
import { timestampToTime } from "@/utils/format";
import { delayAdd } from "@/service/danger";
export default {
  components: {
    LHeader
  },
  data() {
    return {
      text: "延期申请",
      taskId: "",
      delayTime: "", // 延迟时间
      showDelayTime: false,
      delayApply: "", // 延迟理由
      currentDate: new Date(),

    };
  },
  created() {
    this.taskId = this.$route.params.taskId;
  },
  methods: {
   onSubmit(values) {
    this.$toast.loading({
      message: "提交中...",
      forbidClick: true,
      loadingType: "spinner",
      duration: 0,
    });
    let formdata = new FormData();
    formdata.append("applyDate", values.applyDate);
    formdata.append("applyReason", values.applyReason);
    formdata.append("taskId", this.taskId);
    delayAdd(`/delayApply/add`, formdata)
      .then((res) => {
        this.$toast.clear();
        this.$toast.success({
          message: "提交成功",
          duration: 2000,
        });
        history.go(-1);
      })
      .catch(() => {
        this.$toast.clear();
        this.$toast.fail("提交失败,请稍后再试");
      });
  },
    // 延期时间
    onConDelayTime(date) {
      this.delayTime = timestampToTime(date, "DT1", true);
      this.showDelayTime = false;
    },
   
    cancel() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
</style>
