<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="500px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="图片" prop="urls" required>
        <!-- <mult-upload
          v-model="form.urls"
          :uploadUrl="$upload('/yxl-back-end/framework/oss/minio/upload')"
          :limit="10"
          params="prefix="
        /> -->
        <upload
          v-model="form.urls"
          :uploadUrl="$upload('/yxl-back-end/framework/oss/minio/upload')"
          :limit="10"
          params="prefix="
        />
      </el-form-item>
      <el-form-item label="关联场景" prop="kindergartenId">
        <SceneSelect v-model="form.kindergartenId" />
      </el-form-item>
      <el-form-item label="关联活动">
        <ActivitySelect
          v-model="form.activityId"
          :kindergartenId="form.kindergartenId"
          :firstLoad="false"
          :disabled="form.kindergartenId === null"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveItem } from '@/api/photoWarehouse';

export default {
  name: 'PhotoUploadModal',

  data() {
    return {
      title: '上传图片',
      modal: true,

      form: {
        urls: [],
        kindergartenId: '',
        activityId: ''
      },
      rules: {
        urls: [{ required: true, message: '请上传图片', trigger: 'change' }],
        kindergartenId: [{ required: true, message: '请选择关联场景', trigger: 'blur' }]
      }
    };
  },

  watch: {
    'form.kindergartenId': {
      handler(val) {
        this.form.activityId = '';
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // let params = this.form.urls.map(x => ({
          //   activityId: this.form.activityId,
          //   kindergartenId: this.form.kindergartenId,
          //   originPhoto: x.url,
          //   size: x.size,
          //   originFilename: x.name
          // }));
          let params = this.form;
          params.fileRequestList = this.form.urls.map(x => ({
            originalFilename: x.name,
            size: x.size,
            fullPath: x.url
          }));
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('photo_reload');
          }
        }
      });
    }
  }
};
</script>

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