<template>
  <div id="form-1">
    <van-row>
      <van-col span="24" class="main">
        <van-col span="24" class="one">
          <van-button type="info" size="small" @click="toSearch()">查询专利</van-button>
        </van-col>
        <van-col span="24" class="two">
          <van-form @submit="onSubmit">
            <van-field v-model="form.user_id" name="user_id" label="用户id" style="display: none" />
            <van-field v-model="form.user_name" name="user_name" label="用户姓名" style="display: none" />
            <van-field v-model="form.admin_id" name="admin_id" label="管理员id" style="display: none" />
            <van-field v-model="form.admin_name" name="admin_name" label="管理员姓名" style="display: none" />
            <van-field v-model="form.patent_id" name="patent_id" label="专利id" style="display: none" />
            <van-field v-model="form.create_number" name="create_number" label="申请号" readonly />
            <van-field v-model="form.patent_name" name="patent_name" label="专利名称" readonly />
            <van-field v-model="form.inventor" name="inventor" label="发明人" readonly />
            <van-field v-model="form.type" name="type" label="专利类型" readonly />
            <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.abstract" name="abstract" label="摘要" type="textarea" autosize readonly />
            <van-field
              v-model="form.field"
              name="field"
              label="应用领域"
              placeholder="请输入应用领域"
              type="textarea"
              autosize
              rows="1"
              :rules="[{ required: true, message: '请输入应用领域' }]"
            />
            <van-field
              v-model="form.explain"
              name="explain"
              label="技术说明"
              placeholder="请输入技术说明"
              type="textarea"
              autosize
              rows="1"
              :rules="[{ required: true, message: '请输入技术说明' }]"
            />
            <van-field v-model="form.shared_value" name="shared_value" label="合享价值度" readonly />
            <van-field v-model="form.techol_stable" name="techol_stable" label="技术稳定性" readonly />
            <van-field v-model="form.techol_advanced" name="techol_advanced" label="技术先进性" readonly />
            <van-col span="24" class="btn">
              <van-button type="info" size="small" native-type="submit">提交申请</van-button>
            </van-col>
          </van-form>
        </van-col>
      </van-col>
    </van-row>
    <van-dialog class="dialog" v-model="dialog.show" :title="dialog.title" :show-confirm-button="false" show-cancel-button cancel-button-text="返回">
      <search-1 v-if="dialog.type == '1'" @conPatent="conPatent"></search-1>
    </van-dialog>
  </div>
</template>

<script>
import search1 from '@/layout/assessment/search-1.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentassess } = createNamespacedHelpers('patentassess');
const { mapActions: patentinfo } = createNamespacedHelpers('patentinfo');
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'form-1',
  props: {
    patent_id: { type: String },
  },
  components: { search1 },
  data: function () {
    return {
      adminInfo: {},
      form: {},
      // 弹框
      dialog: { show: false, title: '查询专利', type: '1' },
    };
  },
  created() {
    this.searchOther();
  },
  methods: {
    ...adminLogin({ adminQuery: 'query', adminFetch: 'fetch' }),
    ...patentinfo({ patentQuery: 'query', patentFetch: 'fetch' }),
    ...patentassess(['fetch', 'create', 'update']),
    // 提交
    async onSubmit(values) {
      if (values) {
        let data = this.form;
        if (data.id) {
        } else {
          let res = await this.create(data);
          if (this.$checkRes(res)) {
            this.$toast({ type: `success`, message: `操作完成` });
            this.form = {};
          }
        }
      }
    },
    // 查询专利
    toSearch() {
      this.dialog = { show: true, title: '查询专利', type: '1' };
    },
    // 确认选择专利
    conPatent(value) {
      let data = {
        admin_id: this.adminInfo.id,
        admin_name: this.adminInfo.name,
        user_id: this.user.id,
        user_name: this.user.phone,
        patent_id: value.id,
        create_number: value.create_number,
        patent_name: value.name,
        inventor: value.inventor,
        type: value.type,
        contact: this.user.phone,
        phone: this.user.name,
        email: this.user.email,
        abstract: value.abstract,
        shared_value: value.shared_value,
        techol_stable: value.techol_stable,
        techol_advanced: value.techol_advanced,
      };
      this.$set(this, `form`, data);
      this.dialog = { show: false, title: '查询专利', type: '1' };
    },
    // 查询其他信息
    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);
      }
    },
    // 查询专利信息
    async searchPatent() {
      let res = await this.patentFetch(this.patent_id);
      if (this.$checkRes(res)) {
        this.searchOther();
        this.conPatent(res.data);
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    patent_id: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val) this.searchPatent();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    text-align: center;
    padding: 5px 0;
  }
  .two {
    .btn {
      text-align: center;
      margin: 10px 0;
    }
  }
}
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
}
</style>
