<template>
  <!--COMPLETE-->
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <div v-if="showTitle" class="form_title">基本信息</div>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item label="实验室名称" 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="所属院所或单位" prop="school">
          <el-input
            v-model="modelValue.school"
            placeholder="请输入所属院所或单位"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <!--FIXME:-->
    <el-row v-if="isAdd && false">
      <el-col :span="24">
        <el-form-item label="组织机构代码:" 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-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="关键词"
    />

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

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

    <el-row>
      <el-col :span="24">
        <el-form-item label="实验室简介" prop="introduction">
          <editor
            ref="introduceRef"
            v-model="modelValue.introduction"
            :minHeight="150"
          />
        </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 Editor from "@/components/WangEditor";
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { reactive, ref, toRefs } from "vue";

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

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

const data = reactive({
  rules: {
    name: [{ required: true, message: "请输入实验室名称", trigger: "blur" }],
    code: [{ required: true, message: "请输入组织机构代码", trigger: "blur" }],
    school: [
      { required: true, message: "请输入所属院所或单位", trigger: "blur" },
    ],
    introduction: [
      {
        required: true,
        message: "请输入实验室简介",
        trigger: ["change", "blur"],
      },
    ],
    /*-----*/
    kind: [{ required: true, message: "请选择", trigger: "change" }],
    product: [{ required: true, message: "请输入", trigger: "blur" }],
    mobile: [
      { 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"],
      },
    ],
    education: [{ required: true, message: "请选择", trigger: "change" }],
    major: [{ required: true, message: "请输入", trigger: "blur" }],
    job: [{ required: true, message: "请输入", trigger: "blur" }],
    title: [{ required: true, message: "请输入", trigger: "blur" }],
    work_at: [
      {
        required: true,
        message: "从业时间不能为空",
        trigger: ["change", "blur"],
      },
    ],
    license: [
      {
        required: true,
        message: "请上传",
        trigger: ["blur", "change"],
      },
    ],
  },
});
const { rules } = toRefs(data);

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 directionsFormValid = await directionsFormRef.value.validateForm(); // 研究方向表单验证
  return (
    formValid &&
    // cityFormValid &&
    fieldFormValid &&
    keywordsFormValid &&
    directionsFormValid
  );
};

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>
