<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="orgGuid">
        <el-input v-model="queryParams.orgGuid" placeholder="请输入所属单位" clearable style="width: 180px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="日期类型" prop="dateType">
        <el-select v-model="queryParams.dateType" placeholder="请选择日期类型" clearable style="width: 150px">
          <el-option v-for="dict in eme_date_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="dateYear">
        <el-input v-model="queryParams.dateYear" 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:insureeva:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eme:insureeva:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eme:insureeva:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eme:insureeva:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eme-insureeva-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="orgGuid" v-if="columns.orgGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="工程总数" align="center" prop="engTotal" v-if="columns.engTotal.visible" />
      <el-table-column label="是否已投保" align="center" prop="isInsure" v-if="columns.isInsure.visible" />
      <el-table-column label="标段总数" align="center" prop="tendTotal" v-if="columns.tendTotal.visible" />
      <el-table-column label="保费率" align="center" prop="bxl" v-if="columns.bxl.visible" />
      <el-table-column label="保险风险" align="center" prop="bxFx" v-if="columns.bxFx.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:insureeva: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:insureeva: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="800px" 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="startDate">
              <el-date-picker v-model="form.startDate" 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="endDate">
              <el-date-picker v-model="form.endDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束日期" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程总数" prop="engTotal">
              <el-input v-model="form.engTotal" placeholder="请输入工程总数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否已投保" prop="isInsure">
              <el-input v-model="form.isInsure" placeholder="请输入是否已投保" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="未投保数" prop="notInsure">
              <el-input v-model="form.notInsure" placeholder="请输入未投保数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标段总数" prop="tendTotal">
              <el-input v-model="form.tendTotal" placeholder="请输入标段总数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标段已投保" prop="tendIsInsure">
              <el-input v-model="form.tendIsInsure" placeholder="请输入标段已投保" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标段未投保" prop="tendNotInsure">
              <el-input v-model="form.tendNotInsure" placeholder="请输入标段未投保" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保费率" prop="bxl">
              <el-input v-model="form.bxl" placeholder="请输入保费率" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="未参保率" prop="nbxl">
              <el-input v-model="form.nbxl" placeholder="请输入未参保率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险风险" prop="bxFx">
              <el-input v-model="form.bxFx" placeholder="请输入保险风险" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="日期类型" prop="dateType">
              <el-select v-model="form.dateType" placeholder="请选择日期类型">
                <el-option v-for="dict in eme_date_type" :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="dateYear">
              <el-input v-model="form.dateYear" placeholder="请输入年份" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="季度" prop="dateQuarter">
              <el-input v-model="form.dateQuarter" 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="BusiInsureEva">
import { listBusiInsureEva, getBusiInsureEva, delBusiInsureEva, addBusiInsureEva, updateBusiInsureEva } from "@/api/slaj/eme"
import { useTableHeight } from '@/hooks/useTableHeight'

const { tableHeight, containerRef } = useTableHeight()
const { proxy } = getCurrentInstance()
const { eme_date_type } = proxy.useDict("eme_date_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({
  orgGuid: { label: '所属单位', visible: true },
  engTotal: { label: '工程总数', visible: true },
  isInsure: { label: '是否已投保', visible: true },
  tendTotal: { label: '标段总数', visible: true },
  bxl: { label: '保费率', visible: true },
  bxFx: { label: '保险风险', visible: true }
})

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

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

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

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

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

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

function reset() {
  form.value = {
    guid: undefined,
    orgGuid: undefined,
    startDate: undefined,
    endDate: undefined,
    engTotal: undefined,
    isInsure: undefined,
    notInsure: undefined,
    tendTotal: undefined,
    tendIsInsure: undefined,
    tendNotInsure: undefined,
    bxl: undefined,
    nbxl: undefined,
    bxFx: undefined,
    dateType: undefined,
    dateYear: undefined,
    dateQuarter: undefined
  }
  proxy.resetForm("formRef")
}

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