<template>
  <el-form
      ref="formRef"
      :label-width="labelWidth + 'px'"
      :model="modelValue"
      :rules="rules"
  >
    <div v-if="showTitle" class="form_title">基本信息</div>
    <el-row>
      <el-col :span="24">
        <el-form-item label="实验室名称" prop="name">
          <el-input
              v-model="modelValue.name"
              :disabled="!isAdd"
              placeholder="请输入实验室全称"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="所属单院所或高校:" prop="school">
          <el-input
              v-model="modelValue.school"
              placeholder="请输入所属单院所或高校"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="所属科研机构:" prop="researchId">
          <infinite-select
              v-model="modelValue.researchId"
              :default-label="modelValue.researchName"
              :prop="{ label: 'name', value: 'id' }"
              :query="{ page: 'pageNum', size: 'pageSize', searchKey: 'name' }"
              :remote-method="loadResearchOptions"
              class="infinite-select"
              prefix-icon="pointer"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <!-- <CityOptions v-model="modelValue" :labelWidth="labelWidth" ref="cityForm" /> -->

    <!-- <el-row>
      <el-col :span="24">
        <el-form-item label="详细地址:">
          <el-input
            v-model="modelValue.address"
            placeholder="请输入详细地址"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row> -->

    <el-row>
      <el-col :span="24">
        <el-form-item label="实验室网站:">
          <el-input
              v-model="modelValue.url"
              placeholder="请输入实验室网站"
          ></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="应用场景关键词+技术产品关键词"
        title="关键词"
    />

    <InputBoxAdd
        ref="directionsFormRef"
        v-model="modelValue"
        :labelWidth="labelWidth"
        fieldKey="researchs"
        placeholder="请输入研究方向"
        title="研究方向"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item label="主要技术应用场景" prop="primaryTechnology">
          <el-input
              v-model="modelValue.primaryTechnology"
              :autosize="{ minRows: 4 }"
              placeholder="请输入主要技术应用场景"
              type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="承担科研项目情况" prop="researchProject">
          <el-input
              v-model="modelValue.researchProject"
              :autosize="{ minRows: 4 }"
              placeholder="请输入承担科研项目情况"
              type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="代表性科技成果" prop="achievement">
          <el-input
              v-model="modelValue.achievement"
              :autosize="{ minRows: 4 }"
              placeholder="请输入代表性科技成果"
              type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="联系电话" prop="phone">
          <el-input
              v-model="modelValue.phone"
              :maxlength="11"
              oninput="value = value.replace(/\D+/, '')"
              placeholder="请输入联系电话"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!--    ADDEMAIL-->
    <!--    邮箱-->
    <el-row>
      <el-col :span="24">
        <el-form-item label="邮箱" prop="email">
          <el-input placeholder="请输入邮箱" v-model="modelValue.email"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!--    其他联系方式-->
    <el-row>
      <el-col :span="24">
        <el-form-item label="其他联系方式" 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="url">
          <el-input
            placeholder="请输入网址"
            v-model="modelValue.url"
          ></el-input> </el-form-item></el-col
    ></el-row> -->
    <el-row>
      <el-col :span="24">
        <el-form-item label="实验室简介" prop="introduction">
          <WangEditor
              ref="introductionRef"
              v-model="modelValue.introduction"
              minHeight="320px"
              @blur="formRef.validateField(`introduction`)"
          />
          <!-- @click.native="handleEditAble" -->
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="核心成员及简介" prop="member">
          <WangEditor
              ref="memberRef"
              v-model="modelValue.member"
              minHeight="320px"
              @blur="formRef.validateField(`member`)"
          />
          <!-- @click.native="handleEditAble" -->
        </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 {reactive, ref, toRefs} from "vue";
import InfiniteSelect from "@/components/InfiniteSelect/index.vue";
import {researchList} from "@/api/dataList/research";
import {scrollToInvalidField} from "@/utils/validate";

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 options = ref([]);
const data = reactive({
  rules: {
    product: [{required: true, message: "请输入", trigger: "blur"}],
    name: [{required: true, message: "请输入", trigger: "blur"}],
    kind: [{required: true, message: "请选择", trigger: "change"}],
    code: [{required: true, message: "请输入", trigger: "blur"}],
    phone: [
      // { required: true, message: "请输入联系电话", trigger: "blur" },
      // {
      //   pattern: /^1[3-9]\d{9}$/,
      //   message: "请输入正确的手机号码",
      //   trigger: "blur",
      // },
    ],
    researchId: [{required: true, message: "请选择", trigger: "change"}],
    tenantId: [
      {
        required: true,
        message: "请选择",
        trigger: ["blur", "change"],
      },
    ],
    school: [{required: true, message: "请输入", trigger: "blur"}],
    education: [{required: true, message: "请选择", trigger: "change"}],
    major: [{required: true, message: "请输入", trigger: "blur"}],
    job: [{required: true, message: "请输入", trigger: "blur"}],
    title: [{required: true, message: "请输入", trigger: "blur"}],
    workAt: [
      {
        required: true,
        message: "从业时间不能为空",
        trigger: ["change", "blur"],
      },
    ],
    license: [
      {
        required: true,
        message: "请上传",
        trigger: ["blur", "change"],
      },
    ],
    introduce: [
      {required: true, message: "请输入实验室简介", trigger: "blur"},
    ],
  },
});

const {rules} = toRefs(data);

const formRef = ref();
const fieldFormRef = ref();
const keywordsFormRef = ref();
// watch(modelValue.tenantId, (val) => {
//   console.log(val);
//   if (val) {
//     researchSelect({ tenantId: val }).then(({ data }) => {
//       researchOptions.value = data;
//     });
//   } else {
//     researchOptions.value = [];
//     if (modelValue.researchId) modelValue.researchId = undefined;
//   }
// });

const loadResearchOptions = (query) =>
    researchList({...query, examineStatus: "1"}).then((resp) => resp.rows);
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 keywordsFormValid = await keywordsFormRef.value.validateForm();

  const validResult = formValid && fieldFormValid && keywordsFormValid;
  if (!validResult) {
    scrollToInvalidField()
  }
  return validResult
  // if (formType.value == 1) {
  //   //个人
  //   // const researchsFormValid = await researchsFormRef.value.validateForm(); //研究方向验证
  // } else if (formType.value == 2) {
  //   //企业
  //   // const directionsFormValid = await directionsFormRef.value.validateForm(); //生产方向验证
  //   return (
  //     formValid && fieldFormValid && fieldFormValid
  //     // keywordsFormValid &&
  //     // directionsFormValid
  //   );
  // } else {
  //   throw "未知的formType";
  // }
};
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>
