<template>
  <div>
    <el-button size="small" type="primary" icon="el-icon-s-order" @click="$refs['formList'].show()">表单中心选择</el-button>

    <el-button size="small" type="primary" icon="el-icon-plus">表单新增</el-button>

    <el-upload
      class="upload"
      ref="upload"
      :action="action"
      :file-list="fileList"
      :auto-upload="false">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="">上传到服务器</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>

    <formList ref="formList" :value="selections" @selected="selectedHandler"></formList>
  </div>
</template>

<script>
import formList from './formList.vue';
export default {
  components: {
    formList
  },
  props: {
    form: Object,
    fieldInfo: Object,
    file: Object,
  },
  data() {
    return {
      action: process.env.VUE_APP_UPLOAD_API,
      selections: [],
    }
  },
  methods: {
    selectedHandler (ids) {
      this.selections = Array.from(new Set(ids))

      this.$emit('update', this.selections)
    }
  },
  computed: {
    fileList () {
      return (this.file && this.fieldInfo?.field) ? this.file[this.fieldInfo.field] : []
    },
  },
  watch: {
    form: {
      handler(newVal) {
        this.selections = newVal[this.fieldInfo.field]
      },
      immediate: true
    }
  }
}
</script>

<style scoped lang="scss">
.upload {

  margin-top: 10px;
}
</style>
