<script setup lang="ts" name="QueryForm">
import { computed,ref,watch,onMounted} from 'vue'
import type { FormInstance } from 'element-plus'
import PageWrapperSearch from '@/components/search/PageWrapperSearch.vue'
import {
  Download
} from '@element-plus/icons-vue'
import { color } from 'echarts'
import timeRangeOptions from '@/constants/timeRangeOptions.js'
import { queryMenuTreeAndUserlist } from '@/api/logSet/operationLog.js'
import useAppStore from '@/store/modules/app'
const appStore = useAppStore()
// const { approve_status, invoice_status} = useDict('approve_status', 'invoice_status')
const emit = defineEmits(['update:modelValue', 'query', 'reset','handleAdd'])
const invoice_status_filter = ref([])
const employeesList = ref([])
const props = defineProps<{
  modelValue: any
}>()

const userOptions:any = ref([
  // { id: 'admin', realname: 'admin' },
  // { id: 'system', realname: 'system' },
  // { id: 'test', realname: 'test' }
]) 

const projectLevelOptions:any = ref([
  // { id: '1', project: '一级' },
  // { id: '2', project: '二级' },
  // { id: '3', project: '三级' }
])


const queryForm = computed({
  get() {
    return props.modelValue
  },
  set(val: any) {
    console.log('query computed', val)
    emit('update:modelValue', val)
  }
})

// 搜索
function onSearch() {
  emit('query')
}

// 重置
function onReset(formRef: FormInstance) {
  queryForm.value.user = undefined
  queryForm.value.range = '0'
  queryForm.value.projectId = undefined
  emit('reset', formRef)
}

function handleAdd() {
  emit('handleAdd')
}
onMounted(async () => {
  try {
    const response = await queryMenuTreeAndUserlist({ type: 'global' })
    console.log('queryMenuTreeAndUserlist', response)

    userOptions.value = response.data.userlist.map(user => ({
      id: user.id,
      user: user.realname
    }))
  } catch (error) {
    console.error('Failed to fetch user and module data:', error)
  }

  try {
    const params = {
      loginUser: {
        tsysUser: {
          id: appStore.userInfo.user.id,
          username: appStore.userInfo.user.username
        }
      },
      proName: '',
      note: ''
    }
    const projectResponse = await allProject(params)
    projectLevelOptions.value = projectResponse.data.map(project => ({
      id: project.id,
      project: project.project
    }))
  } catch (error) {
    console.error('Failed to fetch project level data:', error)
  }

})
</script>

<template>
  <!-- el-form -->
  <page-wrapper-search
    :model="queryForm"
    :extraButtons="[
      {
        text: '导出',
        icon: Download,
        color: 'rgba(0, 189, 207, 1)',
        type: 'success',
        class: 'btn-fff',
        onClick: handleAdd
      }
    ]"
    @search="onSearch"
    @reset="onReset">
    <el-form-item label="用户" prop="user">
          <el-select
            v-model="queryForm.user"
            placeholder="请选择"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in userOptions"
              :key="item.id"
              :label="item.user"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="时间" prop="range">
          <el-select
            v-model="queryForm.range"
            placeholder="请选择时间"
            style="width: 200px"
          >
            <el-option
              v-for="item in timeRangeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目级" prop="projectId">
          <el-select
            v-model="queryForm.projectId"
            placeholder="请选择项目级"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in projectLevelOptions"
              :key="item.id"
              :label="item.project"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
  </page-wrapper-search>



    <!-- <page-wrapper-search
      :model="queryForm"
      :extraButtons="[
        {
          text: '导出',
          icon: Edit,
          type: 'success',
          onClick: handleExport
        },
        {
          text: '打印',
          type: 'warning',
          onClick: handlePrint
        }
      ]"
      @search="onSearch"
      @reset="onReset">
      <el-form-item label="项目名称" prop="proName">
        <el-input
            v-model="queryForm.proName"
            placeholder="请输入项目名称"
            clearable
        />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
            v-model="queryForm.note"
            placeholder="请输入备注"
            clearable
        />
      </el-form-item>
    </page-wrapper-search> -->
</template>

<style scoped lang="scss">
:deep(.btn-fff){
  span{
    color: #fff;
  }
}
</style>
