<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="场地名称">
          <a-input placeholder="请输入场地名称" v-decorator="['buildingName']" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
          <a-input placeholder="请输入地址" v-decorator="['address']" />
        </a-form-item>
        <a-form-item label="照片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <uploadFile v-if="visible" :value.sync="imageUrl" :fileCount="10" uploadType="image" :sizeCheck="false"/>
        </a-form-item>
        <a-form-item label="视频" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <uploadFile v-if="visible" :value.sync="videoUrl" :fileCount="10" uploadType="video" :sizeCheck="false"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人姓名">
          <a-input placeholder="请输入联系人姓名" v-decorator="['contactName']" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人电话">
          <a-input placeholder="请输入联系人电话" v-decorator="['contactMobile']" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-input placeholder="请输入备注" v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import JTreeSelect from '@/components/jeecg/JTreeSelect'
import moment from 'moment'
import UploadToAli from '@femessage/upload-to-ali'
import uploadFile from '@/components/uploadFile.vue'
export default {
  name: 'YardModal',
  components: {
    UploadToAli,
    JTreeSelect,
    uploadFile,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      videoUrl: [],
      imageUrl: [],
      customDomain: '',
      region: 'oss-cn-beijing',
      bucket: 'ctop-media',
      acceptVideo: 'video/mpeg,video/mp4',
      acceptImage: 'image/jpeg,image/jpg,image/png',
      accessKeyId: 'LTAIbNbqWzSOklQV',
      accessKeySecret: '1rkPz7JNoXk8sJevPaeYHWqfkQXBGh',
      dateFormat: 'YYYY-MM-DD',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),

      url: {
        add: '/ctop/yard/add',
        edit: '/ctop/yard/edit',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      let that = this
      that.form.resetFields()
      that.model = Object.assign({}, record)
      that.visible = true
      this.imageUrl = record.imageUrl ? record.imageUrl : []
      this.videoUrl = record.videoUrl ? record.videoUrl : []
      that.$nextTick(() => {
        that.form.setFieldsValue(pick(this.model, 'buildingName', 'contactName', 'contactMobile', 'address', 'remark'))
        //时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    moment,
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          values.imageUrl = this.imageUrl
          values.videoUrl = this.videoUrl
          values.coverUrl = this.imageUrl[0]
          let formData = Object.assign(this.model, values)
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped>
</style>