<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="应用" prop="appId">
        <el-select v-model="form.appId" placeholder="请选择应用" style="width: 100%">
          <el-option v-for="item in applicationList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="规则名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入规则名称" />
      </el-form-item>

      <el-form-item label="功能场景" prop="scene">
        <el-input v-model="form.scene" placeholder="请输入功能场景标识" />
      </el-form-item>

      <el-form-item label="消耗积分" prop="costPoints">
        <el-input-number v-model="form.costPoints" :min="0" placeholder="本次行为消耗的积分值" style="width: 100%" />
      </el-form-item>

      <el-form-item label="增加经验" prop="gainExp">
        <el-input-number v-model="form.gainExp" :min="0" placeholder="本次行为增加的经验值" style="width: 100%" />
      </el-form-item>

      <el-form-item label="每日限制" prop="limitCount">
        <el-input-number v-model="form.limitCount" :min="-1" placeholder="每日经验增长限制次数 (-1为无限制)" style="width: 100%" />
        <div style="font-size: 12px; color: #909399; margin-top: 4px">-1 表示无限制</div>
      </el-form-item>

      <el-form-item label="规则描述" prop="description">
        <el-input v-model="form.description" placeholder="请输入规则描述" :rows="3" type="textarea" />
      </el-form-item>

      <el-form-item label="是否启用" prop="isEnabled">
        <el-switch v-model="form.isEnabled" />
      </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 { doAdd, doEdit } from '@/api/system/ability'
  import { getList as getApplicationList } from '@/api/appManagement'

  export default {
    name: 'AbilityManagementEdit',
    data() {
      return {
        form: {
          appId: '',
          name: '',
          scene: '',
          costPoints: 0,
          gainExp: 0,
          limitCount: -1,
          description: '',
          isEnabled: true,
        },
        rules: {
          appId: [{ required: true, trigger: 'blur', message: '请选择应用' }],
          name: [{ required: true, trigger: 'blur', message: '请输入规则名称' }],
          scene: [{ required: true, trigger: 'blur', message: '请输入功能场景' }],
        },
        title: '',
        dialogFormVisible: false,
        applicationList: [],
      }
    },
    created() {
      this.fetchApplicationList()
    },
    methods: {
      async fetchApplicationList() {
        try {
          const { data } = await getApplicationList({ pageNo: 1, pageSize: 100 })
          this.applicationList = data.list
        } catch (error) {
          console.error('获取应用列表失败', error)
        }
      },
      showEdit(row) {
        if (!row || !row.id) {
          this.title = '添加'
          this.form = Object.assign({}, this.$options.data().form)
          if (row) {
            this.form = Object.assign(this.form, row)
          }
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              if (this.title === '添加') {
                const { msg } = await doAdd(this.form)
                this.$baseMessage(msg, 'success')
              } else {
                const { msg } = await doEdit(this.form.id, this.form)
                this.$baseMessage(msg, 'success')
              }
              this.$refs['form'].resetFields()
              this.dialogFormVisible = false
              this.$emit('fetch-data')
              this.form = this.$options.data().form
            } catch (error) {
              console.error(error)
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
