<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" append-to-body :close-on-click-modal="false" @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item v-if="action === 'approve' || action === 'reject'" label="审批意见" prop="comment">
        <el-input
          v-model="form.comment"
          type="textarea"
          placeholder="请输入审批意见"
          maxlength="500"
          :rows="4"
        />
      </el-form-item>
      <el-alert
        v-if="action === 'start'"
        title="提示"
        type="info"
        :closable="false"
        style="margin-bottom: 20px;"
      >
        <div>开始征用后，队伍状态将自动设置为"忙碌"（busy）。</div>
        <div>只有状态为"可用"（available）的队伍才能开始征用。</div>
      </el-alert>
      <el-alert
        v-if="action === 'end'"
        title="提示"
        type="info"
        :closable="false"
        style="margin-bottom: 20px;"
      >
        <div>结束征用后，如果该队伍没有其他进行中的征用，队伍状态将自动恢复为"可用"（available）。</div>
      </el-alert>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { approveRequisition, rejectRequisition, startRequisition, endRequisition } from '@/api/resource/requisition'

export default {
  name: 'RequisitionStatusDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    requisition: {
      type: Object,
      default: null
    },
    action: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      form: {},
      rules: {
        comment: [
          { required: true, message: '审批意见不能为空', trigger: 'blur' },
          { max: 500, message: '审批意见长度不能超过500个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      const titles = {
        approve: '审批通过',
        reject: '审批驳回',
        start: '开始征用',
        end: '结束征用'
      }
      return titles[this.action] || '状态流转'
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
      if (val) {
        this.initForm()
      }
    },
    dialogVisible(val) {
      this.$emit('update:visible', val)
    }
  },
  methods: {
    /** 初始化表单 */
    initForm() {
      this.form = {
        comment: undefined
      }
      this.resetForm('form')
    },
    /** 提交表单 */
    submitForm() {
      if (this.action === 'approve' || this.action === 'reject') {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.executeAction()
          }
        })
      } else {
        this.executeAction()
      }
    },
    /** 执行操作 */
    executeAction() {
      const requisitionId = this.requisition.id
      if (this.action === 'approve') {
        approveRequisition(requisitionId, this.form.comment).then(response => {
          this.$modal.msgSuccess('审批通过成功')
          this.dialogVisible = false
          this.$emit('success')
        })
      } else if (this.action === 'reject') {
        rejectRequisition(requisitionId, this.form.comment).then(response => {
          this.$modal.msgSuccess('审批驳回成功')
          this.dialogVisible = false
          this.$emit('success')
        })
      } else if (this.action === 'start') {
        startRequisition(requisitionId).then(response => {
          this.$modal.msgSuccess('开始征用成功')
          this.dialogVisible = false
          this.$emit('success')
        })
      } else if (this.action === 'end') {
        endRequisition(requisitionId).then(response => {
          this.$modal.msgSuccess('结束征用成功')
          this.dialogVisible = false
          this.$emit('success')
        })
      }
    },
    /** 关闭对话框 */
    handleClose() {
      this.form = {}
    }
  }
}
</script>

