<script setup name="projectIrsDoc">
import { h, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import ElTree from '@/components/elTree/index.vue'
import store from '@/store'
import { getIsShowRegionTree, getTreeParams } from '@/utils/getIsShowRegionTree'
import { list } from '@/http/apis/projectStoreManage/projectIrsDoc'

const
  router = useRouter(),
  { statusOptions, statusGjOptions, statusOptionsCascader, projectTypeOptions } = store.dictStore.globalDicts || {},
  searchForm = reactive({
    projectName: undefined,
    buildOrg: undefined,
    projectType: undefined,
    status: undefined,
    approveAmountMin: undefined,
    approveAmountMax: undefined,
    projectYear: undefined,
    times: undefined
  }),
  tableListRef = ref(),
  total = ref(0),
  // 列表数据
  column = reactive([
    {
      label: '序号',
      type: 'index',
      width: '60'
    },
    {
      label: '项目名称',
      key: 'projectName',
      prop: 'projectName',
      minWidth: '200',
      showOverflowTooltip: true
    },
    {
      label: '申报单位',
      key: 'buildOrgName',
      prop: 'buildOrgName',
      minWidth: '150',
      showOverflowTooltip: true
    },
    {
      label: '项目类型',
      key: 'projectTypeName',
      prop: 'projectTypeName',
      width: 80
    },
    {
      label: '预算年度',
      key: 'projectYear',
      prop: 'projectYear',
      width: '80'
    },
    {
      label: '项目状态',
      key: 'status',
      prop: 'status',
      width: '120',
      render: row => [
        h('span', {
          class: ['dot mr-4', `bg-${statusOptions[row.status]?.color || statusGjOptions[row.status]?.color}`]
        }),
        h(
          'span',
          {
            class: `text-${statusOptions[row.status]?.color || statusGjOptions[row.status]?.color}`
          },
          row.status && statusOptions[row.stage]?.name ? `${statusOptions[row.stage]?.name}-${statusOptions[row.status]?.name}` : statusGjOptions[row.status]?.name || '-'
        )
      ]
    },
    {
      label: '创建时间',
      key: 'createOn',
      prop: 'createOn',
      width: '180'
    },
    {
      label: '操作',
      slot: 'action',
      width: '80',
      fixed: 'right'
    }
  ]),
  data = ref([]),
  areaCode = ref(),
  getTree = (value) => {
    areaCode.value = value.regionLevel === 3 ? value.regionCode : undefined
    tableListRef.value.pageParams.pageNumber = 1
    getTableData()
  },
  getTableData = async (pageParams = tableListRef.value.pageParams) => {
    const res = await list({
      ...pageParams,
      ...searchForm,
      projectYear: searchForm.projectYear * 1 || undefined,
      createOnMin: searchForm.times?.[0] || undefined,
      createOnMax: searchForm.times?.[1] || undefined,
      regionCode: areaCode.value || undefined,
      approveAmountMin: searchForm.approveAmountMin * 1 || undefined,
      approveAmountMax: searchForm.approveAmountMax * 1 || undefined,
      times: undefined,
      status: searchForm.status?.[searchForm.status.length - 1]
    })
    data.value = res.data.records
    total.value = res.data.total
  },
  search = () => {
    getTableData()
  },
  reset = () => {
    searchForm.projectName = undefined
    searchForm.buildOrg = undefined
    searchForm.projectType = undefined
    searchForm.status = undefined
    searchForm.projectYear = undefined
    searchForm.approveAmountMin = undefined
    searchForm.approveAmountMax = undefined
    searchForm.times = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  checkDetail = (row) => {
    router.push({ name: 'projectIrsDocDetail', query: { id: row.id }})
  }
onMounted(async () => {
  if (!getIsShowRegionTree(['SUPER_ADMIN', 'REGION_MANAGER'])) getTableData()
})
</script>
<template>
  <el-row>
    <el-col
      v-if="getIsShowRegionTree(['SUPER_ADMIN','REGION_MANAGER'])"
      :span="4"
      class="pr-16"
    >
      <elTree :params="getTreeParams({'SUPER_ADMIN':false,'REGION_MANAGER':false})" @get-tree="getTree" />
    </el-col>
    <el-col :span="getIsShowRegionTree(['SUPER_ADMIN','REGION_MANAGER'])?20:24">
      <el-card class="w-full search">
        <el-form
          :model="searchForm"
          size="small"
          label-suffix=":"
        >
          <el-row
            :gutter="16"
            class="mb-16"
          >
            <el-col :span="8">
              <el-form-item label="项目名称">
                <el-input
                  v-model="searchForm.projectName"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="申报单位">
                <el-input
                  v-model="searchForm.buildOrg"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="项目类型">
                <el-select
                  v-model="searchForm.projectType"
                  placeholder="全部"
                  class="w-full"
                >
                  <el-option
                    v-for="(v,k) in projectTypeOptions"
                    :key="k"
                    :label="v"
                    :value="k"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row
            :gutter="16"
            class="mb-16"
          >
            <el-col :span="8">
              <el-form-item label="项目状态">
                <el-cascader
                  v-model="searchForm.status"
                  class="w-full"
                  :props="{label:'name',value:'code'}"
                  :options="statusOptionsCascader"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                class="whitespace-nowrap"
                label="批复资金(万元)"
              >
                <div class="flex">
                  <el-input
                    v-model="searchForm.approveAmountMin"
                    type="number"
                    placeholder=""
                    clearable
                  /> &nbsp;至&nbsp;
                  <el-input
                    v-model="searchForm.approveAmountMax"
                    type="number"
                    placeholder=""
                    clearable
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="预算年度">
                <el-date-picker
                  v-model="searchForm.projectYear"
                  type="year"
                  value-format="YYYY"
                  placeholder="请选择"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="16">
            <el-col :span="16">
              <el-form-item label="创建时间">
                <el-date-picker
                  v-model="searchForm.times"
                  type="datetimerange"
                  :editable="false"
                  format="YYYY-MM-DD HH:mm"
                  value-format="YYYY-MM-DD HH:mm"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="btn">
                <div class="flex">
                  <el-button
                    type="primary"
                    @click="search"
                  >查询
                  </el-button>
                  <el-button
                    @click="reset"
                  >重置
                  </el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="w-full mt-8">
        <template #header>
          <div class="flex justify-between">
            <span>列表</span>
          </div>
        </template>
        <table-list
          ref="tableListRef"
          :column="column"
          :data="data"
          :total="total"
          @get-table-data="getTableData"
        >
          <template #action="{ scope }">
            <a @click="checkDetail(scope.row)">详情</a>
          </template>
        </table-list>
      </el-card>
    </el-col>
  </el-row>
</template>
<style lang='less' scoped>
</style>
