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

<script setup lang="jsx">
const router = useRouter();
const searchConfig = reactive([
  {
    label: '项目名称',
    prop: 'projectName',
    component: 'el-input',
    props: {
      placeholder: '请输入项目名称查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '起始时间',
    prop: 'time',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择起始时间',
      clearable: true,
    },
    colProps: {}
  },
])
const tableIns = ref()
const tableConfig = reactive({
  columns: [
    {
      prop: 'shareName',
      label: '分摊名称',
      align: 'center'
    },
    {
      prop: 'apportionmentMonth',
      label: '分摊月份',
      align: 'center'
    },
    {
      prop: 'generationTime',
      label: '生成时间',
      align: 'center'
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => (<Tag dictType={'special_fund'} value={row.state}/>)
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons
        if(row.buttons){
          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("edit")) {
          btn.push({label: '编辑', prem: ['mosr:requirement:resubmit'], func: () => handleEdit(row), type: 'primary'})
        // }
        // if (buttons.has("delete")) {
        //   btn.push({label: '删除',prem: ['mosr:requirement:del'], func: () => handleDelete(row), type: 'primary'})
        // }
        // if (buttons.has("report")) {
          btn.push({label: '明细导出', prem: ['mosr:requirement:info'], func: () => handleReport(row), type: 'primary'})
        // }
        // if (buttons.has("report")) {
          btn.push({label: '汇总导出', prem: ['mosr:requirement:info'], func: () => handleReport(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:requirement:del']}
                                    onDelete={() => handleDelete(row)}/>
                // : ''
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/cost/allocation',
  btns: [
    {name: '添加分摊', key: 'add', color: '#DED0B2'}
  ],
  params: {}
})

const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}
const handleAdd = () => {
  router.push({
    name: 'Share/add',
    query: {}
  })
}
const handleDetail = (row) => {
  router.push({
    name: 'Share/detail',
    query: {
      id:row.id
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Share/edit',
    query: {
      id:row.id
    }
  })
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
  }
}
</script>

<style scoped>

</style>
