<template>
  <div class="app-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>移动端月报撤回</span>
          <el-button type="primary" plain icon="ArrowLeft" @click="goBack">返回</el-button>
        </div>
      </template>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="上报单位" prop="repOrgGuid">
          <el-input v-model="queryParams.repOrgGuid" placeholder="请输入上报单位" clearable style="width: 240px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="上报期间" prop="repPeri">
          <el-input v-model="queryParams.repPeri" placeholder="请输入上报期间" clearable style="width: 240px" @keyup.enter="handleQuery" />
        </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-card>

    <el-card shadow="never" class="mt10">
      <el-table v-loading="loading" :data="monthList">
        <el-table-column label="上报单位" align="center" prop="repOrgGuid" :show-overflow-tooltip="true" />
        <el-table-column label="上报期间" align="center" prop="repPeri" />
        <el-table-column label="上报状态" align="center" prop="repStat">
          <template #default="scope">
            <dict-tag :options="rep_stat" :value="scope.row.repStat" />
          </template>
        </el-table-column>
        <el-table-column label="上报时间" align="center" prop="repTime" width="180" />
        <el-table-column label="操作" align="center" width="120">
          <template #default="scope">
            <el-tooltip content="撤回" placement="top" v-if="scope.row.repStat === '1'">
              <el-button link type="warning" icon="RefreshLeft" @click="handleRevocation(scope.row)"></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-card>

    <!-- 撤回确认对话框 -->
    <el-dialog title="确认撤回" v-model="revocationOpen" width="500px" append-to-body>
      <el-form :model="revocationForm" :rules="revocationRules" ref="revocationRef" label-width="100px">
        <el-form-item label="撤回原因" prop="reason">
          <el-input v-model="revocationForm.reason" type="textarea" :rows="3" placeholder="请输入撤回原因" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRevocation">确 定</el-button>
          <el-button @click="revocationOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MobileMonthRevocation">
import { listBisOrgMonRepPeri, mobileSaveRevoc } from "@/api/slaj/hidd"

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

const monthList = ref([])
const loading = ref(true)
const total = ref(0)
const revocationOpen = ref(false)
const revocationForm = ref({})
const revocationRules = {
  reason: [{ required: true, message: "撤回原因不能为空", trigger: "blur" }]
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    repOrgGuid: undefined,
    repPeri: undefined,
    repStat: '1'
  }
})

const { queryParams } = toRefs(data)

/** 查询月报列表 */
function getList() {
  loading.value = true
  listBisOrgMonRepPeri(queryParams.value).then(res => {
    loading.value = false
    monthList.value = res.rows
    total.value = res.total
  })
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.repStat = '1'
  handleQuery()
}

/** 撤回按钮 */
function handleRevocation(row) {
  revocationForm.value = {
    guid: row.guid,
    reason: undefined
  }
  revocationOpen.value = true
}

/** 提交撤回 */
function submitRevocation() {
  proxy.$refs["revocationRef"].validate(valid => {
    if (valid) {
      mobileSaveRevoc(revocationForm.value).then(response => {
        proxy.$modal.msgSuccess("撤回成功")
        revocationOpen.value = false
        getList()
      })
    }
  })
}

/** 返回 */
function goBack() {
  proxy.$router.push({ path: '/slaj/hidd/month' })
}

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

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.mt10 {
  margin-top: 10px;
}
</style>
