<template>
  <div id="form-1">
    <van-row>
      <van-col span="24" class="main">
        <van-form @submit="onSubmit">
          <van-field v-model="form.ask_id" name="ask_id" label="发问人id" style="display: none" />
          <van-field v-model="form.ask_name" name="ask_name" label="发问人" :rules="[{ required: true, message: '发问人' }]" />
          <van-field v-model="form.name" name="name" label="问题名称" placeholder="请填写问题名称" :rules="[{ required: true, message: '问题名称' }]" />
          <van-field
            readonly
            clickable
            name="calendar"
            :value="form.ask_date"
            label="发问时间"
            placeholder="点击选择日期"
            @click="showCalendar = true"
            :rules="[{ required: true, message: '发问时间' }]"
          />
          <van-calendar v-model="showCalendar" @confirm="onConfirm" />
          <van-field
            v-model="form.ask_explain"
            name="ask_explain"
            label="问题说明"
            placeholder="请填写问题说明"
            autosize
            type="textarea"
            rows="1"
            :rules="[{ required: true, message: '问题说明' }]"
          />
          <van-field name="ask_file" label="问题文件" :rules="[{ required: false, message: '问题文件' }]">
            <template #input>
              <van-uploader
                :fileList="form.ask_file"
                :max-count="1"
                :after-read="(file) => toUpload(file, 'ask_file')"
                @delete="(file) => toDelete(file, 'ask_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>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: upload } = createNamespacedHelpers('upload');

const moment = require('moment');
export default {
  name: 'form-1',
  props: {
    form: { type: Object },
  },
  components: {},
  data: function () {
    return {
      // 发问时间
      showCalendar: false,
    };
  },
  created() {},
  methods: {
    ...upload(['upload']),
    onSubmit(values) {
      if (values) this.$emit('onSubmit', { data: this.form });
    },
    // 确认选择时间
    onConfirm(values) {
      this.$set(this.form, 'ask_date', moment(values).format('YYYY-MM-DD'));
      this.showCalendar = false;
    },
    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 }]);
      }
    },
    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>
