<!-- banner管理 -->
<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <div class="content_top">
        <el-form :model="formData" :rules="formRules" ref="formRef" style="max-width: 1048px" class="form">
          <el-form-item label="公司名称" prop="company_name">
            <el-input v-model="formData.company_name" placeholder="请输入公司名称" show-word-limit maxlength="50" />
          </el-form-item>
          <el-form-item label="公司地址" prop="company_address">
            <el-input v-model="formData.company_address" placeholder="请输入公司地址" show-word-limit maxlength="100" />
          </el-form-item>
          <el-form-item label="公司logo" prop="logo">
            <bg-upload-image
              v-model="formData.logo"
              :fileSize="500"
              :fileSizeUnit="'KB'"
              :showTips="true"
              customTips="请选择图片上传：大小80 * 80像素支持jpg、png等格式，图片需小于500KB"
              :limit="1"
              listType="picture-card"
              :accept="['.jpg', '.jpeg', '.png']"></bg-upload-image>
          </el-form-item>
          <el-form-item label="公司电话" prop="contact_phone">
            <el-input v-model="formData.contact_phone" placeholder="请输入公司电话" show-word-limit maxlength="20" />
          </el-form-item>
        </el-form>
      </div>
      <div class="content_bottom">
        <div>
          <el-button type="primary" @click="submit">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, ref, onBeforeMount, nextTick } from "vue";
import axios from "../../../../request/http.js";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
const router = useRouter();
const route = useRoute();
const formRef = ref(null);

const checkPhone = (rule, value, callback) => {
  let reg = /^((1[0-9]{10})|(((([0-9]{3}-)?[0-9]{8})|(([0-9]{4}-)?[0-9]{7}))(-[0-9]{1,4})?))$/;
  if (value !== "") {
    console.log(1);
    if (!reg.test(value)) {
      console.log(2);
      callback(new Error("电话号码格式不正确，请重新输入"));
    } else {
      console.log(3);
      callback();
    }
  } else {
    callback();
  }
};
const state = reactive({
  formData: {
    company_name: "",
    company_address: "",
    logo: [],
    contact_phone: "",
  },
  formRules: {
    company_name: [
      { required: true, message: "请输入公司名称", trigger: "blur" },
      { max: 50, message: "长度最大为50个字符", trigger: "blur" },
    ],
    company_address: [],
    logo: [],
    contact_phone: [{ validator: checkPhone, trigger: "blur" }],
  },
});

const getFootInfo = () => {
  axios
    .get(`/apaas/system/v5/config/get`)
    .then((res) => {
      if (res.data.code == 200) {
        const data = res.data.data;
        state.formData = {
          company_name: data.company_name,
          company_address: data.company_address,
          logo: data.logo ? [{ url: data.logo }] : [],
          contact_phone: data.contact_phone,
        };
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

const submit = () => {
  formRef.value.validate((valid, fields) => {
    if (valid) {
      let params = {
        ...state.formData,
        logo: state.formData.logo && state.formData.logo.length > 0 ? state.formData.logo[0].url : "",
      };
      axios.post(`/apaas/system/v5/config/set`, params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
        } else {
          ElMessage.error(res.data.data);
        }
      });
    }
  });
};

onBeforeMount(() => {
  getFootInfo();
});

const { formData, formRules } = toRefs(state);
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  padding: 0 24px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  .main_container {
    flex-grow: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    background-color: #fff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    margin: 0 0 16px;
    position: relative;
    height: calc(100% - 62px);
    // padding: 24px;
    .content_top {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: auto;
      padding: 24px;
      .form {
        .el-form-item {
          display: block;
          :deep().bg-upload {
            .el-upload--picture-card {
              width: 80px;
              height: 80px;
              background-color: #fff;
              border: 1px solid #dadee7;
            }
            .el-upload-list--picture-card .el-upload-list__item {
              width: 80px;
              height: 80px;
            }
          }
        }
      }
    }
    .content_bottom {
      height: 68px;
      line-height: 68px;
      text-align: right;
      padding: 0 20px;
      border-top: 1px solid #e6e9ef;
      .el-button {
        width: 92px;
      }
    }
  }
}
</style>
