<template>
  <div>
    <LHeader :text="text"></LHeader>

    <van-cell-group
      inset
      v-for="(item, index) in messageList"
      :key="index"
      @click="goDetail(item.taskId)"
    >
      <van-row gutter="">
        <van-col span="5">所属项目:</van-col>
        <van-col span="19">{{ item.proId }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">风险等级:</van-col>
        <van-col span="19">{{ item.riskLevel }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">风险源:</van-col>
        <van-col span="19">{{ item.riskSource }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">事故类型:</van-col>
        <van-col span="19">{{ item.accidentType }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">管控层级:</van-col>
        <van-col span="19">{{ item.controllevel }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">主责部门:</van-col>
        <van-col span="19">{{ item.responsibilityDept }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">主责人员:</van-col>
        <van-col span="19">{{ item.responsibilityMember }}</van-col>
      </van-row>
      <van-row gutter="">
        <van-col span="5">工单状态:</van-col>
        <van-col span="19">{{ item.taskName }}</van-col>
      </van-row>
      <!-- 详情 -->
      <div class="detail">详情</div>
    </van-cell-group>

    <van-form
      @submit="onSubmit"
      :show-error="false"
      :scroll-to-error="true"
      validate-trigger="onSubmit"
    >
      <van-field name="radio" 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
          v-model="agreeIdea"
          label="意见"
          name="confirmOpinion"
          rows="3"
          type="textarea"
          placeholder="请输入"
          :rules="[{ required: true, message: '意见不能为空' }]"
        />
      </div>

      <!-- 退回是的选项 -->
      <div class="disagree" v-else>
        <van-field
          v-model="disagreeIdea"
          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 { postriskConAdd } from "@/service/risk";
export default {
  name: "risk-affirm",
  components: {
    LHeader
  },
  data() {
    return {
      taskId: "",
      messageList: [],
      radio: "1",
      text: "风险确认",
      agreeIdea: "", // 同意意见
      disagreeIdea: "" //退回意见
    };
  },

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (from.name === "risk-confirme") {
        vm.messageList = [];
        let paramsData = to.params.data;
        vm.taskId = paramsData.taskId;
        vm.messageList.push(paramsData);
        vm.agreeIdea = "";
        vm.disagreeIdea = ""; // 隐患整改人
      }
    });
  },
  created() {},
  methods: {
    selectResult(val) {
      this.radio = val;
    },

    onSubmit(values) {
      if (!this.taskId) {
        return;
      }
      let formdata = new FormData();
      formdata.append("confirmResult", values.radio);
      formdata.append("confirmOpinion", values.confirmOpinion);
      this.$toast.loading({
        message: "提交中...",
        forbidClick: true,
        loadingType: "spinner",
        duration: 0
      });
      postriskConAdd(`/riskConfirm/add/${this.taskId}`, formdata)
        .then(res => {
          this.$toast.clear();
          this.$toast.success({
            message: "提交成功",
            duration: 2000
          });
          history.go(-1);
        })
        .catch(() => {
          this.$toast.clear();
          this.$toast.fail("提交失败,请稍后再试");
        });
    },
    // 详情
    goDetail(taskId) {
      this.$router.push({
        name: "risk-big-detail",
        params: {
          id: taskId
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
.van-cell-group--inset {
  margin: 0;
  margin-bottom: 10px;
  padding: 10px;
  font-size: 13px;
  position: relative;
  .van-row {
    margin-bottom: 0.133333rem;
    line-height: 0.64rem;
  }
  .van-overlay {
    position: absolute;
    .wrapper {
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      height: 100%;
    }
  }
}
.detail {
  position: absolute;
  bottom: 0.32rem;
  right: 15px;
  color: #2a80f7;
  font-weight: bolder;
  z-index: 99;
}
</style>
