<script setup name="applicationListDialog">
import { h, reactive, ref, watch } from 'vue'
import store from '@/store'
import { addApplication, finalAppList } from '@/http/apis/performanceEvaluation/evaluationPlan'

const { digitalModifySystem } = store.dictStore.globalDicts || {},
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: {
      type: Array,
      default: undefined
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    }
  }),
  emits = defineEmits(['close', 'getAppData']),
  searchForm = reactive({
    projectName: undefined,
    buildOrg: undefined
  }),
  tableListRef = ref(),
  total = ref(0),
  column = [
    {
      type: 'selection',
      reserveSelection: true,
      width: '55'
    },
    {
      label: '应用名称',
      key: 'relatedExistsApplication',
      prop: 'relatedExistsApplication',
      showOverflowTooltip: true,
      width: 150,
      render: row => h('span', row.relatedExistsApplication || row.applicationName)
    },
    {
      label: '申报单位',
      key: 'buildOrgName',
      prop: 'buildOrgName',
      minWidth: '120',
      showOverflowTooltip: true
    },
    {
      label: '应用类型',
      key: 'applicationType',
      prop: 'applicationType',
      width: '150'
    },
    {
      label: '应用领域',
      key: 'digitalModification',
      prop: 'digitalModification',
      width: '140',
      render: row => h('span', row.digitalModification?.split(',')?.map(i => digitalModifySystem[i]).join('，'))
    },
    {
      label: '发布端',
      key: 'publishSide',
      prop: 'publishSide',
      width: '80'
    }
  ],
  tableData = ref([]),
  getTableData = async (pageParams = tableListRef.value?.pageParams) => {
    const res = await finalAppList({
      ...pageParams,
      ...searchForm,
      ...props.params
    })
    total.value = res.data.total
    tableData.value = res.data.records || []
    const selectCopyData = JSON.parse(JSON.stringify(selectData.value))
    if (selectCopyData?.length) {
      tableData.value && tableData.value.forEach(item => {
        const dataIds = selectCopyData.map(i => i.id)
        if (dataIds.includes(item.id)) {
          tableListRef.value.toggleRowSelect(item, true)
        }
      })
    }
  },
  search = () => {
    tableListRef.value.pageParams.pageNumber = 1
    getTableData()
  },
  reset = () => {
    searchForm.projectName = undefined
    searchForm.buildOrg = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  selectData = ref([]),
  selectionChange = val => {
    selectData.value = val
    const ids = val.map(i => i.id)
    tableData.value.forEach(row => {
      if (ids.includes(row.id)) {
        selectData.value.push(row)
      } else {
        selectData.value = selectData.value.filter(i => i.id !== row.id)
      }
    })
  },
  confirm = async () => {
    const obj = {}
    const data = selectData.value.reduce((cur, next) => {
      obj[next.id] ? '' : obj[next.id] = true && cur.push(next)
      return cur
    }, [])
    const postData = data.map(i => i.id)
    await addApplication({ applicationIds: postData, appraisalId: props.params.appraisalId })
    emits('getAppData', data)
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      if (props.data?.length) {
        selectData.value = props.data.map(i => {
          return {
            ...i,
            id: i.appId
          }
        })
      }
      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.applicationName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申报单位">
              <el-input v-model="searchForm.buildUnitName" 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"
      :total="total"
      row-key="id"
      @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>
