<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="close"
  >
    <el-scrollbar style="height: 550px">
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item :label="__('优惠券名称')" prop="activity_title">
          <el-input v-model="form.activity_title" :placeholder="__('活动标题')" />
        </el-form-item>
        <el-form-item :label="__('优惠券图标')" label-width="120px">
          <upload
            height="64px"
            :image="form.voucher_image"
            width="64px"
            @upImage="(image) => (form.voucher_image = image)"
          />
          <div>{{ __('尺寸 64 * 64') }}</div>
        </el-form-item>
        <el-form-item :label="__('消费金额')" prop="subtotal">
          <el-input v-model="form.subtotal" :placeholder="__('消费金额')" />
        </el-form-item>
        <el-form-item :label="__('面额')" prop="voucher_price">
          <el-input v-model="form.voucher_price" :placeholder="__('面额')" />
        </el-form-item>
        <el-form-item :label="__('可发送总数')" prop="voucher_quantity">
          <el-input
            v-model="form.voucher_quantity"
            :placeholder="__('可发送总数')"
          />
        </el-form-item>
        <el-form-item :label="__('每人限领')" prop="voucher_pre_quantity">
          <el-input
            v-model="form.voucher_pre_quantity"
            :placeholder="__('每人限领')"
          />
        </el-form-item>
        <el-row>
          <el-col :md="12">
            <div class="grid-content bg-purple">
              <el-form-item :label="__('活动开始时间')" prop="activity_starttime">
                <el-date-picker
                  v-model="form.activity_starttime"
                  :placeholder="__('选择日期')"
                  :readonly="isReadOnly"
                  :style="{ width: '100%' }"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :md="12">
            <div class="grid-content bg-purple"></div>
            <el-form-item :label="__('活动结束时间')" prop="activity_endtime">
              <el-date-picker
                v-model="form.activity_endtime"
                :placeholder="__('选择日期')"
                :readonly="isReadOnly"
                :style="{ width: '100%' }"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :md="12">
            <div class="grid-content bg-purple">
              <el-form-item :label="__('生效日期')" prop="voucher_startdate">
                <el-date-picker
                  v-model="form.voucher_startdate"
                  :placeholder="__('选择日期')"
                  :readonly="isReadOnly"
                  :style="{ width: '100%' }"
                  type="date"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :md="12">
            <div class="grid-content bg-purple"></div>
            <el-form-item :label="__('失效日期')" prop="voucher_enddate">
              <el-date-picker
                v-model="form.voucher_enddate"
                :placeholder="__('选择日期')"
                :readonly="isReadOnly"
                :style="{ width: '100%' }"
                type="date"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="activity_state">
          <el-radio v-model="form.activity_state" :label="0">
            {{ __('未开启') }}
          </el-radio>
          <el-radio v-model="form.activity_state" :label="1">
            {{ __('开启') }}
          </el-radio>
        </el-form-item>
        <el-form-item prop="activity_on_is_off">
          <el-radio v-model="form.activity_on_is_off" :label="0">
            {{ __('线上活动') }}
          </el-radio>
          <el-radio v-model="form.activity_on_is_off" :label="1">
            {{ __('线下活动') }}
          </el-radio>
        </el-form-item>
        <el-form-item prop="activity_type">
          <el-radio v-model="form.activity_type" :label="1">
            {{ __('免费参与') }}
          </el-radio>
          <el-radio v-model="form.activity_type" :label="2">
            {{ __('积分参与') }}
          </el-radio>
          <el-radio v-model="form.activity_type" :label="3">
            {{ __('满返券') }}
          </el-radio>
          <!--<el-radio v-model="form.activity_type" :label="4">
            {{ __('分享参与') }}
          </el-radio>-->
        </el-form-item>
        <el-form-item
          v-if="form.activity_type === 4"
          :label="__('分享次数')"
          prop="activity_share_num"
        >
          <el-input
            v-model="form.activity_share_num"
            :placeholder="__('需要分享次数')"
          />
        </el-form-item>
        <el-form-item v-if="form.activity_type === 2" :label="__('需要积分数')" prop="voucher_points_needed">
          <el-input
            v-model="form.voucher_points_needed"
            :placeholder="__('需要积分数')"
          />
        </el-form-item>
        <el-form-item v-if="false && form.activity_type === 3" :label="__('需要费用')" prop="voucher_points_needed">
          <el-input
            v-model="form.voucher_points_needed"
            :placeholder="__('需要费用')"
          />
        </el-form-item>
      </el-form>
    </el-scrollbar>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit, Update } from '@/api/store/activity/base'
  import Upload from '@/components/upload'
  import { URL } from '@/config'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreActivityBaseEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        form: {
          activity_state: 1,
          activity_type: 1,
          activity_starttime: '',
          activity_endtime: '',
          voucher_startdate: '',
          voucher_enddate: '',
          voucher_image: '',
          activity_type_id: 1105,
          activity_on_is_off: 0,
        },
        rules: {
          activity_title: [
            { required: true, trigger: 'blur', message: this.__('活动标题') },
          ],
          subtotal: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入消费金额'),
            },
          ],
          voucher_price: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入面额'),
            },
          ],
          voucher_quantity: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入可发送总数'),
            },
          ],
          voucher_pre_quantity: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入每人限领个数'),
            },
          ],
          activity_starttime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动开始时间'),
            },
          ],
          activity_endtime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动结束时间'),
            },
          ],
          voucher_startdate: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('生效日期'),
            },
          ],
          voucher_enddate: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('失效日期'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    computed: {
      isReadOnly() {
        return this.form.activity_id !== undefined
      },
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            activity_id: row.activity_id,
            activity_type_id: 1105,
            action: 'voucher',
            activity_starttime: renderTime(row.activity_starttime),
            activity_endtime: renderTime(row.activity_endtime),
            voucher_startdate: renderTime(row.activity_rule.voucher_start_date),// + " 00:00:00"
            voucher_enddate: renderTime(row.activity_rule.voucher_end_date),
            activity_title: row.activity_title,
            voucher_image: row.activity_rule.voucher_image,
            subtotal: row.activity_rule.requirement.buy.subtotal,
            voucher_price: row.activity_rule.voucher_price,
            voucher_quantity: row.activity_rule.voucher_quantity,
            voucher_pre_quantity: row.activity_rule.voucher_pre_quantity,
            activity_state: row.activity_state,
            activity_type: row.activity_type,
            voucher_points_needed: row.activity_rule.requirement.points.needed,
            activity_on_is_off: row.activity_on_is_off,
            activity_share_num: row.activity_share_num,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          activity_state: 1,
          activity_type: 1,
          activity_starttime: '',
          activity_endtime: '',
          voucher_startdate: '',
          voucher_enddate: '',
          voucher_image: '',
          activity_type_id: 1105,
          action: 'voucher',
          activity_on_is_off: 0,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.activity_id && this.form.activity_id != undefined) {
              const { msg, status } = await Update(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            } else {
              const { msg, status } = await doEdit(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
