<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="suppName">
        <el-input v-model="queryParams.suppName" placeholder="请输入物资名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="物资类型" prop="suppType">
        <el-select v-model="queryParams.suppType" placeholder="请选择物资类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_supp_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="物资状态" prop="suppStatus">
        <el-select v-model="queryParams.suppStatus" placeholder="请选择物资状态" clearable style="width: 150px">
          <el-option v-for="dict in eme_supp_status" :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:suppliesreserve:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:suppliesreserve:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:suppliesreserve:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:suppliesreserve:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-suppliesreserve-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="suppCode" v-if="columns.suppCode.visible" :show-overflow-tooltip="true" />
      <el-table-column label="物资名称" align="center" prop="suppName" v-if="columns.suppName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="物资类型" align="center" prop="suppType" v-if="columns.suppType.visible">
        <template #default="scope">
          <dict-tag :options="eme_supp_type" :value="scope.row.suppType" />
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="suppAmount" v-if="columns.suppAmount.visible" />
      <el-table-column label="单位" align="center" prop="suppUnit" v-if="columns.suppUnit.visible" />
      <el-table-column label="存储条件" align="center" prop="storCond" v-if="columns.storCond.visible" :show-overflow-tooltip="true" />
      <el-table-column label="管理人" align="center" prop="manaPers" v-if="columns.manaPers.visible" />
      <el-table-column label="管理人电话" align="center" prop="manaPersTel" v-if="columns.manaPersTel.visible" />
      <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:suppliesreserve: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:suppliesreserve: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="750px" 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="suppType">
              <el-select v-model="form.suppType" placeholder="请选择物资类型">
                <el-option v-for="dict in eme_supp_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="suppCode">
              <el-input v-model="form.suppCode" placeholder="请输入物资编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物资名称" prop="suppName">
              <el-input v-model="form.suppName" placeholder="请输入物资名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="suppSpec">
              <el-input v-model="form.suppSpec" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="suppAmount">
              <el-input v-model="form.suppAmount" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="suppUnit">
              <el-input v-model="form.suppUnit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存储条件" prop="storCond">
              <el-input v-model="form.storCond" placeholder="请输入存储条件" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管理人" prop="manaPers">
              <el-input v-model="form.manaPers" placeholder="请输入管理人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理人电话" prop="manaPersTel">
              <el-input v-model="form.manaPersTel" placeholder="请输入管理人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物资状态" prop="suppStatus">
              <el-select v-model="form.suppStatus" placeholder="请选择物资状态">
                <el-option v-for="dict in eme_supp_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调配日期" prop="distDate">
              <el-date-picker v-model="form.distDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择调配日期" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管理单位" prop="manaOrgGuid">
              <el-input v-model="form.manaOrgGuid" 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="BusiSuppliesReserve">
import { listBusiSuppliesReserve, getBusiSuppliesReserve, delBusiSuppliesReserve, addBusiSuppliesReserve, updateBusiSuppliesReserve } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

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

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({
  suppCode: { label: '物资编码', visible: true },
  suppName: { label: '物资名称', visible: true },
  suppType: { label: '物资类型', visible: true },
  suppAmount: { label: '数量', visible: true },
  suppUnit: { label: '单位', visible: true },
  storCond: { label: '存储条件', visible: true },
  manaPers: { label: '管理人', visible: true },
  manaPersTel: { label: '管理人电话', visible: true }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    suppName: undefined,
    suppType: undefined,
    suppStatus: undefined,
    orgGuid: undefined
  },
  rules: {}
})

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

function getList() {
  loading.value = true
  listBusiSuppliesReserve(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()
  getBusiSuppliesReserve(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/suppliesreserve/detail', query: { guid: guid } })
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != undefined) {
        updateBusiSuppliesReserve(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBusiSuppliesReserve(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 delBusiSuppliesReserve(guids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

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

function reset() {
  form.value = {
    guid: undefined,
    orgGuid: undefined,
    suppType: undefined,
    suppCode: undefined,
    suppName: undefined,
    suppSpec: undefined,
    suppAmount: undefined,
    suppUnit: undefined,
    storCond: undefined,
    manaPers: undefined,
    manaPersTel: undefined,
    suppStatus: undefined,
    distDate: undefined,
    manaOrgGuid: undefined,
    note: undefined
  }
  proxy.resetForm("formRef")
}

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