<template>
  <div class="mainView">
    <bread-crumb />
    <div class="editInfo" v-if="!isSubmit">
      <el-form
        :rules="rules"
        :model="formData"
        label-width="180"
        ref="ruleFormRef"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            size="large"
            clearable
            v-model="formData.title"
            placeholder="请填写标题"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content" class="length">
          <el-input
            size="large"
            :rows="8"
            type="textarea"
            v-model="formData.content"
            placeholder="请填写内容"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNumber" class="length code">
          <el-input
            size="large"
            clearable
            v-model.number="formData.mobileNumber"
            placeholder="请填写手机号"
          />
        </el-form-item>
        <el-form-item
          label="验证码"
          prop="code"
          class="length code graphicCode"
        >
          <el-input
            size="large"
            clearable
            maxlength="4"
            v-model="formData.code"
            placeholder="请填写验证码"
          />
          <el-image
            size="large"
            :src="checkCode.imgCode"
            @click="getCodeImg()"
          />
        </el-form-item>
      </el-form>
      <div class="btnBox">
        <el-button size="large" type="primary" @click="submitData"
          >提交</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 lang="ts" name="">
import { ref, reactive, onMounted } from "vue";
import "@/style/hdjl/xfjb.less";
import api from "@/api/hdjl.js";
import { ElMessage } from "element-plus";
import codeApi from "@/api/captcha.js";
import { useRouter } from "vue-router";
import { validateMobile, valiReportCode } from "@/utils/filterRuies.js";
import BreadCrumb from "@/components/breadcrumbModule/index.vue";

const checkCode = reactive({
  code: "",
  imgCode: "",
});
const router = useRouter();
const ruleFormRef = ref();
const isSubmit = ref(false);

let formData = reactive({
  code: "",
  title: "",
  content: "",
  mobileNumber: "",
});

const rules = {
  title: [
    {
      required: true,
      trigger: "blur",
      message: "请填写信件标题",
    },
  ],
  content: [
    {
      required: true,
      trigger: "blur",
      message: "请填写信件内容",
    },
  ],
  mobileNumber: [
    {
      validator: (rule, value, callback) => {
        if (value && !/^1[3-9]\d{9}$/.test(value)) {
          callback(new Error("手机号格式不正确"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  code: [
    {
      required: true,
      trigger: "blur",
      message: "请填写验证码",
    },
    {
      validator: (rule, value, callback) => {
        valiReportCode(value, callback, checkCode, getCodeImg);
      },
      trigger: "blur",
    },
  ],
};
onMounted(() => {
  getCodeImg();
});
//提交
function submitData() {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      api.leaderMailbox(formData).then((res) => {
        if (res.code == 200) {
          isSubmit.value = true;
        }
      });
    }
  });
}

//返回
function goBack() {
  router.push({
    path: "/hdjl/info",
  });
}
// 获取图形校验码
function getCodeImg() {
  codeApi.getPicCaptcha().then((res) => {
    console.log(res);
    checkCode.code = res.data.code;
    checkCode.imgCode = "data:image/png;base64," + res.data.file;
  });
}
</script>

<style scoped lang="less">
.mainView {
  .editInfo {
    .el-form {
      .el-form-item:first-child {
        width: 100%;
        .el-input {
          width: 100%;
        }
      }
    }
  }
  .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>
