<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="时间范围" prop="dateRange">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hidd:revo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="hidd-revo-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="revoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="撤销原因" align="center" prop="revoReason" v-if="columns.revoReason.visible" :show-overflow-tooltip="true" />
      <el-table-column label="撤销时间" align="center" prop="revoTime" v-if="columns.revoTime.visible" width="180" />
      <el-table-column label="审核状态" align="center" prop="verifyStat" v-if="columns.verifyStat.visible">
        <template #default="scope">
          <dict-tag :options="verify_stat" :value="scope.row.verifyStat" />
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="verifyOpinion" v-if="columns.verifyOpinion.visible" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-tooltip content="审核" placement="top" v-if="scope.row.verifyStat === '0'">
            <el-button link type="primary" icon="Stamp" @click="handleVerify(scope.row)" v-hasPermi="['hidd:revo:edit']"></el-button>
          </el-tooltip>
        </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="撤销审核" v-model="verifyOpen" width="600px" append-to-body>
      <el-form :model="verifyForm" :rules="verifyRules" ref="verifyRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="撤销原因">
              <el-input v-model="verifyForm.revoReason" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核结果" prop="verifyResult">
              <el-radio-group v-model="verifyForm.verifyResult">
                <el-radio label="1">通过</el-radio>
                <el-radio label="2">驳回</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核意见" prop="verifyOpinion">
              <el-input v-model="verifyForm.verifyOpinion" type="textarea" placeholder="请输入审核意见" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitVerify">确 定</el-button>
          <el-button @click="verifyOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BisOrgMonRepRevo">
import { listBisOrgMonRepRevo, getBisOrgMonRepRevo, updateBisOrgMonRepRevo } from "@/api/slaj/hidd"

const { proxy } = getCurrentInstance()
const { verify_stat } = proxy.useDict("verify_stat")

const revoList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])

// 审核对话框
const verifyOpen = ref(false)
const verifyForm = ref({})
const verifyRules = {
  verifyResult: [{ required: true, message: "审核结果不能为空", trigger: "change" }]
}

const columns = ref({
  revoReason: { label: '撤销原因', visible: true },
  revoTime: { label: '撤销时间', visible: true },
  verifyStat: { label: '审核状态', visible: true },
  verifyOpinion: { label: '审核意见', visible: true }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    beginTime: undefined,
    endTime: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询月报撤销审核列表 */
function getList() {
  loading.value = true
  listBisOrgMonRepRevo(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false
    revoList.value = res.rows
    total.value = res.total
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.guid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 审核按钮 */
function handleVerify(row) {
  verifyForm.value = {
    guid: row.guid,
    revoReason: row.revoReason,
    verifyResult: undefined,
    verifyOpinion: undefined
  }
  verifyOpen.value = true
}

/** 提交审核 */
function submitVerify() {
  proxy.$refs["verifyRef"].validate(valid => {
    if (valid) {
      const data = {
        guid: verifyForm.value.guid,
        verifyStat: verifyForm.value.verifyResult,
        verifyOpinion: verifyForm.value.verifyOpinion
      }
      updateBisOrgMonRepRevo(data).then(response => {
        proxy.$modal.msgSuccess("审核成功")
        verifyOpen.value = false
        getList()
      })
    }
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("hidd/monreprevo/export", {
    ...queryParams.value
  }, `monRepRevo_${new Date().getTime()}.xlsx`)
}

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