<template>
  <el-dialog
    title="批量审批"
    :visible.sync="visible"
    width="600px"
    append-to-body
    @close="handleClose"
  >
    <div v-loading="loading">
      <!-- 待审批数据列表 -->
      <div style="margin-bottom: 20px">
        <div style="font-weight: bold; margin-bottom: 10px">待审批项目（共 {{ selectedRows.length }} 项）</div>
        <el-table :data="selectedRows" style="width: 100%" max-height="200">
          <el-table-column prop="code" label="申请单号" width="150" show-overflow-tooltip />
          <el-table-column prop="proName" label="项目名称" show-overflow-tooltip />
        </el-table>
      </div>

      <!-- 审批操作 -->
      <el-form ref="approveForm" :model="approveData" label-width="100px">
        <el-form-item label="审批结果" prop="approveResult" required>
          <el-radio-group v-model="approveData.approveResult">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审批意见" prop="approveOpinion" required>
          <el-input
            v-model="approveData.approveOpinion"
            type="textarea"
            rows="4"
            placeholder="请输入审批意见"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">提交审批</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doApprove, getApprovalInstanceByBusiness } from '@/api/approval'

export default {
  name: 'BatchApproveDialog',
  data() {
    return {
      visible: false,
      loading: false,
      selectedRows: [],
      businessType: 'EQUIPMENT_OUT',
      approveData: {
        approveResult: 1,
        approveOpinion: ''
      }
    }
  },
  methods: {
    openDialog(selectedRows, businessType = 'EQUIPMENT_OUT') {
      if (!selectedRows || selectedRows.length === 0) {
        this.$message.warning('请先选择要审批的项目')
        return
      }
      this.selectedRows = selectedRows
      this.businessType = businessType
      this.approveData = {
        approveResult: 1,
        approveOpinion: ''
      }
      this.visible = true
    },

    async handleSubmit() {
      // 验证表单
      if (!this.approveData.approveOpinion.trim()) {
        this.$message.warning('请输入审批意见')
        return
      }

      this.loading = true
      try {
        // 对每个选中的项目执行审批
        const promises = this.selectedRows.map(async (row) => {
          // 先获取审批实例ID
          const instanceRes = await getApprovalInstanceByBusiness(this.businessType, row.id)
          if (!instanceRes || !instanceRes.data || !instanceRes.data.id) {
            throw new Error(`获取审批实例失败: ${row.code}`)
          }

          // 执行审批
          return doApprove({
            instanceId: instanceRes.data.id,
            approveResult: this.approveData.approveResult,
            approveOpinion: this.approveData.approveOpinion
          })
        })

        await Promise.all(promises)
        this.$message.success('批量审批成功')
        this.visible = false
        this.$emit('success')
      } catch (error) {
        console.error('批量审批失败:', error)
        this.$message.error(error.message || '批量审批失败，请重试')
      } finally {
        this.loading = false
      }
    },

    handleClose() {
      this.visible = false
      this.selectedRows = []
      this.approveData = {
        approveResult: 1,
        approveOpinion: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 20px;
}
</style>

