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

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

const router = useRouter()
const searchConfig = reactive([
  {
    label: '需求名称',
    prop: 'requirementName',
    component: 'el-input',
    props: {
      placeholder: '请输入名称查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '征集类型',
    prop: 'collectType',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择征集类型',
      clearable: true,
      filterable: true,
      cacheKey: 'todo_type'
    }
  }
])
const tableIns = ref()
const tableConfig = reactive({
  columns: [
    {
      type: 'selection',
      prop: 'selection'
    },
    {
      prop: 'requirementName',
      label: '需求名称',
      align: 'center'
    },
    {
      prop: 'collectType',
      label: '征集类型',
      align: 'center'
    },
    {
      prop: 'approveName',
      label: '审批人',
      align: 'center'
    },
    {
      prop: 'deadline',
      label: '截止时间',
      align: 'center'
    },
    {
      prop: 'taskNode',
      label: '当前节点',
      align: 'center'
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      width: 200,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => (<Tag dictType={'process_state'} value={row.state}/>)
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = [{label: '详情', func: () => handleDetail(row), type: 'primary'}]
        if (row.state === '3' || row.state === '2') {
          btn.push({label: '编辑', func: () => handleEdit(row), type: 'primary'})
          btn.push({label: '删除', func: () => handleDelete(row), type: 'danger'})
        } else if (row.state === '4') {
          btn.push({label: '上报', func: () => handleReport(row), type: 'primary'})
        }
        return (
            <div style={{width: '100%'}}>
              {
                btn.map(item => (
                    <el-button
                        type={item.type}
                        // v-perm={item.auth}
                        onClick={() => item.func()}
                        link
                    >
                      {item.label}
                    </el-button>
                ))
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/requirement',
  btns: [
    {name: '新增', key: 'add', color: '#DED0B2'},
    {name: '导出', key: 'add', type: ''},
  ],
  params: {}
})

const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}
const handleAdd = () => {
  router.push({
    path: '/projectdemand/demandadd',
    query: {
      isAdd: 1
    }
  })
}
const handleEdit = (row) => {
  router.push({
    path: '/projectdemand/demandedit',
    query: {
      id: row.requirementId
    }
  })
}
const handleDetail = (row) => {
  router.push({
    path: '/projectdemand/demanddetail',
    query: {
      id: row.requirementId
    }
  })
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
    case 'edit':
      handleEdit()
      break;
    case 'detail':
      handleDetail()
      break;
  }
}
</script>

<style lang="scss" scoped>

</style>
