<template>
  <a-modal
    title="内容管理"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item label="销售咨询" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
        <a-input
          placeholder="请输入职位名称"
          v-decorator="['name', {rules: [{required: true, message: '请输入职位名称'}]}]"
        />
      </a-form-item>

      <a-form-item label="上传二维码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
        <a-upload
          name="file"
          list-type="picture-card"
          class="avatar-uploader"
          :show-upload-list="false"
          action="http://192.168.131.126:8080/api/sysFileInfo/upload"
          :before-upload="beforeUpload"
          @change="handleChange"
        >
          <img v-if="imageUrl" :src="imageUrl" alt="avatar" />
          <div v-else>
            <a-icon :type="loading ? 'loading' : 'plus'" />
          </div>
        </a-upload>
        <div class="ant-upload-text">请上传250*250规格图像</div>
      </a-form-item>
      <a-form-item label="销售咨询" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
        <a-input
          placeholder="请输入职位名称"
          v-decorator="['name', {rules: [{required: true, message: '请输入职位名称'}]}]"
        />
      </a-form-item>
      <a-form-item label="销售咨询" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
        <a-input
          placeholder="请输入职位名称"
          v-decorator="['name', {rules: [{required: true, message: '请输入职位名称'}]}]"
        />
      </a-form-item>
      <a-form-item label="销售咨询" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
        <a-input
          placeholder="请输入职位名称"
          v-decorator="['name', {rules: [{required: true, message: '请输入职位名称'}]}]"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
function getBase64 (img, callback) {
  const reader = new FileReader()
  reader.addEventListener('load', () => callback(reader.result))
  reader.readAsDataURL(img)
}

export default {
  data () {
    return {
      imageUrl: '',
      loading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    details (record) {
      this.visible = true
      // setTimeout(() => {
      //   this.form.setFieldsValue({
      //     message: record.message
      //   })
      // }, 100)
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    },
    // 上传图片
    handleChange (info) {
      if (info.file.status === 'uploading') {
        this.loading = true
        return
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        // 获取图片传参
        console.log(info.fileList[info.fileList.length - 1].response.data)
        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!')
      }
      console.log(file)
      return isJpgOrPng && isLt2M
    }
  }
}
</script>
