<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="form" :rules="rules" ref="mobileMonthRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上报期间" prop="repPeri">
              <el-input v-model="form.repPeri" placeholder="请输入上报期间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上报单位" prop="repOrgGuid">
              <el-input v-model="form.repOrgGuid" placeholder="请输入上报单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="月报说明" prop="monthDesc">
              <el-input v-model="form.monthDesc" type="textarea" :rows="3" placeholder="请输入月报说明" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件上传">
              <file-upload v-model="form.fileList" :limit="5" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never" class="mt10">
      <template #header>
        <div class="card-header">
          <span>选择隐患</span>
        </div>
      </template>
      <el-table v-loading="hiddLoading" :data="hiddList" @selection-change="handleHiddSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="隐患名称" align="center" prop="hiddName" :show-overflow-tooltip="true" />
        <el-table-column label="隐患等级" align="center" prop="hiddGrad">
          <template #default="scope">
            <dict-tag :options="hidd_grad" :value="scope.row.hiddGrad" />
          </template>
        </el-table-column>
        <el-table-column label="隐患状态" align="center" prop="hiddStat">
          <template #default="scope">
            <dict-tag :options="hidd_stat" :value="scope.row.hiddStat" />
          </template>
        </el-table-column>
        <el-table-column label="涉及部位" align="center" prop="proPart" :show-overflow-tooltip="true" />
      </el-table>
    </el-card>

    <div class="mt10" style="text-align: center;">
      <el-button type="primary" @click="submitForm">提交月报</el-button>
      <el-button @click="goBack">取消</el-button>
    </div>
  </div>
</template>

<script setup name="MobileMonthAdd">
import { listObjHidd, mobileSaveMonth } from "@/api/slaj/hidd"
import FileUpload from "@/components/FileUpload"

const { proxy } = getCurrentInstance()
const { hidd_grad, hidd_stat } = proxy.useDict("hidd_grad", "hidd_stat")

const hiddList = ref([])
const selectedHidd = ref([])
const hiddLoading = ref(true)

const form = ref({
  repPeri: undefined,
  repOrgGuid: undefined,
  monthDesc: undefined,
  fileList: []
})

const rules = {
  repPeri: [{ required: true, message: "上报期间不能为空", trigger: "blur" }],
  repOrgGuid: [{ required: true, message: "上报单位不能为空", trigger: "blur" }]
}

/** 查询可选隐患列表 */
function getHiddList() {
  hiddLoading.value = true
  listObjHidd({ hiddStat: '0' }).then(res => {
    hiddLoading.value = false
    hiddList.value = res.rows
  })
}

/** 隐患选择改变 */
function handleHiddSelectionChange(selection) {
  selectedHidd.value = selection.map(item => item.guid)
}

/** 提交月报 */
function submitForm() {
  proxy.$refs["mobileMonthRef"].validate(valid => {
    if (valid) {
      const data = {
        ...form.value,
        hiddGuids: selectedHidd.value
      }
      mobileSaveMonth(data).then(response => {
        proxy.$modal.msgSuccess("月报提交成功")
        goBack()
      })
    }
  })
}

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

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

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