<template>
  <div id="form-1">
    <van-row>
      <van-col span="24" class="main">
        <van-form @submit="onSubmit" label-width="4em">
          <van-field v-model="form.send_id" name="send_id" label="发送人id" style="display: none" />
          <van-field v-model="form.send_name" name="send_name" label="发送人" readonly />
          <van-field name="to_type" label="接收用户">
            <template #input>
              <van-radio-group v-model="form.to_type" direction="horizontal">
                <van-radio name="3">平台用户</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field v-model="form.content" name="content" rows="2" autosize label="发送内容" type="textarea" placeholder="发送内容" />
          <van-field name="notice_file" label="通知文件" :rules="[{ required: false, message: '通知文件' }]">
            <template #input>
              <van-uploader
                :fileList="form.notice_file"
                :max-count="1"
                :after-read="(file) => toUpload(file, 'notice_file')"
                @delete="(file) => toDelete(file, 'notice_file')"
                accept="file"
              />
            </template>
          </van-field>
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit">提交</van-button>
          </div>
        </van-form>
      </van-col>
    </van-row>
  </div>
</template>

<script>
const { mapActions: upload } = createNamespacedHelpers('upload');
import { mapState, createNamespacedHelpers } from 'vuex';
export default {
  name: 'form-1',
  props: {
    form: { type: Object },
  },
  components: {},
  data: function () {
    return {};
  },
  created() {},
  methods: {
    ...upload(['upload']),
    onSubmit(values) {
      this.$emit('onSubmit', values);
    },
    async toUpload({ file }, model) {
      // 上传,赋值
      const res = await this.upload({ file, dir: 'analysis' });
      if (this.$checkRes(res)) {
        this.$set(this.form, model, [{ name: res.name, url: res.uri }]);
      }
    },
    toDelete(file, model) {
      const index = this.form[model].findIndex((f) => _.isEqual(f, file));
      this.form[model].splice(index, 1);
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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