<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="填报月份" prop="month">
        <el-date-picker v-model="queryParams.month" type="month" value-format="YYYY-MM" placeholder="请选择月份" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="审核状态" prop="reviewStatus">
        <el-select v-model="queryParams.reviewStatus" placeholder="审核状态" clearable style="width: 200px">
          <el-option label="待提交" value="DRAFT" />
          <el-option label="已提交待审核" value="SUBMITTED" />
          <el-option label="审核通过" value="APPROVED" />
          <el-option label="审核不通过" value="REJECTED" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Edit" @click="handleAdd" v-hasPermi="['performance:output:add']">填报产值</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Upload" :disabled="multiple" @click="handleBatchSubmit" v-hasPermi="['performance:output:submit']">批量提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['performance:output:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outputList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true" />
      <el-table-column label="填报月份" align="center" prop="month" />
      <el-table-column label="当月进度(%)" align="center" prop="currentProgress" />
      <el-table-column label="当月产值(万元)" align="center" prop="currentOutput" />
      <el-table-column label="协作产值(万元)" align="center" prop="coopOutput" />
      <el-table-column label="审核状态" align="center" prop="reviewStatus">
        <template #default="scope">
          <dict-tag :options="reviewStatusOptions" :value="scope.row.reviewStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-if="scope.row.reviewStatus === 'DRAFT'" v-hasPermi="['performance:output:edit']">修改</el-button>
          <el-button link type="primary" icon="Upload" @click="handleSubmit(scope.row)" v-if="scope.row.reviewStatus === 'DRAFT'" v-hasPermi="['performance:output:submit']">提交</el-button>
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 产值填报对话框 -->
    <el-dialog :title="title" v-model="open" width="650px" append-to-body>
      <el-form :model="form" :rules="rules" ref="outputRef" label-width="140px">
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目" :disabled="form.outputId !== undefined" style="width: 100%">
            <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName" :value="item.projectId" />
          </el-select>
        </el-form-item>
        <el-form-item label="填报月份" prop="month">
          <el-date-picker v-model="form.month" type="month" value-format="YYYY-MM" placeholder="请选择月份" :disabled="form.outputId !== undefined" style="width: 100%" />
        </el-form-item>
        <el-form-item label="当月实际进度(%)" prop="currentProgress">
          <el-input-number v-model="form.currentProgress" :precision="1" :min="0" :max="100" placeholder="请输入当月实际进度" style="width: 100%" />
        </el-form-item>
        <el-form-item label="当月实际产值(万元)" prop="currentOutput">
          <el-input-number v-model="form.currentOutput" :precision="4" :min="0" placeholder="请输入当月实际产值" style="width: 100%" />
        </el-form-item>
        <el-form-item label="进度说明" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入进度说明" maxlength="500" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PerformanceOutput">
import { listOutput, getOutput, addOutput, updateOutput, submitOutput } from '@/api/performance/output'

const { proxy } = getCurrentInstance()

const reviewStatusOptions = ref([
  { label: '待提交', value: 'DRAFT' },
  { label: '已提交待审核', value: 'SUBMITTED' },
  { label: '审核通过', value: 'APPROVED' },
  { label: '审核不通过', value: 'REJECTED' }
])

const outputList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const projectOptions = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectName: undefined,
    month: undefined,
    reviewStatus: undefined
  },
  rules: {
    projectId: [{ required: true, message: '项目不能为空', trigger: 'change' }],
    month: [{ required: true, message: '填报月份不能为空', trigger: 'change' }],
    currentProgress: [{ required: true, message: '当月实际进度不能为空', trigger: 'blur' }],
    currentOutput: [{ required: true, message: '当月实际产值不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

function getList() {
  loading.value = true
  listOutput(queryParams.value).then(res => {
    outputList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.outputId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function reset() {
  form.value = {
    outputId: undefined,
    projectId: undefined,
    month: undefined,
    currentProgress: undefined,
    currentOutput: undefined,
    description: undefined
  }
  proxy.resetForm('outputRef')
}

function cancel() {
  open.value = false
  reset()
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '产值填报'
}

function handleUpdate(row) {
  reset()
  getOutput(row.outputId).then(res => {
    form.value = res.data
    open.value = true
    title.value = '修改产值填报'
  })
}

function handleDetail(row) {
  // TODO: 查看填报详情
}

function handleSubmit(row) {
  proxy.$modal.confirm('确认提交该条产值填报？提交后将无法修改。').then(() => {
    return submitOutput(row.outputId)
  }).then(() => {
    proxy.$modal.msgSuccess('提交成功')
    getList()
  }).catch(() => {})
}

function handleBatchSubmit() {
  if (ids.value.length === 0) {
    proxy.$modal.msgWarning('请选择要提交的填报记录')
    return
  }
  proxy.$modal.confirm('确认批量提交选中的产值填报？').then(() => {
    const tasks = ids.value.map(id => submitOutput(id))
    return Promise.all(tasks)
  }).then(() => {
    proxy.$modal.msgSuccess('批量提交成功')
    getList()
  }).catch(() => {})
}

function handleExport() {
  proxy.download('/performance/output/export', { ...queryParams.value }, `output_${new Date().getTime()}.xlsx`)
}

function submitForm() {
  proxy.$refs.outputRef.validate(valid => {
    if (valid) {
      if (form.value.outputId) {
        updateOutput(form.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addOutput(form.value).then(() => {
          proxy.$modal.msgSuccess('填报成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

onMounted(() => {
  getList()
})
</script>
