<template>
  <div class="d-flex flex-wrap pb-3">
    <!-- 添加 -->
    <a-modal :closable="false" footer title="添加创业导师" v-model="visible" width="700px">
      <a-form :form="form" @submit="handleSubmit">
        <!-- 导师名称 -->
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="导师名称"
        >
          <a-input
            placeholder="导师名称"
            v-decorator="[
                      'tutorName',
                      {
                        rules: [
                          { required: true, message: '导师名称不能为空' },
                          { whitespace: true, message: '导师名称不能为空' },
                          { max: 140, massage: '导师名称最多140个字符' },
                        ],
                      },
                    ]"
          />
        </a-form-item>
        <!-- 导师简介 -->
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="导师简介"
        >
          <a-textarea
            placeholder="导师简介"
            v-decorator="[
                      'tutorProfile',
                    ]"
          />
        </a-form-item>
        <!-- 头像 -->
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="头像"
        >
          <a-upload
            :before-upload="beforeUpload"
            :show-upload-list="false"
            @change="handleChange"
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            class="avatar-uploader"
            list-type="picture-card"
            name="avatar"
          >
            <img :src="imageUrl" alt="avatar" v-if="imageUrl" />
            <div v-else>
              <a-icon :type="loading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>

        <a-form-item class="d-flex flex-row-reverse">
          <a-button @click="$emit('closeModal')" class="mr-3">取消</a-button>
          <a-button class="white--text" html-type="submit" type="primary">保存</a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 16 },
};
const tailItemLayout = { wrapperCol: { span: 16, offset: 6 } };
function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

export default {
  name: "MentorAdd",
  props: { visible: { type: Boolean, default: false } },
  data() {
    return {
      formItemLayout,
      tailItemLayout,
      form: this.$form.createForm(this, { name: 'mentor-add' }),
      loading: false,
      imageUrl: '',
    }
  },

  methods: {
    // 上传图片
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.loading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, imageUrl => {
          this.imageUrl = imageUrl;
          this.loading = false;
        });
      }
    },
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },

    // 提交表单
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFieldsAndScroll(async (err, values) => {
        if (!err) {
          try {
            console.log('values: ', values);
            // const params = this.generateParams(values);
            // const res = await createTask(params);
            // const { data, msg, code } = res.data;
            // // 清除新建项目标志数据
            // this.clearCreateTask();
            // this.$emit('closeDialog');
            // if (code === 200) {
            //   this.handleCreateSuccess(params.executorId);
            // } else {
            //   throw msg;
            // }
          } catch (error) {
            this.$message.error(error || '添加研发团队失败');
          }
        }
      });
    },
  },
};
</script>

<style scoped lang="stylus">
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
