<template>
  <div class="edit-award">
    <el-dialog
      :title="title"
      :visible="true"
      width="550px"
      top="50px"
      :before-close="handleClose"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div style="height: 500px;overflow: auto;">
        <el-form
          ref="dataForm"
          :rules="formRules"
          :model="formData"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item :label="$t('pages.luckyBoxAwardConfig.form.system')" prop="sysOrigin">
            <el-select
              v-model="formData.sysOrigin"
              :placeholder="$t('pages.luckyBoxAwardConfig.placeholder.selectSystem')"
              style="width:100%;"
              class="filter-item"
            >
              <el-option
                v-for="item in permissionsSysOriginPlatforms"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left;"> <sys-origin-icon :icon="item.value" :desc="item.value" /></span>
                <span style="float: left;margin-left:10px">{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('pages.luckyBoxAwardConfig.form.lotteryType')" prop="lotteryType">
            <el-select
              v-model="formData.lotteryType"
              :placeholder="$t('pages.luckyBoxAwardConfig.placeholder.type')"
              clearable
              style="width:100%;"
              class="filter-item"
            >
              <el-option :label="$t('pages.luckyBoxAwardConfig.lotteryType.CLASSICS')" :value="'CLASSICS'" />
              <el-option :label="$t('pages.luckyBoxAwardConfig.lotteryType.CONSTELLATION')" :value="'CONSTELLATION'" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('pages.luckyBoxAwardConfig.form.name')" prop="name">
            <el-input v-model.trim="formData.name" type="text" :placeholder="$t('pages.luckyBoxAwardConfig.placeholder.name')" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">
          {{ $t('common.cancel') }}
        </el-button>
        <el-button
          :loading="submitLoading"
          type="primary"
          @click="handleSubmit"
        >
          {{ $t('common.submit') }}
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { addGameLuckyBoxAwardConfig } from '@/api/game-lucky-box-config'
import { mapGetters } from 'vuex'
export default {
  name: 'AwardConfigEdit',
  props: {
    updateData: {
      type: Object,
      default: null
    },
    sysOrigin: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      formData: {
        id: '',
        sysOrigin: 'HALAR',
        name: '',
        lotteryType: ''
      },
      submitLoading: false
    }
  },
  computed: {
    isAdd() {
      return this.updateData === null
    },
    title() {
      const system = this.isAdd ? this.sysOrigin : this.updateData.sysOrigin
      const action = this.isAdd
        ? this.$t('pages.luckyBoxAwardConfig.action.add')
        : this.$t('pages.luckyBoxAwardConfig.action.edit')
      return this.$t('pages.luckyBoxAwardConfig.dialog.formTitle', { action, system })
    },
    formRules() {
      const requiredMessage = this.$t('pages.luckyBoxAwardConfig.validation.requiredField')
      return {
        sysOrigin: [{ required: true, message: requiredMessage, trigger: 'blur' }],
        name: [{ required: true, message: requiredMessage, trigger: 'blur' }],
        lotteryType: [{ required: true, message: requiredMessage, trigger: 'blur' }]
      }
    },
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  watch: {
    updateData: {
      immediate: true,
      deep: true,
      handler(newVal) {
        Object.assign(this.formData, newVal)
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    handleSubmit() {
      const that = this
      that.$refs.dataForm.validate(valid => {
        if (valid) {
          that.submitLoading = true
          that.formData.sysOrigin = that.sysOrigin
          addGameLuckyBoxAwardConfig(that.formData).then(res => {
            that.submitLoading = false
            this.$emit('success', 'create')
          }).catch(er => {
            that.submitLoading = false
            console.error(er)
            this.$emit('fail')
          })
        } else {
          console.error('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.col-margin {
  margin-bottom: 20px;
}
img[src=""],img:not([src]){
            opacity: 0;
        }
</style>
