<script setup name="planProjectDialog">
import { h, reactive, ref, watch } from 'vue'
import { planProjectList } from '@/http/apis/performanceEvaluation/evaluationPlan'
import { useRoute } from 'vue-router'
import store from '@/store'

const {
    projectTypeOptions
  } = store.dictStore.globalDicts || {},
  route = useRoute(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: {
      type: Array,
      default: undefined
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    },
    templateTypeListData: {
      type: Array,
      default: undefined
    }
  }),
  emits = defineEmits(['close', 'getProjectData']),
  searchForm = reactive({
    projectName: undefined,
    buildOrgName: undefined
  }),
  tableListRef = ref(),
  column = [
    {
      type: 'selection',
      reserveSelection: true,
      width: '55'
    },
    {
      label: '项目名称',
      key: 'projectName',
      prop: 'projectName',
      minWidth: '200',
      showOverflowTooltip: true
    },
    {
      label: '预算年度',
      key: 'projectYear',
      prop: 'projectYear',
      width: 80
    },
    {
      label: '项目金额',
      key: 'declareAmount',
      prop: 'declareAmount'
    },
    {
      label: '申报单位',
      key: 'buildOrgName',
      prop: 'buildOrgName'
    },
    {
      label: '项目类型',
      key: 'projectType',
      prop: 'projectType',
      render: row => h('span', projectTypeOptions[row.projectType] || '-'),
      width: 80
    },
    {
      label: '模板类型',
      key: 'templateType',
      prop: 'templateType',
      render: row => h('span', props.templateTypeListData?.find(i => i.id === row.templateType)?.name || '-')
    }
    // {
    //   label: '是否评价',
    //   key: 'isAppraisal',
    //   prop: 'isAppraisal',
    //   render: row => h('span', row.isAppraisal ? '是' : '否'),
    //   width: 80
    // },
    // {
    //   label: '是否复评',
    //   key: 'isReAppraisal',
    //   prop: 'isReAppraisal',
    //   render: row => h('span', row.isReAppraisal ? '是' : '否'),
    //   width: 80
    // }
  ],
  tableData = ref([]),
  total = ref(0),
  getTableData = async (pageParams = tableListRef.value?.pageParams) => {
    const res = await planProjectList({
      ...pageParams,
      ...searchForm,
      appraisalId: route.query.appraisalId
    })
    tableData.value = res.data.records
    total.value = res.data.total
    if (searchForm.projectName) {
      tableData.value = tableData.value.filter(i => i.projectName.indexOf(searchForm.projectName) !== -1)
    }
    if (searchForm.buildOrgName) {
      tableData.value = tableData.value.filter(i => i.buildOrgName.indexOf(searchForm.buildOrgName) !== -1)
    }
    const selectCopyData = JSON.parse(JSON.stringify(selectData.value)) || []
    if (selectCopyData?.length) {
      tableData.value && tableData.value.forEach(item => {
        const dataIds = selectCopyData.map(i => i.projectCode)
        if (dataIds.includes(item.projectCode)) {
          tableListRef.value.toggleRowSelect(item, true)
        }
      })
    }
  },
  search = () => {
    tableListRef.value.pageParams.pageNumber = 1
    getTableData()
  },
  reset = () => {
    searchForm.projectName = undefined
    searchForm.buildOrgName = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  selectData = ref([]),
  selectionChange = val => {
    const ids = val.map(i => i.projectCode)
    tableData.value.forEach(row => {
      if (ids.includes(row.projectCode)) {
        selectData.value.push(row)
      } else {
        selectData.value = selectData.value.filter(i => i.projectCode !== row.projectCode)
      }
    })
  },
  confirm = () => {
    const obj = {}
    const data = selectData.value.reduce((cur, next) => {
      obj[next.projectCode] ? '' : obj[next.projectCode] = true && cur.push(next)
      return cur
    }, [])
    emits('getProjectData', data)
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      if (props.data?.length) {
        selectData.value = props.data.map(i => i)
        console.log(selectData.value)
      }
      getTableData()
    }
  }
)
</script>
<template>
  <el-dialog
    :model-value="visible"
    title="添加项目"
    width="70%"
    destroy-on-close
    @close="emits('close')"
  >
    <div class="search mb-16">
      <el-form label-suffix=":" :model="searchForm" size="small">
        <el-row :gutter="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.buildOrgName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="btn">
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <table-list
      ref="tableListRef"
      :column="column"
      :data="tableData"
      row-key="projectCode"
      :total="total"
      @selection-change="selectionChange"
      @get-table-data="getTableData"
    />
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('close')">关闭</el-button>
        <el-button
          type="primary"
          @click="confirm"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
