<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: '名称最多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"
            v-decorator="[
              'img',
              {
                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">
          <a-date-picker
            placeholder="请选择开始时间"
            :showTime="true"
            v-decorator="[
              'starttime',
              {
                validateFirst: true,
                rules: [{ required: true, message: '请选择开始时间' }]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-date-picker
            placeholder="请选择结束时间"
            :showTime="true"
            v-decorator="[
              'endtime',
              {
                validateFirst: true,
                rules: [{ required: true, message: '请选择结束时间' }]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            placeholder="请输入联系电话"
            v-decorator="[
              'phone',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入联系电话' },
                  { max: 20, message: '联系电话最多20个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-cascader
            :options="area"
            placeholder="请选择地区"
            @change="regionChange"
            v-decorator="[
              'area',
              {
                validateFirst: true,
                rules: [{ required: true, message: '请选择地区' }]
              }
            ]"
          >
          </a-cascader>
        </a-form-item>
        <a-form-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :rows="4"
            placeholder="请输入详细地址"
            v-decorator="[
              'address',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入详细地址' },
                  { max: 200, message: '详细地址最多200个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="活动内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            v-show="false"
            style="min-height: 300px;"
            :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 { Submit } from '@/api/modular/main/huodong'
import { UploadImg } from '@/api/modular/main/upload'
import WangEditor from '@/components/Editor/WangEditorV5'
import moment from 'moment'
const areadata = require('@/utils/areadata.json')
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  components: { WangEditor },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      loading: false,
      imageUrl: '',
      headimg: '',
      html: '',
      area: areadata,
      uploadConfig: {
        method: 'http',
        url: 'common/uploadimg'
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    moment,
    GetClientDataList,
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      this.imageUrl = ''
      this.headimg = ''
      this.html = ''
      if (record && record.id) {
        this.headimg = record.img
        this.imageUrl = record.imageurl
        setTimeout(() => {
          this.form.setFieldsValue({
            name: record.name,
            istop: record.istop,
            starttime: moment(record.starttime),
            endtime: moment(record.endtime),
            address: record.address,
            img: record.img,
            area: [record.province, record.city, record.area],
            content: record.content,
            phone: record.phone
          })
          this.html = record.content
        }, 100)
      }
    },
    normFile(e) {
      if (Array.isArray(e)) {
        return e
      }
      return e && e.fileList
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          values['img'] = this.headimg
          values['content'] = this.$refs.contentRef.gethtml()
          values['starttime'] = moment(values['starttime']).format('YYYY-MM-DD HH:mm')
          values['endtime'] = moment(values['endtime']).format('YYYY-MM-DD HH:mm')
          if (values['area'].length !== 3) {
            this.$message.error('请选择地址')
            return
          }
          values['province'] = values['area'][0]
          values['city'] = values['area'][1]
          values['area'] = values['area'][2]
          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
        }
      })
    },
    // 地区选择
    regionChange(data, selectedOptions) {},
    // 上传
    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
          this.form.setFieldsValue({
            img: res.data.path
          })
        } else {
          this.$myerror(res.message)
        }
        this.uploading = false
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
