import moment from 'moment'
export default {
  beforeCreate () {
    /* 打开租赁票种编辑或新增弹窗前需清空计费类型 */
    this.billingTypeListArr = [] // 清空弹窗上次选中的计费类型
    this.form = this.$form.createForm(this)
    this.form.getFieldDecorator('is_refund', { initialValue: true })
    this.form.getFieldDecorator('is_change', { initialValue: false })
    this.form.getFieldDecorator('is_book_today', { initialValue: true })
    this.form.getFieldDecorator('is_order_limit', { initialValue: false })
    this.form.getFieldDecorator('is_restricted_sales', { initialValue: false })
    this.form.getFieldDecorator('show_qrcode', { initialValue: false })
  },
  computed: {
    tags () {
      return this.$store.state.app.tagList
    }
  },
  data () {
    return {
      previewImage: '',
      imageList: [],
      previewImageVisible: false,
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        }
      },
      formItemLayout2: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        }
      },
      config: {
        language: 'zh_cn',
        heightMin: 300,
        theme: 'green',
        toolbarSticky: false,
        imageUploadURL: this.uploadUrl,
        videoUploadURL: this.uploadUrl,
        events: {
          'froalaEditor.initialized': function () {
            // console.log('initialized')
          },
          'froalaEditor.image.uploaded': (e, editor, response) => {
            // console.log(e, editor, response)
          },
          'froalaEditor.contentChanged': (e, editor) => {
            // this.form.setFieldsValue({ description_content: editor.html.get() })
            // this.form.validateFields(['description_content'], (err, values) => {
            //   if (!err) {
            //     // console.log(values)
            //   }
            // })
          }
        }
      }
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    product: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    moment,
    formatParams (values) {
      console.log('values', values)
      // billing_type基本用billing_type传过来的结构体，但price要改成用户输入的
      // const billing_type = [JSON.parse(values.ticketTypeExtraInfo.billing_type)]
      // billing_type.price = values.ticketTypeExtraInfo.billing_type$price
      return {
        'type': 'RENT',
        // 租赁票种新加字段
        'ticketTypeExtraInfo': {
          'area': values.ticketTypeExtraInfo.area,
          'crop_suitability': values.ticketTypeExtraInfo.crop_suitability,
          'geographic': values.ticketTypeExtraInfo.geographic,
          'soil_type': values.ticketTypeExtraInfo.soil_type
          // 租赁票种计费类型
          // 'billing_type': billing_type // JSON.stringify(values.ticketTypeExtraInfo.billing_type)
          // 'billing_type$price': values.ticketTypeExtraInfo.billing_type$price
        },
        'name': values.name || '',
        'category': values.category,
        'price': values.price || 0,
        'commission': values.commission || 0, // 佣金
        'show_qrcode': values.show_qrcode ? 1 : 0,
        'price_original': values.price_original || 0,
        'book_future_days': values.book_future_days,
        'book_max': values.book_max,
        'book_min': values.book_min,
        'advance_days': values.advance_days || null,
        'delay_days': values.delay_days || null,
        'is_book_today': values.is_book_today ? 1 : 0,
        'is_convertible': values.is_convertible ? 1 : 0,
        'is_promissory_note': values.is_promissory_note ? 1 : 0, // 是否是期票，0不是，1是
        'is_reservation': values.is_reservation ? 1 : 0, // 是否是预约票，0不是，1是
        'is_student': values.is_student ? 1 : 0, // 是否是学生票，0不是，1是
        'show_purchase_protocol_separate': values.show_purchase_protocol_separate ? 1 : 0,
        'is_agency_order': values.is_agency_order ? 1 : 0,
        'is_refund': values.is_refund ? 1 : 0,
        'is_change': values.is_change ? 1 : 0,
        'change_today_endtime': values.change_today_endtime ? values.change_today_endtime.format('HH:mm:ss') : null,
        'discount_rule': JSON.stringify([values.discount_rule0, values.discount_rule1]),
        'tags': values.tags,
        'refund_strategy': values.refund_strategy,
        'age_limits': values.age_limits,
        'refund_advance_days': values.refund_advance_days || 0,
        'need_realname': values.need_realname ? 1 : 0,
        'player_need_realname': values.player_need_realname ? 1 : 0,
        'purchase_separate': values.purchase_separate,
        'book_today_endtime': values.book_today_endtime ? values.book_today_endtime.format('HH:mm:ss') : null,
        'refund_today_endtime': values.refund_today_endtime ? values.refund_today_endtime.format('HH:mm:ss') : null,
        'sale_time_end': values.sale_time_end ? values.sale_time_end.format('HH:mm:ss') : null,
        'sale_time_start': values.sale_time_start ? values.sale_time_start.format('HH:mm:ss') : null,
        // 销售数量限制
        'is_restricted_sales': values.is_restricted_sales ? 1 : 0,
        'is_order_limit': values.is_order_limit ? 1 : 0,
        'limit_identities': values.limit_identities ? 1 : [],
        'order_limit_group': values.order_limit_group,
        'sales_volume': values.sales_volume,
        'total_sales_limit': values.total_sales_limit,
        'sell_startdate': values.sellDate[0].format('YYYY-MM-DD'),
        'sell_enddate': values.sellDate[1].format('YYYY-MM-DD'),
        'ticket_product_id': values.ticket_product_id || null,
        'use_startdate': values.useDate[0].format('YYYY-MM-DD'),
        'use_enddate': values.useDate[1].format('YYYY-MM-DD'),
        'use_time_start': values.use_time_start ? values.use_time_start.format('HH:mm:ss') : null,
        'use_time_end': values.use_time_end ? values.use_time_end.format('HH:mm:ss') : null,
        'use_valid_days': values.use_valid_days,
        'description': values.description || '',
        'other_notice': values.other_notice || '',
        'purchase_notice': values.purchase_notice || '',
        'use_notice': values.use_notice || '',
        'withdrawal_notice': values.withdrawal_notice || '',
        'is_use_purchase_protocol': values.is_use_purchase_protocol ? 1 : 0,
        'purchase_protocol': values.purchase_protocol || '',
        'is_grey_list': values.is_grey_list ? 1 : 0,
        'grey_list_id': values.is_grey_list ? values.grey_list_id : 0
      }
    },
    handleCancel () {
      this.$emit('input', false)
      this.form.resetFields()
      this.formData.age_limits = []
      this.imageList = []
      this.purchase_content && (this.purchase_content = '')
      this.description_content && (this.description_content = '')
      this.withdrawal_content && (this.withdrawal_content = '')
      this.other_content && (this.other_content = '')
      this.limit_identities && (this.limit_identities = [])
      /* 关闭租赁票种编辑或新增弹窗前需清空计费类型 */
      this.billingTypeListArr = [] // 清空弹窗上次选中的计费类型
    },
    beforeImageUpload (file) {
      const isImage = file.type.indexOf('image') > -1
      if (!isImage) {
        this.$message.error('请选择图片格式的文件')
      }
      return isImage
    },
    handleImagePreview (file) {
      // console.log(file)
      this.previewImage = file.url || file.thumbUrl
      this.previewImageVisible = true
    },
    handleImageChange (info) {
      const fileList = info.fileList

      if (!info.file.status) {
        const index = fileList.findIndex(item => {
          return item.uid === info.file.uid
        })
        fileList.splice(index, 1)
      }

      this.imageList = fileList
    },
    handleImageCancel () {
      this.previewImageVisible = false
    }
  },
  watch: {
    description_content (val) {
      this.form.setFieldsValue({ description_content: val })
    },
    purchase_content (val) {
      this.form.setFieldsValue({ purchase_content: val })
    },
    withdrawal_content (val) {
      this.form.setFieldsValue({ withdrawal_content: val })
    },
    other_content (val) {
      this.form.setFieldsValue({ other_content: val })
    }
  }
}
