<template>
  <div ref="containerRef" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="队伍名称" prop="teamName">
        <el-input v-model="queryParams.teamName" placeholder="请输入队伍名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="队伍类型" prop="teamType">
        <el-select v-model="queryParams.teamType" placeholder="请选择队伍类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_team_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入所属单位" clearable style="width: 180px" @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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['eme:teaminfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:teaminfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:teaminfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:teaminfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-teaminfo-index"></right-toolbar>
    </el-row>

    <div class="table-wrapper">
    <el-table :height="tableHeight" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="队伍编码" align="center" prop="teamCode" v-if="columns.teamCode.visible" :show-overflow-tooltip="true" />
      <el-table-column label="队伍名称" align="center" prop="teamName" v-if="columns.teamName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="队伍类型" align="center" prop="teamType" v-if="columns.teamType.visible">
        <template #default="scope">
          <dict-tag :options="eme_team_type" :value="scope.row.teamType" />
        </template>
      </el-table-column>
      <el-table-column label="队伍人数" align="center" prop="teamNum" v-if="columns.teamNum.visible" />
      <el-table-column label="队伍电话" align="center" prop="teamTel" v-if="columns.teamTel.visible" />
      <el-table-column label="装备信息" align="center" prop="equiInfo" v-if="columns.equiInfo.visible" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['eme:teaminfo:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['eme:teaminfo:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    </div>

    <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="700px" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属单位" prop="orgGuid">
              <el-input v-model="form.orgGuid" placeholder="请输入所属单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="队伍类型" prop="teamType">
              <el-select v-model="form.teamType" placeholder="请选择队伍类型">
                <el-option v-for="dict in eme_team_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="队伍编码" prop="teamCode">
              <el-input v-model="form.teamCode" placeholder="请输入队伍编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="队伍名称" prop="teamName">
              <el-input v-model="form.teamName" placeholder="请输入队伍名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="chieTeam">
              <el-input v-model="form.chieTeam" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="队伍人数" prop="teamNum">
              <el-input v-model="form.teamNum" placeholder="请输入队伍人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="队伍电话" prop="teamTel">
              <el-input v-model="form.teamTel" placeholder="请输入队伍电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="装备信息" prop="equiInfo">
              <el-input v-model="form.equiInfo" placeholder="请输入装备信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="培训内容" prop="trainCont">
              <el-input v-model="form.trainCont" type="textarea" placeholder="请输入培训内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="队伍职责" prop="teamResp">
              <el-input v-model="form.teamResp" type="textarea" placeholder="请输入队伍职责" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </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="BusiTeamInfo">
import { listBusiTeamInfo, getBusiTeamInfo, delBusiTeamInfo, addBusiTeamInfo, updateBusiTeamInfo } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()
const { eme_team_type } = proxy.useDict("eme_team_type")

const dataList = 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 columns = ref({
  teamCode: { label: '队伍编码', visible: true },
  teamName: { label: '队伍名称', visible: true },
  teamType: { label: '队伍类型', visible: true },
  teamNum: { label: '队伍人数', visible: true },
  teamTel: { label: '队伍电话', visible: true },
  equiInfo: { label: '装备信息', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teamName: undefined,
    teamType: undefined,
    orgGuid: undefined
  },
  rules: {}
})

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

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

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

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

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

function handleAdd() {
  reset()
  open.value = true
  title.value = "添加应急救援队伍"
}

function handleUpdate(row) {
  const guid = row.guid || ids.value
  reset()
  getBusiTeamInfo(guid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改应急救援队伍"
  })
}

function handleDetail(row) {
  const guid = row.guid || ids.value
  proxy.$router.push({ path: '/slaj/eme/teaminfo/detail', query: { guid: guid } })
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateBusiTeamInfo(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBusiTeamInfo(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  const guids = row.guid || ids.value
  proxy.$modal.confirm('是否确认删除该应急救援队伍？').then(function () {
    return delBusiTeamInfo(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

function handleExport() {
  proxy.download("eme/teaminfo/export", { ...queryParams.value }, `teaminfo_${new Date().getTime()}.xlsx`)
}

function reset() {
  form.value = {
    guid: undefined,
    orgGuid: undefined,
    teamType: undefined,
    teamCode: undefined,
    teamName: undefined,
    chieTeam: undefined,
    teamNum: undefined,
    teamTel: undefined,
    trainCont: undefined,
    equiInfo: undefined,
    teamResp: undefined,
    note: undefined
  }
  proxy.resetForm("formRef")
}

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