<template>
  <el-form
    ref="formRef"
    :model="modelValue"
    :rules="rules"
    :label-width="labelWidth + 'px'"
  >
    <div class="form_title" v-if="showTitle">基本信息</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" :disabled="!isAdd"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="" :disabled="!isAdd"
                >查找</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="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="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>

    <el-row>
      <el-col :span="24">
        <el-form-item label="所属站点:" prop="tenantId">
          <el-select
            v-model="modelValue.tenantId"
            placeholder="请选择，支持搜索"
            size="default"
            filterable
          >
            <el-option
              v-for="item in siteList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <!-- <SiteOptions :size="''" :limitWidth="false" v-model="value" /> -->
        </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="所属实验室:">
          <el-select
            v-model="modelValue.laboratory_id"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

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

    <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-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
              v-for="item in educationOptions"
              :key="item.text"
              :label="item.text"
              :value="item.text"
            >
            </el-option>
          </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="workAt">
          <el-date-picker
            v-model="modelValue.workAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </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 v-model="value" :labelWidth="labelWidth" ref="fieldForm" /> -->

    <InputBoxAdd
      :labelWidth="labelWidth"
      v-model="modelValue"
      title="关键词"
      placeholder="应用场景关键词+技术产品关键词"
      fieldKey="keywords"
      ref="keywordsFormRef"
    />

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

    <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"
            :disabled="!isAdd"
          ></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-if="formType == 1"
            v-model="modelValue.introduce"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
          />
          <WangEditor v-else 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 setup name="ExpertForm">
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 { enterpriseOptions } from "@/utils/parameter";
import { reactive, ref, toRefs } from "vue";
import WangEditor from "../../../components/WangEditor/index.vue";

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 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: [{ 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 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 getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};
const check = async () => {
  // console.log(cityFormRef.value);
  // console.log(cityFormRef.value.formRef.value.validate());
  // cityFormRef.value
  //   .validateForm()
  //   .then((re) => {
  //     console.log(re);
  //   })
  //   .catch((error) => {
  //     console.log(error);
  //   });
  // console.log(await keywordsFormRef.value.validateForm());
  const res = await validateForm();
  console.log(res);
};
const validate1 = async () => {
  try {
    return await formRef.value.validate();
  } catch (error) {
    return false;
  }
};
const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const cityFormValid = await cityFormRef.value.validateForm();

  const keywordsFormValid = await keywordsFormRef.value.validateForm();
  if (formType.value == 1) {
    const researchsFormValid = await researchsFormRef.value.validateForm();
    return (
      formValid && cityFormValid && keywordsFormValid && researchsFormValid
    );
  } else if (formType.value == 2) {
    const directionsFormValid = await directionsFormRef.value.validateForm();
    return (
      formValid && cityFormValid && keywordsFormValid && directionsFormValid
    );
  } else {
    throw "未知的formType";
  }
};
defineExpose({
  validateForm,
});
// const submitForm = () => {
//   let flag = false;
//   if (this.formType == 1) {
//     this.$refs["form"].validate((valid) => {
//       const cityForm = this.$refs.cityForm.submitForm(); // 城市
//       const fieldForm = this.$refs.fieldForm.submitForm(); // 所属领域
//       const keywordsForm = this.$refs.keywordsForm.submitForm(); // 关键词
//       const researchsForm = this.$refs.researchsForm.submitForm(); // 研究方向
//       if (valid && cityForm && fieldForm && keywordsForm && researchsForm) {
//         flag = !flag;
//       }
//     });
//   } else if (this.formType == 2) {
//     this.$refs["form"].validate((valid) => {
//       const cityForm = this.$refs.cityForm.submitForm();
//       const fieldForm = this.$refs.fieldForm.submitForm();
//       const keywordsForm = this.$refs.keywordsForm.submitForm();
//       const directionsForm = this.$refs.directionsForm.submitForm();
//       if (valid && cityForm && fieldForm && keywordsForm && directionsForm) {
//         flag = !flag;
//       }
//     });
//   }
//   return flag;
// };
getSiteList();
</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;
}
// 上传图片框限制
// ::v-deep .el-upload--picture-card {
//   width: 120px;
//   height: 120px;
//   line-height: 120px;
// }
// .el-select,
// .el-date-editor {
//   display: block;
//   width: 100%;
// }
</style>
