<template>
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <!-- <el-row class="mb20">
      <el-radio-group v-model="modelValue.mode">
        <el-radio-button :label="1">{{
          `免费成果${modelValue.id ? "修改" : "发布"}`
        }}</el-radio-button>
        <el-radio-button :label="2">{{
          `付费成果${modelValue.id ? "修改" : "发布"}`
        }}</el-radio-button>
      </el-radio-group>
    </el-row> -->

    <div v-if="showTitle" class="form_title">
      {{ t("admin.form.basicInfo") }}
    </div>

    <el-alert
      v-if="modelValue.mode == 2"
      :closable="false"
      show-icon
      style="margin-bottom: 20px"
      title="1创新币=1元"
      type="warning"
    ></el-alert>

    <el-row v-if="modelValue.mode == 2">
      <el-col :span="12">
        <el-form-item label="视频定价:">
          <el-input v-model="modelValue.price" placeholder="请输入创新币个数">
            <template #append>{{ t("admin.table.innovationCoin") }}</template>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="免费观看时长:">
          <el-input
            v-model="modelValue.duration"
            placeholder="默认视频的1/3时长"
          >
            <template #append>min</template>
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="modelValue.mode == 2">
      <el-col :span="24">
        <el-form-item label="视频文件:">
          <FileUpload
            v-model="modelValue.image"
            :fileType="['mp4']"
            :limit="1"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.name', { type: t('admin.common.achievement') })"
          prop="title"
        >
          <el-input
            v-model="modelValue.title"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.common.achievement'),
              })
            "
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

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

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

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

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.achievementMaturity')"
          prop="maturity"
        >
          <el-select
            v-model="modelValue.maturity"
            clearable
            :placeholder="
              t('admin.form.pleaseSelect', {
                type: t('admin.form.achievementMaturity'),
              })
            "
          >
            <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('webSearch.achievementLeadership')"
          prop="leadStandard"
        >
          <el-select
            v-model="modelValue.leadStandard"
            clearable
            :placeholder="
              t('admin.form.pleaseSelect', {
                type: t('webSearch.achievementLeadership'),
              })
            "
          >
            <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.cooperationMode"
            clearable
            :placeholder="
              t('admin.form.pleaseSelect', {
                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>

    <InputBoxAdd
      v-if="modelValue.mode == 2"
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="kws"
      :placeholder="
        t('admin.form.placeholder', {
          type: t('admin.table.applicationScenario'),
        })
      "
      :title="t('admin.table.applicationScenario')"
    />

    <InputBoxAdd
      ref="keywordsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      :title="t('admin.form.keywords')"
      fieldKey="keywords"
      :placeholder="
        t('admin.form.placeholder', {
          type: t('admin.form.keywords'),
        })
      "
    />
    <!-- //TODO: -->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('webSearch.unit')" prop="unit">
          <el-input
            v-model="modelValue.unit"
            :placeholder="
              t('admin.form.placeholder', { type: t('webSearch.unit') })
            "
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('webSearch.achievementIntroduction')"
          prop="description"
        >
          <el-input
            v-model="modelValue.description"
            :autosize="{ minRows: 6, maxRows: 8 }"
            type="textarea"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-row>
      <el-col :span="24">
        <el-form-item label="成果来源:">
          <wangEditor
            v-model="modelValue.comeFrom"
            width="100%"
            min-height="150px"
          ></wangEditor>
        </el-form-item>
      </el-col>
    </el-row> -->
    <el-row v-if="modelValue.mode == 1">
      <el-col :span="24">
        <el-form-item :label="t('admin.form.achievementPicture')" prop="image">
          <ImageUpload
            v-model="modelValue.image"
            :isShowTip="false"
            :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.maturityProofMaterial')">
          <FileUpload
            v-model="modelValue.file"
            :fileType="['doc', 'xls', 'ppt', 'txt', 'pdf', 'jpg']"
            :limit="1"
          />
        </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, ref, toRefs } from "vue";
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: {
    title: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.name", {
              type: t("admin.common.achievement"),
            }),
          })
        ),
        trigger: "blur",
      },
    ],
    maturity: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.pleaseSelect", {
            type: t("admin.form.maturity"),
          })
        ),
        trigger: "change",
      },
    ],
    country: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", { type: t("admin.form.country") })
        ),
        trigger: "blur",
      },
    ],
    leadStandard: [
      {
        required: true,
        /*"请选择领先情况"*/
        message: computed(() =>
          t("admin.form.pleaseSelect", {
            type: t("admin.form.leadership"),
          })
        ),
        trigger: "change",
      },
    ],
    description: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.introduction", {
              type: t("admin.common.achievement"),
            }),
          })
        ),
        trigger: "blur",
      },
    ],
    image: [
      {
        required: true,
        // message: "请上传成果图片",
        message: computed(() => t("tips.pleaseUploadFile")),
        trigger: ["change", "blur"],
      },
    ],
    unit: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", { type: t("webSearch.unit") })
        ),
        trigger: "blur",
      },
    ],
  },
});
const { rules } = toRefs(data);
// import { researchSelect, laboratorySelect } from "@/api/identity/index";
const formRef = ref();
const fieldFormRef = ref();
const customersFormRef = ref();
const directionsFormRef = ref();
const cityFormRef = ref();
const keywordsFormRef = ref();
const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const fieldFormValid = await fieldFormRef.value.validateForm(); // 城市选择表单验证
  const customersValid = await customersFormRef.value.validateForm(); // 领域选择表单验证
  // const directionsFormRef = await customersFormRef.value.validateForm(); // 领域选择表单验证

  // const cityFormValid = await cityFormRef.value.validateForm();
  const keywordsFormValid = await keywordsFormRef.value.validateForm(); // 关键词
  return (
    formValid &&
    fieldFormValid &&
    customersValid &&
    // cityFormValid &&
    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>
