<template>
  <fvSearchForm :searchConfig="searchConfig" @search="search"></fvSearchForm>
  <fvTable ref="tableIns" :tableConfig="tableConfig" @headBtnClick="headBtnClick"></fvTable>
</template>

<script setup lang="jsx">
import fvSelect from '@/fvcomponents/fvSelect/index.vue'

const tableIns = ref()
const router = useRouter()
const shortcuts = [
  {
    text: '上周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '上月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '三月前',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
const searchConfig = reactive([
  {
    label: '征集名称',
    prop: 'requirementName',
    props: {
      placeholder: '请输入征集名称',
      clearable: true,
      checkStrictly: true
    },
    component: 'el-input',
    colProps: {}
  },
  {
    label: '项目类型',
    prop: 'projectType',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择项目类型',
      cacheKey: 'project_type',
      clearable: true,
      filterable: true,
    },
    colProps: {}
  },
  {
    label: '项目影响',
    prop: 'projectImpact',
    component: shallowRef(fvSelect),
    props: {
      cacheKey: 'project_impact',
      placeholder: '请选择项目影响',
      clearable: true,
      filterable: true,
    },
    colProps: {}
  },
  {
    label: '研发主体',
    prop: 'rdSubject',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择研发主体',
      cacheKey: 'rd_subject',
      clearable: true,
      filterable: true,
    }
  },
  {
    label: '起止时间',
    prop: 'dateValue',
    component: 'el-date-picker',
    props: {
      clearable: true,
      type: 'daterange',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      shortcuts: shortcuts
    }
  },
  {
    label: '状态',
    prop: 'state',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择状态',
      clearable: true,
      cacheKey: 'project_initiation'
    }
  },
  // {
  //   label: '项目影响',
  //   prop: 'projectImpact',
  //   component: shallowRef(fvSelect),
  //   props: {
  //     placeholder: '请选择项目影响',
  //     cacheKey: 'project_impact',
  //     clearable: true,
  //     filterable: true
  //   },
  //   colProps: {}
  // },
  // {
  //   label: '起止时间',
  //   prop: 'startTime',
  //   component: 'el-date-picker',
  //   props: {},
  //   colProps: {}
  // },
  // {
  //   label: '最小金额',
  //   prop: 'minMoney',
  //   component: 'el-input',
  //   colProps: {}
  // },
  // {
  //   label: '最大金额',
  //   prop: 'maxMoney',
  //   component: 'el-input',
  //   colProps: {}
  // },
])
const auths = {
  edit: ['mosr:collect:resubmit'],
  detail: ['mosr:collect:info'],
  report: ['mosr:collect:reported'],
}
const tableConfig = reactive({
  columns: [
    // {
    //   type: 'selection',
    //   prop: 'selection'
    // },
    {
      prop: 'requirementName',
      label: '征集名称',
      align: 'center'
    },
    {
      prop: 'projectName',
      label: '项目名称',
      align: 'center'
    },
    {
      prop: 'undertaker',
      label: '承办单位',
      align: 'center'
    },
    {
      prop: 'projectType',
      label: '项目类型',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.projectType !== null) {
          return (<Tag dictType={'project_type'} value={row.projectType}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'rdSubject',
      label: '研发主体',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.rdSubject !== null) {
          return (<Tag dictType={'rd_subject'} value={row.rdSubject}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'projectImpact',
      label: '项目影响',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.projectImpact !== null) {
          return (<Tag dictType={'project_impact'} value={row.projectImpact}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'startTime',
      label: '起止时间',
      align: 'center',
      currentRender: ({row}) => {
        return row.startTime + ' 至 ' + row.endTime
      }
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.state !== null && row.state != 0) {
          return (<Tag dictType={'demand_summary'} value={row.state}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      fixed:'right',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons = new Set(Array.from(row.buttons))
        if (buttons.has("details")) {
          btn.push({label: '详情', prem: auths.detail, func: () => handleDetail(row), type: 'primary'})
        }
        if (buttons.has("edit")) {
          btn.push({label: '编辑', prem: auths.edit, func: () => handleEdit(row), type: 'primary'})
        }
        return (
            <div style={{width: '100%'}}>
              {
                btn.map(item => (
                    <el-button
                        type={item.type}
                        v-perm={item.prem}
                        onClick={() => item.func()}
                        link
                    >
                      {item.label}
                    </el-button>
                ))
              }
              {
                buttons.has("delete") ?
                    <popover-delete name={row.requirementName} type={'需求'} btnType={'danger'}
                                    perm={['mosr:collect:del']} onDelete={() => handleDelete(row)}/>
                    : ''
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/requirement/collect',
  params: {},
  btns: [
    {name: '新增上报', key: 'add', color: '#DED0B2', auth:auths.report},
    // {name: '年度计划导出', key: '_export', auth: ''},
    // {name: '经费预算生成', key: 'preMonty', auth: ''},
  ]
})
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
  }
}

const search = (val) => {
  let obj = {...val}
  if (obj.dateValue) {
    obj.startTime = obj.dateValue[0]
    obj.endTime = obj.dateValue[1]
    delete obj.dateValue
  }
  tableConfig.params = obj
  tableIns.value.refresh()
}

const handleAdd = () => {
  router.push({
    name: 'Summary/add',
    query: {
      // id: row.requirementId
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Summary/edit',
    query: {
      // requirementId: row.requirementId,
      projectId: row.projectId
    }
  })
}
const handleDetail = (row) => {
  router.push({
    name: 'Summary/detail',
    query: {
      id: row.requirementId==null?'-1':row.requirementId,
      projectId: row.projectId,
      state: row.state
    }
  })
}
</script>

<style lang="scss" scoped>

</style>
