<template>
  <fvSearchForm :searchConfig="researchFundSearchConfig" @search="searchResearchFund"
                style="margin-left: 16px"></fvSearchForm>
  <fvTable ref="tableIns" :tableConfig="researchFundTableConfig" @headBtnClick="headBtnClick">
    <template #empty>
      <el-empty description="暂无数据"/>
    </template>
  </fvTable>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {deleteAllocation} from "@/api/expense-manage";
import fvSelect from '@/fvcomponents/fvSelect/index.vue'

const router = useRouter();
const researchFundSearchConfig = reactive([
  {
    label: '文章标题',
    prop: 'projectName',
    component: 'el-input',
    props: {
      placeholder: '请输入文章标题查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '文章类型',
    prop: 'state',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择文章类型',
      clearable: true,
      filterable: true,
      checkStrictly: true,
      cacheKey: 'article_type',
      remote: true
    }
  },
  {
    label: '时间',
    prop: 'apportionmentMonth',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择时间',
      clearable: true,
      type: 'month',
      format: 'YYYY-MM',
      valueFormat: "YYYY-MM"
    },
    colProps: {}
  },
  {
    label: '阅读状态',
    prop: 'state',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择阅读状态',
      clearable: true,
      filterable: true,
      checkStrictly: true,
      cacheKey: 'read_state',
      remote: true
    }
  },
])
const tableIns = ref()
const researchFundTableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: 85,
      index: index => {
        return (tableIns.value.getQuery().pageNum - 1) * tableIns.value.getQuery().pageSize + index + 1
      }
    },
    {
      prop: 'shareName',
      label: '文章标题',
      align: 'center'
    },
    {
      prop: 'state',
      label: '文章类型',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.state == undefined || row.state == 0) {
          return '--'
        } else {
          return (<Tag dictType={'article_type'} value={row.state}/>)
        }
      }
    },
    {
      prop: 'state',
      label: '阅读状态',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.state == undefined || row.state == 0) {
          return '--'
        } else {
          return (<Tag dictType={'read_state'} value={row.state}/>)
        }
      }
    },
    {
      prop: 'apportionmentMonth',
      label: '时间',
      align: 'center'
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      fixed: 'right',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons
        if (row.buttons) {
          buttons = new Set(Array.from(row.buttons))
        }
        btn.push({label: '详情', prem: ['mosr:requirement:info'], func: () => handleDetail(row), type: 'primary'})
        btn.push({label: '编辑', prem: ['mosr:requirement:resubmit'], 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>
                ))
              }
              {
                <popover-delete name={row.shareName} type={'研发投入资金'} btnType={'danger'}
                                onDelete={() => handleDelete(row)}/>

              }
            </div>
        )
      }
    }
  ],
  api: '',
  btns: [
    {name: '新增', key: 'add', color: '#DED0B2'}
  ],
  params: {}
})
const searchResearchFund = (val) => {
  let obj = {...val}
  if (obj.dateValue) {
    obj.startGenerationTime = obj.dateValue[0]
    obj.endGenerationTime = obj.dateValue[1]
    delete obj.dateValue
  }
  researchFundTableConfig.params = obj
  tableIns.value.refresh()
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
  }
}
const handleAdd = () => {
  router.push({
    name: 'Share/add',
    query: {}
  })
}
const handleDetail = (row) => {
  router.push({
    name: 'Share/detail',
    query: {
      id: row.allocationId
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Share/edit',
    query: {
      id: row.allocationId
    }
  })
}
const handleDelete = (row) => {
  deleteAllocation(row.allocationId).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tableIns.value.refresh()
    }
  });
}
</script>

<style scoped lang="scss">
:deep(.el-table__header) {
  .is-leaf:first-child {
    .cell {
      margin-left: -25px !important;
    }
  }
}

:deep(.el-table__body) {
  .el-table__cell:first-child {
    .cell {
      margin-left: -13px !important;
    }
  }
}

:deep(.el-date-editor--month) {
  width: 100%;
}
</style>
