<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-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.companyLogo')">
          <ImageUpload v-model="modelValue.image" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="
            t('admin.form.name', {
              type: t('admin.common.company'),
            })
          "
          prop="name"
        >
          <el-input v-model="modelValue.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.contact')" prop="username">
          <el-input v-model="modelValue.username"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('webSearch.contactInformation')" prop="phone">
          <!--            :maxlength="11"-->
          <el-input
            v-model="modelValue.phone"
          ></el-input>
          <!-- v-number -->
        </el-form-item>
      </el-col>
    </el-row>
    <!--    ADDEMAIL-->
    <!--    邮箱-->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.email')" prop="email">
          <el-input v-model="modelValue.email"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!--    其他联系方式-->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.otherContact')" prop="otherContact">
          <el-input v-model="modelValue.otherContact"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-row>
      <el-col :span="24">
        <el-form-item label="信用代码:" prop="code">
          <el-row type="flex" justify="space-between">
            <el-col :span="20">
              <el-input v-model="modelValue.code"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="">查找</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row> -->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.companyType')" prop="kind">
          <el-select
            v-model="modelValue.kind"
            :placeholder="t('admin.form.pleaseSelect')"
          >
            <el-option
              v-for="item in enterpriseOptions"
              :key="item.key"
              :label="item[locale]"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.coreAchievementProduct')"
          prop="product"
        >
          <el-input v-model="modelValue.product"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <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-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.address')">
          <el-input v-model="modelValue.address"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <FieldOptions
      ref="fieldFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="keywordsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="keywords"
      :placeholder="t('admin.form.inputKeywords')"
      :title="t('admin.form.keywords')"
    />
    <InputBoxAdd
      v-if="isAdd"
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="directions"
      :placeholder="
        t('admin.form.placeholder', {
          type: t('admin.form.productionDirection'),
        })
      "
      :title="t('admin.form.productionDirection')"
    />

    <el-row>
      <!-- <el-col :span="24">
        <el-form-item label="邀请码:">
          <el-input v-model="modelValue.inviter_code"></el-input>
        </el-form-item>
      </el-col> -->
      <el-col :span="24">
        <el-form-item :label="t('webSearch.companyWebsite')">
          <el-input v-model="modelValue.url"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.businessLicense')" prop="license">
          <image-upload
            v-model="modelValue.license"
            :isShowTip="false"
            :limit="1"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item label="营业执照:" 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('webContact.unit'),
            })
          "
          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 name="EnterpriseForm" setup>
import FieldOptions from "../FieldOptions";
import InputBoxAdd from "../InputBoxAdd";
// import { researchSelect, laboratorySelect } from "@/api/identity/index";
import { countryOptions, enterpriseOptions } from "@/utils/parameter";
import { computed, reactive, ref, toRefs } from "vue";
import ImageUpload from "@/components/ImageUpload/index.vue";
import { useI18n } from "vue-i18n";

const { t, locale } = useI18n();
const props = defineProps({
  modelValue: Object,
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});
const { modelValue, isAdd, showTitle, labelWidth } = toRefs(props);

const vNumber = {
  mounted: (el, _binding, vnode, _prevVnode) => {
    console.log(vnode);
    el.children[0].addEventListener("keypress", (e) => {
      // console.log(e);
      if (/\D/.test(e.key)) {
        e.preventDefault();
      }
    });
    // el.addEventListener("input", (e) => {
    //   console.log(e);
    //   e.srcElement.value = e.srcElement.value.replace("3", "");
    //   // value = value.replace("3", "");
    // });
  },
};
const data = reactive({
  rules: {
    product: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    name: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    username: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.contact"),
          })
        ),
        trigger: "blur",
      },
    ],
    phone: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("webSearch.contactInformation"),
          })
        ),
        trigger: "blur",
      },
      // {
      //   pattern: /^1[3456789]\d{9}$/,
      //   message: "手机号格式不正确",
      //   trigger: "blur",
      // },
    ],
    email: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.email"),
          })
        ),
        trigger: "blur",
      },
      {
        pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
        message: computed(() =>
          t("admin.validation.incorrectFormatOf", {
            type: t("admin.form.email"),
          })
        ),
        trigger: "blur",
      },
    ],
    kind: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    code: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    country: [
      {
        required: true,
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.country"),
          })
        ),
        trigger: "change",
      },
    ],
    mobile: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        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",
      },
    ],
    work_at: [
      {
        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 { rules } = toRefs(data);

const formRef = ref();
// const cityFormRef = ref();
const fieldFormRef = ref();
const keywordsFormRef = ref();
const directionsFormRef = ref();

/**
 * validate form COMPLETE
 * @return {Promise<false|*>}
 */
const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const fieldFormValid = await fieldFormRef.value.validateForm(); // 领域选择表单验证
  const keywordsFormValid = await keywordsFormRef.value.validateForm(); // 关键词表单验证
  if (isAdd.value) {
    const directionsFormValid = await directionsFormRef.value.validateForm();
    return (
      formValid && fieldFormValid && keywordsFormValid && directionsFormValid
    );
  } else {
    return formValid && fieldFormValid && keywordsFormValid;
  }
};
defineExpose({
  validateForm,
});
</script>

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

// 上传图片框限制
// ::v-deep .el-upload--picture-card {
//   width: 120px;
//   height: 120px;
//   line-height: 120px;
// }
.el-select,
.el-date-editor {
  display: block;
  width: 100%;
}
</style>
