<template>
  <div class="mainView">
    <bread-crumb />
    <div class="letterModule" v-if="!isSubmit">
      <el-form
        :model="formData"
        ref="ruleFormRef"
        :formData="formData"
        label-width="120"
        :rules="rules"
      >
        <el-form-item label="标题">
          <el-input size="large" disabled v-model="formData.title" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input
            size="large"
            v-model="formData.nickname"
            placeholder="请填写昵称"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            type="textarea"
            size="large"
            :rows="5"
            placeholder="请填写内容"
            v-model="formData.content"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNumber">
          <el-input
            size="large"
            v-model="formData.mobileNumber"
            placeholder="请填写手机号"
          />
        </el-form-item>
        <el-form-item class="imgCode" label="图形验证码" prop="checkCode">
          <el-input
            size="large"
            maxlength="4"
            v-model="formData.checkCode"
            placeholder="请填写验证码"
          />
          <el-image
            size="large"
            :src="checkCode.imgCode"
            @click="getCodeImg()"
            v-if="checkCode.imgCode"
          />
          <el-button
            link
            v-else
            size="large"
            type="primary"
            @click="getCodeImg()"
            >获取验证码</el-button
          >
        </el-form-item>
      </el-form>
      <div class="btnBox">
        <el-button
          size="large"
          type="primary"
          @click="submitLetter(ruleFormRef)"
          >提交</el-button
        >
      </div>
    </div>
    <div class="message" v-else>
      <el-icon color="#67C23A"><Checked /></el-icon>
      <p>留言成功</p>
      <el-button type="primary" link @click="goBack()">返回</el-button>
    </div>
  </div>
</template>
<script setup name="wyly">
import api from "@/api/hdjl.js";
import codeApi from "@/api/captcha.js";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { ref, onMounted, reactive } from "vue";
import { validateMobile, valiReportCode } from "@/utils/filterRuies.js";
import BreadCrumb from "@/components/breadcrumbModule/index.vue";
const ruleFormRef = ref();
const isSubmit = ref(false);
const formData = reactive({
  title: "",
  mobileNumber: "",
  publicOpinionCollectId: "",
});

const checkCode = reactive({
  code: "",
  imgCode: "",
});
const router = useRouter();
formData.publicOpinionCollectId = router.currentRoute.value.query.id;
onMounted(() => {
  getDetalis();
});

const rules = {
  nickname: [
    {
      required: true,
      trigger: "blur",
      message: "请填写昵称",
    },
  ],
  content: [
    {
      required: true,
      trigger: "blur",
      message: "请填写信件内容",
    },
  ],
  phoneNum: [
    {
      validator: validateMobile,
      trigger: "blur",
    },
  ],
  checkCode: [
    {
      required: true,
      trigger: "blur",
      message: "请填写验证码",
    },
    {
      validator: (rule, value, callback) => {
        valiReportCode(value, callback, checkCode, getCodeImg);
      },
      trigger: "blur",
    },
  ],
};

// 获取文章详情
function getDetalis() {
  api.popularWilldetail({ id: formData.publicOpinionCollectId }).then((res) => {
    if (res.code == 200) {
      formData.title = res.data.icPublicOpinionCollect.collectionTitle;
    }
  });
}

// 提交
function submitLetter() {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      api.leaveMessage(formData).then((res) => {
        if (res.code == 200) {
          isSubmit.value = true;
        }
      });
    } else {
      ElMessage.error("请补充完整信息");
    }
  });
}

// 获取图形校验码
function getCodeImg() {
  codeApi.getPicCaptcha().then((res) => {
    checkCode.code = res.data.code;
    checkCode.imgCode = "data:image/png;base64," + res.data.file;
  });
}
//返回
function goBack() {
  router.back();
}
</script>
<style scoped lang="less">
.mainView {
  .letterModule {
    width: 80%;
    margin: 30px auto;
    .el-form {
      .el-form-item.imgCode {
        .el-input {
          width: 120px;
          line-height: 36px;
          margin-right: 20px;
        }

        .el-image {
          width: 120px;
          height: 40px;
        }
      }
    }
    .btnBox {
      flex: 1;
      display: flex;
      margin-top: 50px;
      justify-content: center;

      .el-button {
        width: 220px;
      }
    }
  }
  .message {
    width: 100px;
    margin: 50px auto;
    text-align: center;
    .el-icon {
      font-size: 100px;
    }
    p {
      color: #67c23a;
      font-size: 16px;
      margin-top: 20px;
      font-weight: bold;
    }
    .el-button {
      margin: 30px 0;
    }
  }
}
</style>
