<template>
  <a-modal
    :title="title"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    :destroyOnClose="true"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入导航标题"
            v-decorator="[
              'name',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入导航标题' },
                  { max: 100, message: '标题最多20个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="排序号" help="数字越小越靠前" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入排序号" v-decorator="['sort']" />
        </a-form-item>
        <a-form-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-upload-dragger
            :customRequest="handleUpload"
            :show-upload-list="false"
            v-decorator="[
              'image',
              {
                rules: [{ required: true, message: '封面不可为空' }]
              }
            ]"
          >
            <img v-if="imageUrl" style="max-height:200px;max-width:400px;" :src="imageUrl" alt="封面图" />
            <div v-else>
              <p class="ant-upload-drag-icon">
                <a-icon :type="loading ? 'loading' : 'inbox'" />
              </p>
              <p class="ant-upload-text">
                上传封面
              </p>
            </div>
          </a-upload-dragger>
        </a-form-item>
        <a-form-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序页面地址"
            v-decorator="[
              'link',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入小程序页面地址' },
                  { max: 100, message: '地址最多200个字' }
                ]
              }
            ]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetClientDataList } from '@/utils/clientdata'
import { Submit } from '@/api/modular/main/navbar'
import { UploadImg } from '@/api/modular/main/upload'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      loading: false,
      imageUrl: '',
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    GetClientDataList,
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      this.imageUrl = ''
      if (record && record.id) {
        this.imageUrl = record.imageurl
        setTimeout(() => {
          this.form.setFieldsValue({
            name: record.name,
            sort: record.sort,
            link: record.link,
            image: record.image
          })
        }, 100)
      }
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          Submit(values)
            .then(res => {
              if (res.code === 0) {
                if (this.currentInfo && this.currentInfo.id) {
                  this.$message.success('编辑成功')
                } else {
                  this.$message.success('添加成功')
                }
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$myerror(res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    typeChange(item) {
      this.type = item
    },
    // 上传
    handleUpload(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      this.uploading = true
      UploadImg(formData).then(res => {
        if (res.code === 0) {
          this.imageUrl = res.data.url
          this.form.setFieldsValue({
            image: res.data.path
          })
        } else {
          this.$myerror(res.message)
        }
        this.uploading = false
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
