import moment from 'moment'

const booleanAttr = [
  'is_refund',
  'is_book_today',
  'is_ticket_face',
  'is_member_online',
  'is_convertible',
  'is_use_purchase_protocol',
  'is_restricted_sales',
  'is_member_online_face',
  'need_realname',
  'player_need_realname',
  'show_qrcode',
  'is_order_limit'
]

const timeAttr = [
  'sale_time_start',
  'sale_time_end',
  'use_time_start',
  'use_time_end',
  'book_today_endtime',
  'refund_today_endtime'
]

export default {
  props: {
    type: {
      type: String,
      default: ''
    }
  },
  computed: {
    salesChannel () {
      return this.$store.state.app.salesChannel
    },
    ticketProductList () {
      return this.$store.state.app.ticketProductList
    },
    tagList () {
      return this.$store.state.app.tagList
    },
    scenicList () {
      return this.$store.state.app.scenicList
    },
    limitGroupList () {
      return this.$store.state.app.limitGroupList
    }
  },
  created () {
    this.initForm()
  },
  data () {
    return {
      form: {},
      modalType: 'add',
      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 }
        }
      }
    }
  },
  methods: {
    moment,
    initForm (type) {
      const form = {
        type,
        is_ticket_face: 0,
        is_member_online: 0,
        is_member_online_face: 0,
        is_convertible: 0,
        is_use_purchase_protocol: 0,
        commission: 0, // 佣金
        person_num: 1,
        need_realname: 0,
        ticket_product_id: '',
        player_need_realname: 0,
        purchase_separate: 'NONE',
        show_qrcode: 1,
        name: '',
        description: '',
        price: 0,
        price_original: 0,
        is_book_today: 1,
        book_today_endtime: moment().endOf('day'),
        book_future_days: 30,
        book_min: 1,
        book_max: null,
        is_restricted_sales: 0,
        is_order_limit: 0,
        order_limit_group: null,
        sales_volume: 0,
        total_sales_limit: 0,
        is_refund: 0,
        refund_advance_days: 0,
        refund_today_endtime: null,
        refund_strategy: {
          'type': 'RATE',
          'price': 0,
          'rate': 0,
          'contents': []
        },
        sellDate: [moment(), moment().add(10, 'year')],
        sell_startdate: null,
        sell_enddate: null,
        sale_time_start: moment().startOf('day'),
        sale_time_end: moment().endOf('day'),
        advance_days: 0,
        delay_days: 0,
        useDate: [moment(), moment().add(10, 'year')],
        use_startdate: null,
        use_enddate: null,
        use_time_start: moment().startOf('day'),
        use_time_end: moment().endOf('day'),
        use_valid_days: 1,
        check_num: 1,
        purchase_notice: '',
        use_notice: '',
        withdrawal_notice: '',
        other_notice: '',
        total_sales_volume: 5,
        pictures: [],
        tags: []
      }

      booleanAttr.forEach(attr => {
        form[attr] = !!form[attr]
      })

      this.form = form
    },
    render2Form (values) {
      const params = Object.assign({}, values)

      booleanAttr.forEach(attr => {
        params[attr] = params[attr] ? 1 : 0
      })

      timeAttr.forEach(attr => {
        params[attr] = params[attr] ? params[attr].format('HH:mm:ss') : null
      })

      params.sell_startdate = params.sellDate[0].format('YYYY-MM-DD')
      params.sell_enddate = params.sellDate[1].format('YYYY-MM-DD')

      params.use_startdate = params.useDate[0].format('YYYY-MM-DD')
      params.use_enddate = params.useDate[1].format('YYYY-MM-DD')

      delete params.category

      if (params.valid_days_type === 'days') {
        params.valid_startdate = null
        params.valid_enddate = null
      } else if (params.valid_days_type === 'range') {
        params.valid_startdate = params.use_valid_range[0].format('YYYY-MM-DD')
        params.valid_enddate = params.use_valid_range[1].format('YYYY-MM-DD')
        params.use_valid_days = 0
      }

      return params
    },
    form2Render (values) {
      const params = Object.assign({}, values)

      booleanAttr.forEach(attr => {
        params[attr] = !!params[attr]
      })

      timeAttr.forEach(attr => {
        params[attr] = params[attr] && moment(params[attr], 'HH:mm:ss')
      })

      params.sellDate = [moment(params.sell_startdate), moment(params.sell_enddate)]
      params.useDate = [moment(params.use_startdate), moment(params.use_enddate)]

      params.tags = params.tags.map(i => i.id)

      params.refund_strategy = params.refund_strategy || {
        'type': 'RATE',
        'price': 0,
        'rate': 0,
        'contents': []
      }

      if (params.valid_startdate && params.valid_enddate) {
        params.valid_days_type = 'range'
        params.use_valid_range = [moment(params.valid_startdate), moment(params.valid_enddate)]
      } else {
        params.valid_days_type = 'days'
      }

      this.limitIdentities = params.limit_identities || []

      return params
    },
    handleCancel () {
      this.visible = false
    }
  }
}
