<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" @tab-change="handleTabChange">
      <el-tab-pane label="招标" name="caller">
        <el-form :model="callerQueryParams" ref="callerQueryRef" :inline="true" v-show="showSearch">
          <el-form-item label="招标编号" prop="ivtCode">
            <el-input v-model="callerQueryParams.ivtCode" placeholder="请输入招标编号" clearable @keyup.enter="handleCallerQuery" />
          </el-form-item>
          <el-form-item label="招标标题" prop="ivtTitle">
            <el-input v-model="callerQueryParams.ivtTitle" placeholder="请输入招标标题" clearable @keyup.enter="handleCallerQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleCallerQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetCallerQuery">重置</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="handleCallerAdd" v-hasPermi="['pms:bidding:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleCallerUpdate" v-hasPermi="['pms:bidding:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleCallerDelete" v-hasPermi="['pms:bidding:remove']">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getCallerList" />
        </el-row>

        <el-table v-loading="callerLoading" :data="callerList" @selection-change="handleCallerSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="招标编号" align="center" prop="ivtCode" width="140" />
          <el-table-column label="招标标题" align="center" prop="ivtTitle" min-width="200" show-overflow-tooltip />
          <el-table-column label="预算金额" align="center" prop="ivtBudget" width="120" />
          <el-table-column label="招标状态" align="center" prop="ivtState" width="80" />
          <el-table-column label="操作" align="center" width="200">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleCallerUpdate(scope.row)" v-hasPermi="['pms:bidding:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleCallerDelete(scope.row)" v-hasPermi="['pms:bidding:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="callerTotal > 0" :total="callerTotal" v-model:page="callerQueryParams.pageNum" v-model:limit="callerQueryParams.pageSize" @pagination="getCallerList" />
      </el-tab-pane>

      <el-tab-pane label="投标" name="tender">
        <el-form :model="tenderQueryParams" ref="tenderQueryRef" :inline="true" v-show="showSearch">
          <el-form-item label="投标单位" prop="bidUnit">
            <el-input v-model="tenderQueryParams.bidUnit" placeholder="请输入投标单位" clearable @keyup.enter="handleTenderQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleTenderQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetTenderQuery">重置</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="handleTenderAdd" v-hasPermi="['pms:bidding:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="tenderSingle" @click="handleTenderUpdate" v-hasPermi="['pms:bidding:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="tenderMultiple" @click="handleTenderDelete" v-hasPermi="['pms:bidding:remove']">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getTenderList" />
        </el-row>

        <el-table v-loading="tenderLoading" :data="tenderList" @selection-change="handleTenderSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="投标单位" align="center" prop="bidUnit" min-width="200" show-overflow-tooltip />
          <el-table-column label="投标金额" align="center" prop="bidAmount" width="120" />
          <el-table-column label="投标日期" align="center" prop="bidDate" width="100" />
          <el-table-column label="操作" align="center" width="200">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleTenderUpdate(scope.row)" v-hasPermi="['pms:bidding:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleTenderDelete(scope.row)" v-hasPermi="['pms:bidding:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="tenderTotal > 0" :total="tenderTotal" v-model:page="tenderQueryParams.pageNum" v-model:limit="tenderQueryParams.pageSize" @pagination="getTenderList" />
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <template v-if="activeTab === 'caller'">
          <el-row>
            <el-col :span="12">
              <el-form-item label="招标编号" prop="ivtCode">
                <el-input v-model="form.ivtCode" placeholder="请输入招标编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预算金额" prop="ivtBudget">
                <el-input-number v-model="form.ivtBudget" :precision="2" style="width:100%" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="招标标题" prop="ivtTitle">
                <el-input v-model="form.ivtTitle" placeholder="请输入招标标题" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="招标状态" prop="ivtState">
                <el-input v-model="form.ivtState" placeholder="请输入招标状态" />
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <template v-else>
          <el-row>
            <el-col :span="12">
              <el-form-item label="投标单位" prop="bidUnit">
                <el-input v-model="form.bidUnit" placeholder="请输入投标单位" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投标金额" prop="bidAmount">
                <el-input-number v-model="form.bidAmount" :precision="2" style="width:100%" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="投标日期" prop="bidDate">
                <el-date-picker v-model="form.bidDate" type="date" placeholder="选择日期" style="width:100%" />
              </el-form-item>
            </el-col>
          </el-row>
        </template>
      </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="PmsBidding">
import { listBidCaller, getBidCaller, addBidCaller, updateBidCaller, delBidCaller, listBidTender, getBidTender, addBidTender, updateBidTender, delBidTender } from '@/api/pms/bidding'

const { proxy } = getCurrentInstance()
const activeTab = ref('caller')
const showSearch = ref(true)
const title = ref('')
const open = ref(false)

const callerLoading = ref(true)
const callerList = ref([])
const callerTotal = ref(0)
const callerIds = ref([])
const single = ref(true)
const multiple = ref(true)

const tenderLoading = ref(true)
const tenderList = ref([])
const tenderTotal = ref(0)
const tenderIds = ref([])
const tenderSingle = ref(true)
const tenderMultiple = ref(true)

const callerData = reactive({
  callerQueryParams: { pageNum: 1, pageSize: 10, ivtCode: undefined, ivtTitle: undefined }
})

const tenderData = reactive({
  tenderQueryParams: { pageNum: 1, pageSize: 10, bidUnit: undefined }
})

const formData = reactive({
  form: {},
  rules: {}
})

const { callerQueryParams } = toRefs(callerData)
const { tenderQueryParams } = toRefs(tenderData)
const { form, rules } = toRefs(formData)

function getCallerList() {
  callerLoading.value = true
  listBidCaller(callerQueryParams.value).then(response => {
    callerList.value = response.rows
    callerTotal.value = response.total
    callerLoading.value = false
  })
}

function getTenderList() {
  tenderLoading.value = true
  listBidTender(tenderQueryParams.value).then(response => {
    tenderList.value = response.rows
    tenderTotal.value = response.total
    tenderLoading.value = false
  })
}

function handleTabChange() {
  if (activeTab.value === 'caller') {
    getCallerList()
  } else {
    getTenderList()
  }
}

function handleCallerQuery() { callerQueryParams.value.pageNum = 1; getCallerList() }
function resetCallerQuery() { proxy.resetForm("callerQueryRef"); handleCallerQuery() }
function handleTenderQuery() { tenderQueryParams.value.pageNum = 1; getTenderList() }
function resetTenderQuery() { proxy.resetForm("tenderQueryRef"); handleTenderQuery() }

function handleCallerSelectionChange(selection) {
  callerIds.value = selection.map(item => item.ivtId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function handleTenderSelectionChange(selection) {
  tenderIds.value = selection.map(item => item.bidId)
  tenderSingle.value = selection.length !== 1
  tenderMultiple.value = !selection.length
}

function reset() {
  form.value = {}
  proxy.resetForm("formRef")
}

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

function handleCallerAdd() {
  reset()
  open.value = true
  title.value = '新增招标'
}

function handleCallerUpdate(row) {
  reset()
  const ivtId = row.ivtId || callerIds.value[0]
  getBidCaller(ivtId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改招标'
  })
}

function handleCallerDelete(row) {
  const ivtIds = row.ivtId || callerIds.value
  proxy.$modal.confirm('是否确认删除招标编号为"' + ivtIds + '"的数据项？').then(() => {
    return delBidCaller(ivtIds)
  }).then(() => {
    getCallerList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function handleTenderAdd() {
  reset()
  open.value = true
  title.value = '新增投标'
}

function handleTenderUpdate(row) {
  reset()
  const bidId = row.bidId || tenderIds.value[0]
  getBidTender(bidId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改投标'
  })
}

function handleTenderDelete(row) {
  const bidIds = row.bidId || tenderIds.value
  proxy.$modal.confirm('是否确认删除投标编号为"' + bidIds + '"的数据项？').then(() => {
    return delBidTender(bidIds)
  }).then(() => {
    getTenderList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (activeTab.value === 'caller') {
        if (form.value.ivtId) {
          updateBidCaller(form.value).then(() => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getCallerList()
          })
        } else {
          addBidCaller(form.value).then(() => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getCallerList()
          })
        }
      } else {
        if (form.value.bidId) {
          updateBidTender(form.value).then(() => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getTenderList()
          })
        } else {
          addBidTender(form.value).then(() => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getTenderList()
          })
        }
      }
    }
  })
}

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