<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="[
              'title',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入标题' },
                  { max: 100, message: '标题最多100个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="是否置顶" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch v-decorator="['istop', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-upload-dragger :customRequest="handleUpload" :show-upload-list="false">
            <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">
          <a-select
            @change="typeChange"
            placeholder="请选择类型"
            v-decorator="['ptype', { rules: [{ required: true, message: '请选择类型' }], initialValue: 1 }]"
          >
            <a-select-option
              v-for="(item, index) in GetClientDataList('conenttypeenum')"
              :key="index"
              :value="item.code"
              >{{ item.val }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="小程序Appid" v-if="type == 1" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序appid"
            v-decorator="['appid', { rules: [{ required: true, message: '请输入小程序appid' }] }]"
          />
        </a-form-item>
        <a-form-item label="小程序页面地址" v-if="type == 1" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入小程序页面地址" v-decorator="['content']" />
        </a-form-item>
        <a-form-item label="链接" v-if="type == 2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :rows="2"
            placeholder="请输入链接地址"
            v-decorator="['content', { rules: [{ required: true, message: '请输入链接地址' }] }]"
          />
        </a-form-item>
        <a-form-item label="页面地址" v-if="type == 3" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入小程序内的页面地址"
            v-decorator="['content', { rules: [{ required: true, message: '请输入小程序内的页面地址' }] }]"
          />
        </a-form-item>
        <a-form-item label="内容" v-if="type == 4" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea style="display: none;" :rows="4" placeholder="请输入内容" v-decorator="['content']" />
          <WangEditor ref="contentRef" :value="html" :uploadConfig="uploadConfig" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetClientDataList } from '@/utils/clientdata'
import { GYSubmit } from '@/api/modular/main/info'
import { UploadImg } from '@/api/modular/main/upload'
import WangEditor from '@/components/Editor/WangEditorV5'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  components: { WangEditor },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      loading: false,
      imageUrl: '',
      headimg: '',
      html: '',
      uploadConfig: {
        method: 'http',
        url: 'common/uploadimg'
      },
      type: 1, // 当前类型,1-小程序,2-链接,3-页面,4-富文本
      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 = ''
      this.headimg = ''
      this.html = ''
      if (record && record.id) {
        this.type = record.ptype
        this.headimg = record.headimg
        this.imageUrl = record.imageurl
        setTimeout(() => {
          this.form.setFieldsValue({
            title: record.title,
            istop: record.istop,
            ptype: record.ptype,
            content: record.content
          })
          if (record.ptype === 4) {
            this.html = record.content
          } else if (record.ptype === 1) {
            this.form.setFieldsValue({
              appid: record.appid
            })
          }
        }, 100)
      }
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          values['headimg'] = this.headimg
          values['content'] = values['ptype'] === 4 ? this.$refs.contentRef.gethtml() : values['content']
          GYSubmit(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.headimg = res.data.path
        } else {
          this.$myerror(res.message)
        }
        this.uploading = false
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
