<template>
  <el-form
    ref="formRef"
    :model="modelValue"
    :rules="rules"
    :label-width="labelWidth + 'px'"
  >
    <div class="form_title">
      <p>
        <b> {{ t("admin.form.basicInfo") }}</b>
      </p>
    </div>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.name', { type: t('admin.form.product') })"
          prop="title"
        >
          <el-input
            v-model="modelValue.title"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.form.name', { type: t('admin.form.product') }),
              })
            "
          ></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"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.contact') })
            "
          ></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"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.contactInformation'),
              })
            "
          ></el-input>
        </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>
    <!--    其他联系方式-->
<!--  TODO:  -->
<!--    <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>-->

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

    <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>

    <input-box-add
      :labelWidth="labelWidth"
      v-model="modelValue"
      :placeholder="
        t('admin.form.placeholder', {
          type: t('webSearch.applicationCustomers'),
        })
      "
      :title="t('webSearch.applicationCustomers')"
      fieldKey="customers"
      ref="customerFormRef"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.productMaturity')" prop="maturity">
          <el-select
            v-model="modelValue.maturity"
            clearable
            :placeholder="t('admin.form.pleaseSelect')"
          >
            <el-option
              v-for="item in maturityOptions"
              :key="item.key"
              :label="item[locale]"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.productLeadership')"
          prop="leadStandard"
        >
          <el-select
            v-model="modelValue.leadStandard"
            clearable
            :placeholder="t('admin.form.pleaseSelect')"
          >
            <el-option
              v-for="item in leadOptions"
              :key="item.key"
              :label="item[locale]"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('webSearch.cooperationMode')">
          <el-select
            v-model="modelValue.cooperationModeArr"
            clearable
            multiple
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.cooperationMode'),
              })
            "
          >
            <el-option
              v-for="item in cooperationOptions"
              :key="item.key"
              :label="item[locale]"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

    <input-box-add
      :labelWidth="labelWidth"
      v-model="modelValue"
      :title="t('admin.form.keywords')"
      :placeholder="
        t('admin.form.placeholder', {
          type: t('admin.form.keywords'),
        })
      "
      fieldKey="keywords"
      ref="keywordsForm"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="
            t('admin.form.introduction', { type: t('admin.form.product') })
          "
          prop="introduce"
        >
          <el-input
            type="textarea"
            v-model="modelValue.introduce"
            :autosize="{ minRows: 6, maxRows: 8 }"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.productPicture')" prop="image">
          <ImageUpload v-model="modelValue.image" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>
    <p>
      <b>{{ t("admin.form.pictureMaterialUpload") }}</b>
    </p>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.proofMaterial')" prop="material">
          <FileUpload
            v-model="modelValue.material"
            :limit="1"
            :fileType="['doc', 'xls', 'pdf', 'jpg', 'png', 'zip']"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import {
  cooperationOptions,
  countryOptions,
  leadOptions,
  maturityOptions,
} from "@/utils/parameter";
import { computed, reactive, toRefs } from "vue"; // import VideoUpload from "@/components/VideoUpload";
// import { researchSelect, laboratorySelect } from "@/api/identity/index";
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 data = reactive({
  rules: {
    // product: [{ required: true, message: computed(()=>t("admin.form.placeholder")), trigger: "blur" }],
    title: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    kind: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    username: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("webSearch.contactInformation"),
          })
        ),
        trigger: "blur",
      },
    ],
    phone: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("webSearch.contactInformation"),
          })
        ),
        trigger: "blur",
      },
      // {
      //   pattern: /^1[3456789]\d{9}$/,
      //   message: computed(() =>
      //     t("admin.validation.incorrectFormatOf", {
      //       type: t("admin.form.mobile"),
      //     })
      //   ),
      //   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",
      },
    ],
    // code: [{ required: true, message: computed(()=>t("admin.form.placeholder")), trigger: "blur" }],
    // 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" }],
    maturity: [
      {
        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" }],
    work_at: [
      {
        required: true,
        message: computed(() =>
          t("admin.validation.required", { type: t("admin.form.workingTime") })
        ),
        trigger: ["change", "blur"],
      },
    ],
    leadStandard: [
      {
        required: true,
        message: computed(() => t("admin.validation.pleaseUpload")),
        trigger: ["blur", "change"],
      },
    ],
    introduce: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: ["change", "blur"],
      },
    ],
    image: [
      {
        required: true,
        message: computed(() => t("admin.validation.pleaseUpload")),
        trigger: ["change", "blur"],
      },
    ],
    video: [
      {
        required: true,
        message: computed(() => t("admin.validation.pleaseUpload")),
        trigger: ["change", "blur"],
      },
    ],
  },
});
const { rules } = toRefs(data);
const formRef = ref(null);
const fieldFormRef = ref(null);
const customerFormRef = ref(null);
// const cityFormRef = ref(null);
const validateForm = async () => {
  // await formRef.value.validate();
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const fieldFormValid = await fieldFormRef.value.validateForm(); // 城市选择表单验证
  const customerValid = await customerFormRef.value.validateForm(); // 领域选择表单验证
  // const cityFormValid = await cityFormRef.value.validateForm(); // 领域选择表单验证

  console.log(formValid, fieldFormValid, customerValid);
  return formValid && fieldFormValid && customerValid;
  // const keywordsFormValid = await keywordsFormRef.value.validateForm(); // 关键词表单验证
  // console.log(cityFormValid);
  // if (isAdd.value) {
  //   const directionsFormValid = await directionsFormRef.value.validateForm();
  //   return (
  //     formValid &&
  //     cityFormValid &&
  //     fieldFormValid &&
  //     keywordsFormValid &&
  //     directionsFormValid
  //   );
  // } else {
  //   return formValid && cityFormValid && fieldFormValid && keywordsFormValid;
  // }
};
defineExpose({
  validateForm,
});
// const submitForm = () => {
//   let flag = false;
//   this.$refs["formRef"].validate((valid) => {
//     const fieldForm = this.$refs.fieldForm.submitForm();
//     const customerForm = this.$refs.customerForm.submitForm();
//     const keywordsForm = this.$refs.keywordsForm.submitForm();
//     if (valid && fieldForm && customerForm && keywordsForm) {
//       flag = !flag;
//     }
//   });
//   return flag;
// };
</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>
