<template>
  <div class="layout-no-flex">
    <el-toolbar>
      <el-button @click="$router.back()" type="primary" size="medium" round class="shadow-primary">
        <i class="fa fa-chevron-left"></i> 返回上页</el-button>
      <span class="button-separator"></span>
      <el-button type="button" class="el-button el-button--default save" :loading="submitLoading" @click="saveClickHandler" :priv="this.ID === 'new' ? 'Member.ScoreOperation.Add' : 'Member.ScoreOperation.Edit'">
        <span>
          <i class="fa fa-save"></i>保存</span>
      </el-button>
    </el-toolbar>
    <div>
      <div class="detail-panel">
        <div class="panel-header">
          <div class="title">{{!!scoreOpt.ID ? '编辑积分配置':'添加积分配置'}}</div>
        </div>
        <el-form :model="scoreOpt" v-loading="formDataLoading" :rules="scoreOptRules" ref="savescoreOptForm" label-width="100px"
          label-suffix="：">
          <el-row>
            <el-col :span="12">
              <el-form-item label="积分操作项" prop="name">
                <el-input v-model="scoreOpt.name" readonly>
                  <template slot="append">
                    <el-button type="primary" @click="selectScoreOperation">选择</el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="积分类型" prop="scoreType">
                <el-select clearable placeholder="请选择类型" v-model="scoreOpt.scoreType" style="width:auto;">
                  <el-option v-for="item in scoreSelect" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="积分值" class="score-value" prop="scoreValue" :verify="['NotNull', 'Int']">
                <el-input v-model="scoreOpt.scoreValue" placeholder="添加积分为正数，减少积分为负数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" :verify="['Length<134']" prop="memo">
                <el-input v-model="scoreOpt.memo"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!--积分操作项选择框-->
    <operate-selector :show.sync="isShowModal" :name="this.scoreOpt.name" :code="this.scoreOpt.code" @callback="getScoreOperate"></operate-selector>
  </div>
</template>
<script>
import util from '@src/common/util.js'
import ScoreOperateSelector from './scoreOperateSelector.vue'

export default {
  props: { ID: String },
  components: {
    'operate-selector': ScoreOperateSelector
  },
  created() {
    if (this.ID !== 'new') {
      this.initScoreOperation()
    }
    this.loadScoreSelect()
  },
  data() {
    return {
      formDataLoading: false,
      scoreOpt: {}, //  积分操作数据
      submitLoading: false,
      scoreSelect: [],
      scoreOptRules: {
        // 添加编辑积分配置校验规则
        name: [{ required: true, message: '积分操作项未选择', trigger: 'change' }],
        scoreType: [
          {
            trigger: 'change',
            required: true,
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('积分类型未选择'))
              }
            }
          }
        ]
      },
      isShowModal: false
    }
  },
  methods: {
    // 回调
    getScoreOperate(data) {
      this.scoreOpt.name = data.name
      this.scoreOpt.code = data.code
    },
    selectScoreOperation() {
      this.isShowModal = true
    },
    // 保存积分配置
    async saveClickHandler() {
      this.submitLoading = true
      try {
        await util.validateForm(this.$refs['savescoreOptForm'])
        let res
        if (this.ID === 'new') {
          res = await axios.post('/ui/scoreoperate', this.scoreOpt)
        } else {
          res = await axios.put(`/ui/scoreoperate/${this.ID}`, this.scoreOpt)
        }
        this.submitLoading = false
        util.showMessage(res)
        if (res.data.status === 1 && this.ID === 'new' && res.data.newId) {
          this.$router.replace({path: `/member/scoreOperateForm/${res.data.newId}`})
        }
      } catch (e) {
        util.showErrorMessageBox(e)
        this.submitLoading = false
      }
    },
    // 初始化积分配置
    async initScoreOperation() {
      this.formDataLoading = true
      try {
        let res = await axios.get(`/ui/scoreoperate/${this.ID}`)
        this.scoreOpt = res.data.data
      } catch (e) {}
      this.formDataLoading = false
    },

    // 加载积分配置项类型下拉列表
    async loadScoreSelect() {
      let res = await axios.get('/ui/scoreoperate/scoreselect')
      this.scoreSelect = res.data.data
    }
  }
}
</script>
<style scoped>
  .el-form-item .el-input {
    width: 250px;
    margin-right: 5px;
  }

  .el-form-item .el-textarea {
    max-width: 500px;
  }

  .score-hint {
    font-size: 1em;
    color: #336699;
    font-style: italic;
  }
</style>
