<template>
  <fvSearchForm :searchConfig="articleSearchConfig" @search="searchArticle" searchProp="articleType" :searchValue="routeArticleType"
                style="margin-left: 16px"></fvSearchForm>
  <fvTable ref="tableIns" :tableConfig="articleTableConfig" @headBtnClick="headBtnClick">
    <template #empty>
      <el-empty description="暂无数据"/>
    </template>
  </fvTable>
</template>

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

const router = useRouter();
const route = useRoute();
const routeArticleType = ref(route.query.type)
const articleSearchConfig = reactive([
  {
    label: '文章标题',
    prop: 'articleTitle',
    component: 'el-input',
    props: {
      placeholder: '请输入文章标题查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '文章类型',
    prop: 'articleType',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择文章类型',
      clearable: true,
      filterable: true,
      checkStrictly: true,
      cacheKey: 'article_type',
      remote: true
    }
  },
  {
    label: '时间',
    prop: 'articleTime',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择时间',
      clearable: true,
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: "YYYY-MM-DD"
    },
    colProps: {}
  },
])
const tableIns = ref()
const articleTableConfig = 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: 'articleTitle',
      label: '文章标题',
      align: 'center'
    },
    {
      prop: 'articleType',
      label: '文章类型',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.articleType == undefined || row.articleType == null) {
          return '--'
        } else {
          return (<Tag dictType={'article_type'} value={row.articleType}/>)
        }
      }
    },
    {
      prop: 'articleTime',
      label: '时间',
      align: 'center'
    },
    {
      prop: 'remarks',
      label: '备注',
      align: 'center'
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      fixed: 'right',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        btn.push({label: '详情', func: () => handleDetail(row), type: 'primary'})
        btn.push({label: '编辑', prem: ['mosr:article:edit'], func: () => handleEdit(row), type: 'primary'})
        return (
            <div style={{width: '100%'}}>
              {

                btn.map(item => (
                    item.prem ?
                        <el-button
                            type={item.type}
                            v-perm={item.prem}
                            onClick={() => item.func()}
                            link
                        >
                          {item.label}
                        </el-button> :
                        <el-button
                            type={item.type}
                            onClick={() => item.func()}
                            link
                        >
                          {item.label}
                        </el-button>
                ))
              }
              {
                <popover-delete name={row.articleTitle} type={'文章'} btnType={'danger'} perm={['mosr:article:del']}
                                onDelete={() => handleDelete(row)}/>
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/article/list',
  btns: [
    {name: '新增', key: 'add', color: '#DED0B2', auth: ['mosr:article:add']}
  ],
  params: {}
})

const searchArticle = (val) => {
  let obj = {...val}
  if (obj.dateValue) {
    obj.startGenerationTime = obj.dateValue[0]
    obj.endGenerationTime = obj.dateValue[1]
    delete obj.dateValue
  }
  articleTableConfig.params = obj
  tableIns.value.refresh()
}
const init = async () => {
  if(routeArticleType.value){
    nextTick(()=>{
      console.info("🚀 ~method:articleSearchConfig -----", articleSearchConfig[1].component.props)
      searchArticle({articleType:routeArticleType.value})
    })
  }
}

init()

const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
  }
}
const handleAdd = () => {
  router.push({
    name: 'Article/add',
    query: {}
  })
}
const handleDetail = (row) => {
  router.push({
    name: 'Article/detail',
    query: {
      id: row.articleId
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Article/edit',
    query: {
      id: row.articleId
    }
  })
}
const handleDelete = (row) => {
  deleteArticle(row.articleId).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>
