<template>
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <div v-if="showTitle" class="form_title">
      <!--      基本信息-->
      {{ t("admin.form.basicInfo") }}
    </div>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.logo', { type: t('admin.form.organization') })"
        >
          <ImageUpload v-model="modelValue.image" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.name', { type: t('admin.form.organization') })"
          prop="name"
        >
          <el-input v-model="modelValue.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item :label="t('admin.form.organizationCode')" prop="code">
          <el-row justify="space-between" type="flex">
            <el-col :span="20">
              <el-input v-model="modelValue.code"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row>

    <el-form-item :label="t('admin.form.country')" prop="country">
      <el-select
        v-model="modelValue.country"
        :placeholder="
          t('admin.form.pleaseSelect', { type: t('admin.form.country') })
        "
      >
        <el-option
          v-for="option in countryOptions"
          :label="option[locale]"
          :value="option.key"
        />
      </el-select>
    </el-form-item>

    <el-row>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.address')">
          <el-input
            v-model="modelValue.address"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.address') })
            "
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <field-options
      ref="fieldFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="researchs"
      :placeholder="
        t('admin.form.placeholder', { type: t('admin.form.researchDirection') })
      "
      :title="t('admin.form.researchDirection')"
    />

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item :label="t('admin.form.businessLicense')" prop="license">
          <ImageUpload
            v-model="modelValue.license"
            :isShowTip="false"
            :limit="1"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="
            t('admin.form.introduction', { type: t('admin.form.organization') })
          "
          prop="introduce"
        >
          <el-input
            v-model="modelValue.introduce"
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import { computed, reactive, ref } from "vue";
import FieldOptions from "../FieldOptions";
import InputBoxAdd from "../InputBoxAdd";
import { useI18n } from "vue-i18n";
import { countryOptions } from "@/utils/parameter";

const { t, locale } = useI18n();
const formRef = ref();
// const cityFormRef = ref();
const fieldFormRef = ref();
const directionsFormRef = ref();

const handleBlur = (value) => {
  console.log(value);
};
const props = defineProps({
  modelValue: Object,
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});
const rules = reactive({
  product: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  name: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  kind: [
    {
      required: true,
      message: computed(() => t("admin.form.pleaseSelect")),
      trigger: "change",
    },
  ],
  country: [
    {
      required: true,
      message: computed(() =>
        t("admin.form.pleaseSelect", {
          type: t("admin.form.country"),
        })
      ),
      trigger: "change",
    },
  ],
  code: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  mobile: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
    {
      pattern: /^1[3456789]\d{9}$/,
      message: computed(() =>
        t("admin.validation.pleaseEnterTheCorrectFormatOfThePhoneNumber")
      ),
      trigger: "blur",
    },
  ],
  research_id: [
    {
      required: true,
      message: computed(() => t("admin.form.pleaseSelect")),
      trigger: "change",
    },
  ],
  tenant_id: [
    {
      required: true,
      message: computed(() => t("admin.form.pleaseSelect")),
      trigger: ["blur", "change"],
    },
  ],
  school: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  education: [
    {
      required: true,
      message: computed(() => t("admin.form.pleaseSelect")),
      trigger: "change",
    },
  ],
  major: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  job: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  title: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  workAt: [
    {
      required: true,
      message: computed(() =>
        t("admin.validation.required", { type: t("admin.form.workingTime") })
      ),
      trigger: ["change", "blur"],
    },
  ],
  license: [
    {
      required: true,
      message: computed(() => t("admin.validation.pleaseUpload")),
      trigger: ["blur", "change"],
    },
  ],
  introduce: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: ["change", "blur"],
    },
  ],
});

const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  // const cityFormValid = await cityFormRef.value.validateForm();
  const fieldFormValid = await fieldFormRef.value.validateForm();
  const directionsFormValid = await directionsFormRef.value.validateForm();
  // const introduceValid = await introduceRef.value.validateForm();
  return (
    formValid && fieldFormValid && directionsFormValid
    // introduceValid
  );
};
defineExpose({
  validateForm,
});
</script>

<style lang="scss" scoped>
.form_title {
  font-weight: 700;
  margin-bottom: 30px;
}

.el-select,
.el-date-editor {
  display: block;
  width: 100%;
}
</style>
