<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="imageName" prop="image">
          <ImageUpload v-model="modelValue.image" :fileSize="2" :limit="1"/>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 2">
      <el-col :span="24">
        <el-form-item :label="nameName" prop="name">
          <el-input
              v-model="modelValue.name"
              :disabled="isAdd ? false : true"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!-- <el-row v-if="formType == 2">
      <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 v-if="formType == 2">
      <el-col :span="24">
        <el-form-item label="联系人" prop="username">
          <el-input v-model="modelValue.username"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row v-if="formType == 2">
      <el-col :span="24">
        <el-form-item label="联系方式" prop="phone">
          <el-input
              v-model="modelValue.phone"
          ></el-input>
          <!-- v-number -->
        </el-form-item>
      </el-col>
    </el-row>





    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="姓名:" prop="name">
          <el-input
              v-model="modelValue.name"
              :disabled="isAdd ? false : true"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号:" prop="mobile">
          <el-input
              v-model="modelValue.mobile"
              :disabled="isAdd ? false : true"
          ></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="tenantId">
          <SiteOptions v-model="modelValue.tenantId" :site-list="siteList" />
        </el-form-item>
      </el-col>
    </el-row> -->

    <el-row v-if="formType == 2">
      <el-col :span="24">
        <el-form-item label="企业类型:" prop="kind">
          <el-select v-model="modelValue.kind" placeholder="请选择">
            <el-option
                v-for="item in enterpriseOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="核心成果核心产品:" prop="product">
          <el-input v-model="modelValue.product"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <!--      <el-col :span="12">-->
      <!--        <el-form-item label="所属单位:" prop="researchId">-->
      <!--          <el-select-->
      <!--            v-model="modelValue.researchId"-->
      <!--            filterable-->
      <!--            placeholder="请选择"-->
      <!--            @change="setLaboratory"-->
      <!--          >-->
      <!--            <el-option-->
      <!--              v-for="item in researchOptions"-->
      <!--              :key="item.id"-->
      <!--              :label="item.name"-->
      <!--              :value="item.id"-->
      <!--            >-->
      <!--            </el-option>-->
      <!--          </el-select>-->
      <!--        </el-form-item>-->
      <!--      </el-col>-->
      <el-col :span="12">
        <el-form-item label="所属实验室:">
          <infinite-select
              v-model="modelValue.laboratoryId"
              :prop="{ label: 'name', value: 'id' }"
              :query="{ page: 'pageNum', size: 'pageSize', searchKey: 'name' }"
              :remote-method="loadLabOptions"
              :default-label="modelValue.laboratoryName"
              class="infinite-select"
              prefix-icon="pointer"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="所属研究机构:">
          <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-->
    <!--        ref="cityFormRef"-->
    <!--        v-model="modelValue"-->
    <!--        :labelWidth="labelWidth"-->
    <!--    />-->

    <el-form-item
        label="国家"
        prop="country"
    >
      <el-select
          v-model="modelValue.country"
          placeholder="请选择国家"
      >
        <el-option
            v-for="option in countryOptions"
            :label="option.value"
            :value="option.key"
        />
      </el-select>
    </el-form-item>

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

    <el-row v-if="formType == 1">
      <el-col :span="24">
        <el-form-item label="毕业院校:" prop="school">
          <el-input v-model="modelValue.school"></el-input>
        </el-form-item>
      </el-col>
      <!--      <el-col :span="12">-->
      <!--        <el-form-item label="所属研究机构:" prop="organization">-->
      <!--          <el-input v-model="modelValue.organization"></el-input>-->
      <!--        </el-form-item>-->
      <!--      </el-col>-->
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="最高学历:" prop="education">
          <el-select v-model="modelValue.education" placeholder="请选择">
            <!--            <el-option label="博士" value="博士"></el-option>-->
            <!--            <el-option label="硕士" value="硕士"></el-option>-->
            <!--            <el-option label="本科" value="本科"></el-option>-->
            <!--            <el-option label="专科" value="专科"></el-option>-->
            <el-option v-for="option in educationOptions" :key="option.key" :value="option.key" :label="option.text"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="主修专业:" prop="major">
          <el-input v-model="modelValue.major"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="职务:" prop="job">
          <el-input v-model="modelValue.job"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="职称:" prop="title">
          <el-input v-model="modelValue.title"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="从业时间:" prop="workTime">
          <el-date-picker
              v-model="modelValue.workTime"
              format="YYYY-MM"
              placeholder="选择日期"
              type="month"
              value-format="YYYY-MM"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="性别:" prop="gender">
          <el-radio v-model="modelValue.gender" label="1">男</el-radio>
          <el-radio v-model="modelValue.gender" label="2">女</el-radio>
        </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
        v-if="formType != 2"
        ref="researchsFormRef"
        v-model="modelValue"
        :labelWidth="labelWidth"
        fieldKey="researchs"
        placeholder="请输入研究方向"
        title="研究方向"
    />

    <!-- <InputBoxAdd
      v-if="formType == 2"
      :labelWidth="labelWidth"
      v-model="modelValue"
      title="生产方向"
      
      placeholder="请输入生产方向"
      fieldKey="directions"
      ref="directionsFormRef"
    /> -->

    <el-row v-if="formType == 2">
      <!-- <el-col :span="24">
        <el-form-item label="邀请码:">
          <el-input
            v-model="modelValue.inviterCode"
            
          ></el-input>
        </el-form-item>
      </el-col> -->
      <el-col :span="24">
        <el-form-item label="企业网站:">
          <el-input v-model="modelValue.url"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 2">
      <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="introduceName" prop="introduce">
           <el-input
            v-model="modelValue.introduce"
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
          />
          <!-- v-else -->
