<template>
  <div id="form-1">
    <van-row>
      <van-col span="24" class="main">
        <van-col span="24" class="one">
          <van-form @submit="onSubmit">
            <van-field v-model="form.name" name="name" label="发明名称" placeholder="发明名称" :rules="[{ required: true, message: '发明名称' }]" />
            <van-field v-model="form.apply_name" name="apply_name" label="申请人" placeholder="申请人" :rules="[{ required: true, message: '申请人' }]" />
            <van-field name="type" label="专利类型" :rules="[{ required: true, message: '请填写专利类型' }]">
              <template #input>
                <van-radio-group v-model="form.type" direction="horizontal">
                  <van-radio name="发明申请">发明申请</van-radio>
                  <van-radio name="实用新型">实用新型</van-radio>
                </van-radio-group>
              </template>
            </van-field>
            <van-field v-model="form.inventor" name="inventor" label="发明人" placeholder="发明人" :rules="[{ required: true, message: '发明人' }]" />
            <van-field v-model="form.contact" name="contact" label="技术联系人" placeholder="技术联系人" :rules="[{ required: true, message: '技术联系人' }]" />
            <van-field v-model="form.phone" name="phone" label="联系人电话" placeholder="联系人电话" :rules="[{ required: true, message: '联系人电话' }]" />
            <van-field v-model="form.email" name="email" label="联系人邮箱" placeholder="联系人邮箱" :rules="[{ required: true, message: '联系人邮箱' }]" />
            <van-field v-model="form.questions.q1" type="textarea" autosize name="特殊情况说明" label="特殊情况说明" placeholder="特殊情况说明" />
            <van-field v-model="form.questions.q2" type="textarea" autosize label="本发明的技术领域" placeholder="本发明的技术领域" />
            <van-field v-model="form.questions.q3" type="textarea" autosize label="与本发明相关的背景技术" placeholder="与本发明相关的背景技术" />
            <van-field v-model="form.questions.q4" type="textarea" autosize label="现有技术的缺点及本发明所要解决的技术问题" />
            <van-field v-model="form.questions.q5" type="textarea" autosize label="本发明技术方案的详细阐述" />
            <van-field v-model="form.questions.q6" type="textarea" autosize label="本申请的关键点和欲保护点" />
            <van-field v-model="form.questions.q7" type="textarea" autosize label="与本发明最相似,相近的实现技术,方案相比,本发明有何优点" />
            <van-field v-model="form.questions.q8" type="textarea" autosize label="针对本发明技术方案,是否还有别的替代方案" />
            <van-col span="24" style="text-align: center; margin: 10px 0">
              <van-button type="info" size="small" native-type="submit">提交申请</van-button>
            </van-col>
          </van-form>
        </van-col>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
const { mapActions: patentanalysis } = createNamespacedHelpers('patentanalysis');
export default {
  name: 'form-1',
  props: {},
  components: {},
  data: function () {
    return {
      // 管理员信息
      adminInfo: {},
      form: { questions: {} },
    };
  },
  created() {
    this.searchOther();
  },
  methods: {
    ...adminLogin({ adminQuery: 'query', adminFetch: 'fetch' }),
    ...patentanalysis(['query', 'fetch', 'create', 'update']),
    async onSubmit(values) {
      if (values) {
        let data = this.form;
        if (data.id) {
          console.log(data);
        } else {
          let res = await this.create(data);
          if (this.$checkRes(res)) {
            this.$toast({ type: `success`, message: `操作成功` });
            this.searchOther();
          }
        }
      }
    },
    async searchOther() {
      let res = await this.adminQuery({ code: this.user.code });
      if (this.$checkRes(res)) {
        let arr = await this.adminFetch(res.data[0].pid);
        if (this.$checkRes(arr)) {
          this.$set(this, `adminInfo`, arr.data);
          let data = {
            user_id: this.user._id,
            user_name: this.user.phone,
            admin_id: this.adminInfo.id,
            admin_name: this.adminInfo.name,
            apply_name: this.user.phone,
            inventor: this.user.phone,
            contact: this.user.phone,
            phone: this.user.name,
            email: this.user.email,
            questions: {},
            status: '0',
          };
          this.$set(this, `form`, data);
        }
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped></style>
