<template>
  <div id="hairmess">
    <admin-frame topType="2" @back="back" :rightArrow="false" :usePage="false" :useNav="false">
      <template v-slot:info>
        <van-form>
          <van-field name="file_url" label="文件">
            <template #input>
              <van-uploader
                :fileList="form.file_url"
                :max-count="1"
                :after-read="(file) => toUpload(file, 'file_url')"
                @delete="(file) => toDelete(file, 'file_url')"
                accept="file"
              />
            </template>
          </van-field>
          <van-field v-model="form.create_number" center clearable label="专利申请号" placeholder="请输入专利申请号">
            <template #button>
              <van-button size="small" type="primary" @click="searchPatent">查询</van-button>
            </template>
          </van-field>
          <van-field v-model="form.patent_name" name="专利名称" label="专利名称" readonly />
          <van-field v-model="form.to_name" name="接收人" label="接收人" readonly />
          <van-col span="24" class="btn">
            <van-button type="danger" size="small" @click="back">取消发送</van-button>
            <van-button type="info" size="small" @click="onSubmit">确认发送</van-button>
          </van-col>
        </van-form>
      </template>
    </admin-frame>
  </div>
</template>

<script>
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentwarning } = createNamespacedHelpers('patentwarning');
const { mapActions: patentapply } = createNamespacedHelpers('patentapply');
const { mapActions: upload } = createNamespacedHelpers('upload');
export default {
  name: 'hairmess',
  props: {},
  components: {
    adminFrame,
  },
  data: function () {
    return {
      form: {},
    };
  },
  async created() {},
  methods: {
    ...upload(['upload']),
    ...patentwarning(['query', 'create']),
    ...patentapply({ patentapplyQuery: 'query' }),
    // 确认发送
    async onSubmit() {
      let data = this.form;
      let res = await this.create(data);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `操作成功` });
        this.back();
      } else {
        this.$toast({ type: `success`, message: `${res.errmsg}` });
      }
    },
    // 查询专利申请
    async searchPatent(create_number) {
      let info = { create_number: this.form.create_number ? this.form.create_number : create_number };
      let res = await this.patentapplyQuery({ ...info });
      if (this.$checkRes(res)) {
        let data = { file_url: this.form.file_url };
        if (res.total > 0) {
          for (const val of res.data) {
            data.create_number = val.create_number;
            data.patent_id = val.id;
            data.patent_name = val.name;
            data.to_id = val.user_id;
            data.to_name = val.apply_name;
          }
        }
        this.$set(this, `form`, data);
      }
    },
    async toUpload({ file }, model) {
      // 上传,赋值
      const res = await this.upload({ file, dir: 'file' });
      if (this.$checkRes(res)) {
        this.$set(this.form, model, [{ name: res.name, url: res.uri }]);
        let create_number = file.name.substring(0, file.name.lastIndexOf('.'));
        this.searchPatent(create_number);
      }
    },
    toDelete(file, model) {
      const index = this.form[model].findIndex((f) => _.isEqual(f, file));
      this.form[model].splice(index, 1);
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/admin/examine/hairmess' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.btn {
  text-align: center;
  .van-button {
    margin: 10px;
  }
}
</style>