<!--          <WangEditor v-model="modelValue.introduce" minHeight="150px"/>-->
          <!-- <Editor
            v-else
            v-model="modelValue.introduce"
            :minHeight="150"
            ref="introduceRef"
            @click.native="handleEditAble"
          /> -->
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script name="ExpertForm" setup>
import CityOptions from "@/views/components/CityOptions";
// import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
// import { researchSelect } from "@/api/dataList/research";
// import { laboratorySelect } from "@/api/dataList/laboratory";
import {tenantSelect} from "@/api/subPlatform/tenant";
import {countryOptions, educationOptions, enterpriseOptions} from "@/utils/parameter";
import {reactive, ref, toRefs} from "vue";
import WangEditor from "@/components/WangEditor/index.vue";
// import SiteOptions from "@/views/components/SiteOptions";
import FieldOptions from "@/views/components/FieldOptions";
import {casLaboratoryList} from "@/api/dataList/laboratory";
import InfiniteSelect from "@/components/InfiniteSelect";
import {researchList} from "@/api/dataList/research";

const props = defineProps({
  modelValue: {type: Object},
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  formType: {
    type: Number,
    default: 1,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});
const {formType, modelValue, labelWidth, showTitle, isAdd} = toRefs(props);
// const cityFormRef = ref(null);
const formRef = ref(null);
const keywordsFormRef = ref(null);
const researchsFormRef = ref(null);
const fieldFormRef = ref(null);
const directionsFormRef = ref([]);
const researchOptions = ref([]);
const options = ref([]);
const siteList = 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"}],
    // mobile: [
    //   { required: true, message: "请输入", trigger: "blur" },
    //   {
    //     pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
    //     message: "请输入正确的手机号码",
    //     trigger: "blur",
    //   },
    // ],
    researchId: [{message: "请选择", trigger: "change"}],
    tenantId: [
      {
        required: true,
        message: "请选择",
        trigger: ["blur", "change"],
      },
    ],
    school: [{required: true, message: "请输入", trigger: "blur"}],
    organization: [{required: true, message: "请输入", trigger: "blur"}],
    education: [{message: "请选择", trigger: "change", required: true}],
    // major: [{ required: true, message: "请输入", trigger: "blur" }],
    // job: [{ required: true, message: "请输入", trigger: "blur" }],
    title: [{required: true, message: "请输入", trigger: "blur"}],
    workTime: [
      {
        required: true,
        message: "从业时间不能为空",
        trigger: ["change", "blur"],
      },
    ],
    license: [
      {
        required: true,
        message: "请上传",
        trigger: ["blur", "change"],
      },
    ],
    introduce: [{required: true, message: "请输入", trigger: "blur"}],
  },
});
const {rules} = toRefs(data);
const introduceName = computed(() => {
  if (formType.value === 1) {
    return "个人简介:";
  } else if (formType.value === 2) {
    return "单位简介:";
  } else {
    return "";
  }
});

const imageName = computed(() => {
  if (formType.value === 1) {
    return "专家头像:";
  } else if (formType.value === 2) {
    return "企业logo:";
  } else {
    return "";
  }
});

const nameName = computed(() => {
  if (formType.value === 2) {
    return "单位名称:";
  } else {
    return "";
  }
});

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

// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};

const loadLabOptions = (query) =>
    casLaboratoryList({...query, examineStatus: "1"}).then((resp) => resp.rows);
const loadResearchOptions = (query) =>
    researchList({...query, examineStatus: "1"}).then((resp) => resp.rows);
getSiteList();
defineExpose({
  validateForm,
});
</script>

<!-- <script>
export default {
  data() {
    return {};
  },
  watch: {
    "modelmodelValue.tenantId"(newOld) {
      console.log(newOld);
      if (newOld) {
        if (this.formType == 1) {
          // this.formType == 1 专家表单
          // 获取所属单位-研究机构options
          researchSelect({ tenantId: newOld }).then(({ data }) => {
            this.researchOptions = data;
          });
        }
      } else {
        this.researchOptions = [];
        if (this.modelmodelValue.researchId) this.modelmodelValue.researchId = undefined;
      }
    },
  },
  mounted() {
    console.log(this.formType);
    if (this.formType != 1) {
      // 取消富文本自动聚焦
      this.$nextTick(() => {
        this.$refs.introduceRef.Quill.enable(false);
      });
    }
  },
  methods: {
    // 富文本聚焦
    handleEditAble() {
      this.$refs.introduceRef.Quill.enable(true);
      this.$refs.introduceRef.Quill.focus();
    },
    setLaboratory(e) {
      laboratorySelect({ researchId: e }).then((res) => {
        console.log(res);
      });
    },
  },
};
</script> -->
<style lang="scss" scoped>
.form_title {
  font-weight: 700;
  margin-bottom: 30px;
}
</style>
