<template>
  <fvSearchForm :searchConfig="searchConfig" @search="search"></fvSearchForm>
  <fvTable ref="tableIns" :tableConfig="tableConfig">
    <template #empty>
      <el-empty description="暂无数据"/>
    </template>
  </fvTable>
</template>

<script setup lang="jsx">
import fvSelect from '@/fvcomponents/fvSelect/index.vue'
import {reactive, shallowRef} from "vue";

const router = useRouter()
const searchConfig = reactive([
  {
    label: '名称',
    prop: 'projectName',
    component: 'el-input',
    props: {
      placeholder: '请输入名称查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '项目类型',
    prop: 'projectType',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择项目类型',
      clearable: true,
      filterable: true,
    }
  },
  {
    label: '项目影响',
    prop: 'projectImpact',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择项目影响',
      clearable: true,
      filterable: true,
    },
    colProps: {}
  },
  {
    label: '研发主体',
    prop: 'rdSubject',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择研发主体',
      clearable: true,
      filterable: true,
    }
  },

  {
    label: '起止时间',
    prop: 'startTime',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择起止时间',
      clearable: true,
    },
    colProps: {}
  },
  {
    label: '最小金额',
    prop: 'requirementName',
    component: 'el-input',
    props: {
      placeholder: '请输入金额查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '最大金额',
    prop: 'requirementName',
    component: 'el-input',
    props: {
      placeholder: '请输入金额查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
])
const tableIns = ref()
const tableConfig = reactive({
  columns: [
    {
      type: 'selection',
      prop: 'selection'
    },
    {
      prop: 'projectName',
      label: '名称',
      align: 'center'
    },
    {
      prop: 'affiliatedCompany',
      label: '所属公司',
      align: 'center'
    },
    {
      prop: 'approveName',
      label: '审批人',
      align: 'center'
    },
    {
      prop: 'projectType',
      label: '项目类型',
      align: 'center'
    },
    {
      prop: 'rdSubject',
      label: '研发主体',
      align: 'center'
    },
    {
      prop: 'projectImpact',
      label: '项目影响',
      align: 'center'
    },
    {
      prop: 'economicEstimate',
      label: '经济概况',
      align: 'center'
    },
    {
      prop: 'startTime',
      label: '起止时间',
      align: 'center'
    },
    {
      prop: 'taskNode',
      label: '当前节点',
      align: 'center'
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.state !== null) {
          return (<Tag dictType={'project_initiation'} value={row.state}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons = new Set(Array.from(row.buttons))
        if (buttons.has("details")) {
          btn.push({label: '详情', prem: ['mosr:requirement:info'], func: () => handleDetail(row), type: 'primary'})
        }
        if (buttons.has("check")) {
          btn.push({label: '验收', prem: ['mosr:requirement:resubmit'], func: () => handleCheck(row), type: 'primary'})
        }
        if (buttons.has("edit")) {
        btn.push({label: '编辑', prem: ['mosr:requirement:info'], func: () => handleEdit(row), type: 'primary'})
        }
        // if (buttons.has("standing")) {
          btn.push({label: '台账', prem: ['mosr:requirement:info'], func: () => handleStandingBook(row), type: 'primary'})
        // }
        // if (buttons.has("attachments")) {
        btn.push({label: '附件', prem: ['mosr:requirement:info'], func: () => handleAttachment(row), type: 'primary'})
        // }
        if (buttons.has("viewAllocation")) {
          btn.push({label: '查看分摊', prem: ['mosr:requirement:info'], func: () => handleShare(row), type: 'primary'})
        }
        if (buttons.has("phaseChange")) {
          btn.push({label: '阶段变更', prem: ['mosr:requirement:info'], func: () => handlePhaseChange(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>
                ))
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/project/implementation',
  params: {},
  btns: [
    {name: '生成分摊报表', key: '_export', color: '#DED0B2', auth: ''}
  ]
})

const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}

const handleDetail = (row) => {
  router.push({
    name: 'Implementation/detail',
    query: {
      id: row.requirementId,
      projectId: row.projectId,
      state: row.state,
      step: '40'
    }
  })
}
const handleCheck = (row) => {
  router.push({
    name: 'Implementation/check',
    query: {
      id: row.requirementId,
      projectId: row.projectId
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Implementation/edit',
    query: {
      id: row.requirementId,
      projectId: row.projectId
    }
  })
}
const handleStandingBook = (row) => {
  router.push({
    name: 'Implementation/account',
    query: {
      id: row.projectId
    }
  })
}
const handleAttachment = (row) => {
  router.push({
    name: 'Implementation/attachment',
    query: {
      id: row.projectId
    }
  })
}
const handleShare = (row) => {
  router.push({
    name: 'Implementation/share',
    query: {
      id: row.requirementId
    }
  })
}
const handlePhaseChange = (row) => {
  router.push({
    name: 'Phase/change',
    query: {
      id: row.requirementId
    }
  })
}
</script>
