<template>
  <div>
    <LHeader :text="text"></LHeader>
    <van-form
      @submit="onSubmit"
      :show-error="false"
      :scroll-to-error="true"
      validate-trigger="onSubmit"
    >
      <van-field name="confirmResult" label="隐患确认信息">
        <template #input>
          <van-radio-group
            v-model="radio"
            direction="horizontal"
            @change="selectResult"
          >
            <van-radio name="1">同意</van-radio>
            <van-radio name="0">退回</van-radio>
          </van-radio-group>
        </template>
      </van-field>

      <!-- 同意时的选项 -->
      <div class="agree" v-if="radio == 1">
        <van-field
          readonly
          clickable
          name="endTime"
          :value="value"
          label="整改截止时间"
          placeholder="点击选择日期"
          @click="showCalendar = true"
          :rules="[{ required: true, message: '请选择日期' }]"
        />
        <van-popup v-model="showCalendar" position="bottom">
          <van-datetime-picker
            type="datetime"
            @confirm="onConfirm"
            @cancel="showCalendar = false"
          />
        </van-popup>

        <van-field
          readonly
          clickable
          name="rectificationUser"
          :value="valueRect"
          label="隐患整改人"
          placeholder="请选择"
          @click="showPickerRect = true"
          :rules="[{ required: true, message: '请选择隐患整改人' }]"
        />
        <van-popup v-model="showPickerRect" position="bottom">
          <van-picker
            show-toolbar
            value-key="userName"
            :columns="columnsRect"
            @confirm="onConRect"
            @cancel="showPickerRect = false"
          />
        </van-popup>

        <van-field
          readonly
          clickable
          name="rectificationReview"
          :value="valueReview"
          label="隐患复查人"
          placeholder="请选择"
          @click="showPickerReview = true"
          :rules="[{ required: true, message: '请选择隐患复查人' }]"
        />
        <van-popup v-model="showPickerReview" position="bottom">
          <van-picker
            show-toolbar
            value-key="userName"
            :columns="columnsReview"
            @confirm="onConReview"
            @cancel="showPickerReview = false"
          />
        </van-popup>

          <van-field
            v-model="valueIdea"
            label="意见"
            name="confirmOpinion"
            rows="3"
            type="textarea"
            placeholder="请输入"
            :rules="[{ required: true, message: '意见不能为空' }]"
          />
      </div>

      <!-- 退回是的选项 -->
      <div class="disagree" v-else>
        <van-field
          v-model="returnReason"
          label="退回原因"
          name="confirmOpinion"
          rows="3"
          type="textarea"
          placeholder="请输入"
          :rules="[{ required: true, message: '退回原因不能为空' }]"
        />
      </div>

      <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 { getConfirmFormList, dangerConSub } from "@/service/danger";
export default {
  components: {
    LHeader
  },
  data() {
    return {
      taskId: "",
      radio: "1",
      text: "隐患确认",
      value: "",
      showCalendar: false,
      rectId: "", // 隐患整改人
      valueRect: "",
      columnsRect: [],
      showPickerRect: false,
      reviewId: "", // 隐患复查人
      valueReview: "",
      columnsReview: [],
      showPickerReview: false,
      valueIdea: "",
      returnReason: ""
    };
  },
  created() {
    this.taskId = this.$route.params.taskId;
    this.getFormList();
  },
  methods: {
    getFormList() {
      if (!this.taskId) {
        return;
      }
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0
      });
      getConfirmFormList(`confirm/add/${this.taskId}`)
        .then(res => {
          this.$toast.clear();
          this.columnsRect = res.data.rectificationUser;
          this.columnsReview = res.data.rectificationReview;
        })
        .catch(() => {
          this.$toast.clear();
          this.$toast.fail("加载失败,请稍后再试");
        });
    },

    // 保存时的操作
    onSubmit(values) {
      console.log("submit", values);
      if (!this.taskId) {
        return;
      }
      let formdata = new FormData()
      formdata.append('confirmResult', this.radio)
      formdata.append('endTime', values.endTime)
      formdata.append('rectificationUser', this.rectId)
      formdata.append('rectificationReview', this.reviewId)
      formdata.append('confirmOpinion', values.confirmOpinion)
      this.$toast.loading({
        message: "提交中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0
      });
      dangerConSub(`/confirm/add1/${this.taskId}`,formdata)
        .then(res => {
          this.$toast.clear();
          this.$toast.success({
            message: "提交成功",
            duration: 2000
          });
          history.go(-1);
        })
        .catch(() => {
          this.$toast.clear();
          this.$toast.fail("提交失败,请稍后再试");
        });
    },

    selectResult(val) {
      this.radio = val;
      this.returnReason = "";
      this.valueIdea = "";
    },
    onConfirm(date) {
      this.value = timestampToTime(date, "DT1", true);
      this.showCalendar = false;
    },

    onConRect(value) {
      this.valueRect = value.userName;
      this.rectId = value.userId;
      this.showPickerRect = false;
    },
    onConReview(value) {
      this.valueReview = value.userName;
      this.reviewId = value.userId;
      this.showPickerReview = false;
    },

    cancel() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
</style>
