<template>
  <div class="edit-role">
    <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.luckyBoxStandardConfig.form.system')" prop="sysOrigin">
            <el-select
              v-model="formData.sysOrigin"
              :placeholder="$t('pages.luckyBoxStandardConfig.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.luckyBoxStandardConfig.form.type')" prop="lotteryType">
            <el-select
              v-model="formData.lotteryType"
              :placeholder="$t('pages.luckyBoxStandardConfig.placeholder.type')"
              clearable
              style="width:100%;"
              class="filter-item"
            >
              <el-option :label="$t('pages.luckyBoxStandardConfig.lotteryType.CLASSICS')" :value="'CLASSICS'" />
              <el-option :label="$t('pages.luckyBoxStandardConfig.lotteryType.CONSTELLATION')" :value="'CONSTELLATION'" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('pages.luckyBoxStandardConfig.form.name')" prop="name">
            <el-input v-model.trim="formData.name" type="text" :placeholder="$t('pages.luckyBoxStandardConfig.placeholder.name')" />
          </el-form-item>
          <el-form-item :label="$t('pages.luckyBoxStandardConfig.form.giftQuantity')" prop="giftQuantity">
            <el-input v-model.trim="formData.giftQuantity" type="text" :placeholder="$t('pages.luckyBoxStandardConfig.placeholder.giftQuantity')" />
          </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 { addStandardConfig } from '@/api/game-lucky-box-config'
import { mapGetters } from 'vuex'
export default {
  name: 'StandardConfigEdit',
  props: {
    updateData: {
      type: Object,
      default: null
    },
    sysOrigin: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      formData: {
        id: '',
        sysOrigin: '',
        name: '',
        lotteryType: '',
        giftQuantity: ''
      },
      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.luckyBoxStandardConfig.action.add')
        : this.$t('pages.luckyBoxStandardConfig.action.edit')
      return this.$t('pages.luckyBoxStandardConfig.dialog.formTitle', { action, system })
    },
    formRules() {
      const requiredMessage = this.$t('pages.luckyBoxStandardConfig.validation.requiredField')
      return {
        sysOrigin: [{ required: true, message: requiredMessage, trigger: 'blur' }],
        lotteryType: [{ required: true, message: requiredMessage, trigger: 'blur' }],
        giftQuantity: [{ 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
          addStandardConfig(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>
