<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('券码名称')" prop="card_type_name">
        <el-input
          v-model="form.card_type_name"
          clearable
          :placeholder="__('请输入券码名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('券码类型')" prop="app_id">
        <el-radio v-model="form.app_id" :label="0">
          {{ __('自营券码') }}
        </el-radio>
        <el-radio v-model="form.app_id" :label="1">
          {{ __('第三方券码') }}
        </el-radio>
      </el-form-item>
      <el-form-item :label="__('券码描述')" prop="card_type_desc">
        <el-input
          v-model="form.card_type_desc"
          clearable
          :placeholder="__('券码描述')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('有效时间')" prop="timeArr" required>
        <el-date-picker
          v-model="form.timeArr"
          :end-placeholder="__('活动结束日期')"
          :start-placeholder="__('活动开始日期')"
          :style="{ width: '100%' }"
          type="datetimerange"
          @change="change"
        />
      </el-form-item>
      <el-form-item :label="__('卡片logo')" prop="card_type_image" required>
        <el-upload
          ref="card_type_image"
          :action="action"
          :before-upload="BeforeUpload"
          class="avatar-uploader"
          :data="params"
          :on-success="(value) => handleAvatarSuccess('card_type_image', value)"
          :show-file-list="false"
        >
          <img
            v-if="form.card_type_image"
            class="avatar"
            :src="form.card_type_image"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item :label="__('券码状态')" prop="card_state">
        <el-radio v-model="form.card_state" :label="1">
          {{ __('开启') }}
        </el-radio>
        <el-radio v-model="form.card_state" :label="0">
          {{ __('未开启') }}
        </el-radio>
      </el-form-item>
    </el-form>
    <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 } from '@/api/card/type'
  import { renderTime } from '@/utils/date'
  import { URL } from '@/config'
  import { getToken } from '@/utils/token'

  export default {
    name: 'CardTypeEdit',
    data() {
      return {
        params: {
          authorization: '',
        },
        action: URL.action,
        form: {
          timeArr: [],
          pay_type: 3,
          app_id: 0,
          card_state: 1,
          card_type_image: '',
        },
        rules: {
          card_type_name: [
            {
              required: true,
              message: this.__('请输入次卡名称'),
              trigger: 'blur',
            },
          ],
          timeArr: [
            {
              required: true,
              message: this.__('请选择有效时间'),
              trigger: 'change',
            },
          ],
          card_type_image: [
            {
              required: true,
              message: this.__('请上传卡片logo'),
              trigger: 'change',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.params.authorization = getToken()
    },
    methods: {
      change(timeArr) {},
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
          this.form = {
            app_id: 0,
            card_state: 1,
            card_type_image: '',
          }
        } else {
          this.title = this.__('编辑')
          this.form = {
            card_type_id: row.card_type_id,
            card_type_name: row.card_type_name,
            card_type_desc: row.card_type_desc,
            app_id: row.app_id,
            card_state: row.card_state,
            card_type_image: row.card_type_image,
            timeArr: [],
          }
          this.form.timeArr.push(row.card_type_starttime)
          this.form.timeArr.push(row.card_type_endtime)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          timeArr: [],
          pay_type: 3,
          card_type_image: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.form.card_type_starttime = renderTime(this.form.timeArr[0])
            this.form.card_type_endtime = renderTime(this.form.timeArr[1])
            this.form.pay_type = 3
            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
          }
        })
      },
      handleAvatarSuccess(name, res) {
        this.form[name] = res.data.media_url
      },
      BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
    },
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
