﻿<template>
	<div class="NCC-common-layout">
		<div class="NCC-common-layout-left">
			<div class="NCC-common-title">
				<h2>栏目</h2>
			</div>
			<el-tree :data="treeData" :props="treeProps" default-expand-all highlight-current ref="treeBox" :expand-on-click-node="false" @node-click="handleNodeClick" class="NCC-common-el-tree" node-key="id">
				<span class="custom-tree-node" slot-scope="{node,data}">
					<i :class="data.icon"></i>
					<span class="text">{{node.label}}</span>
				</span>
			</el-tree>
		</div>
		<div class="NCC-common-layout-center">
			<el-row class="NCC-common-search-box" :gutter="16">
				<el-form @submit.native.prevent>
					<el-col :span="6">
						<el-form-item label="标题">
							<el-input v-model="query.title" placeholder="标题" clearable />	
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="栏目">
							<el-select v-model="query.categoryId" placeholder="栏目" clearable >
								<el-option v-for="(item, index) in categoryIdOptions" :key="index" :label="item.fullName" :value="item.id"  />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item>
							<el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
							<el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<div class="NCC-common-layout-main NCC-flex-main">
				<div class="NCC-common-head">
					<div>
						<el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
						<el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()">批量删除</el-button>
					</div>
					<div class="NCC-common-head-right">
						<el-tooltip effect="dark" content="刷新" placement="top">
							<el-link icon="icon-ym icon-ym-Refresh NCC-common-head-icon" :underline="false" @click="reset()" />
						</el-tooltip>
						<screenfull :isContainer="true" />
					</div>
				</div>
				<NCC-table v-loading="listLoading" :data="list" @sort-change='sortChange' has-c @selection-change="handleSelectionChange">
					<el-table-column prop="title" label="标题" align="left" width="" />
					<el-table-column label="栏目" width="160" prop="categoryId" align="center">
							<template slot-scope="scope">{{ scope.row.categoryId | dynamicText(categoryIdOptions) }}</template>
					</el-table-column>
					<el-table-column prop="creater" label="创建人" align="center" width="120" />
                    <el-table-column prop="createTime" label="创建时间" align="center"  width="140">
                        <template slot-scope="scope" algin="center" >
                        <p class="text-grey">{{scope.row.createTime | toDate("yyyy-MM-dd HH:mm") }}</p>
                        </template>
                    </el-table-column>
                     <el-table-column label="状态" width="100">
                       <template slot-scope="scope">
                        <el-tag :type="scope.row.enabledMark == 1 ? 'success' : 'danger'" disable-transitions>
                          {{ scope.row.enabledMark==1?'已发送':'存草稿' }}
                      </el-tag>
                      </template>
                    </el-table-column>
					 <el-table-column label="操作" width="150">
                   <template slot-scope="scope">
                   <tableOpts @edit="handleAddEdit(scope.row.id)" @del="handleDel(scope.row.id)"
                          :editDisabled="scope.row.enabledMark != 0">
                    <el-dropdown v-if="scope.row.enabledMark == 0">
                    <el-button type="text" size="mini">
                            {{$t('common.moreBtn')}}<i class="el-icon-arrow-down el-icon--right" />
                    </el-button>
                     <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="handleView(scope.row.id)">
                          详情</el-dropdown-item>
                       <el-dropdown-item @click.native="handlePublish(scope.row.id)">发布
                      </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <el-button v-if="scope.row.enabledMark == 1" type="text" size="mini"
                  @click="handleView(scope.row.id)">详情</el-button>
              </tableOpts>
            </template>
          </el-table-column>
				</NCC-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
            </div>
        </div>
		<NCC-Form v-if="formVisible" ref="NCCForm" @refresh="refresh" />
		<ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
        <ViewNotice v-if="viewVisible" ref="View"  @refreshDataList="initData" />
	</div>
</template>
<script>
    import request from '@/utils/request'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    import NCCForm from './Form'
    import ExportBox from './ExportBox'
    import ViewNotice from './View'
    import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
        name: 'arcticeView',
        components: { NCCForm, ExportBox,ViewNotice },
        data() {
            return {
				query: {
                    title:undefined,
                    categoryId:undefined,
                    categoryId:undefined,
				},
				treeProps: {
                    children: 'children',
                    label: 'fullName',
					value: 'id',
                },
                list: [],
                listLoading: true,
                multipleSelection: [], total: 0,
                listQuery: {
                    currentPage: 1,
                    pageSize: 20,
					sort: "desc",
                    sidx: "",
                },
                formVisible: false,
                exportBoxVisible: false,
                viewVisible: false,
                treeData:[],
                columnList: [
                    { prop: 'title', label: '标题' },
                    { prop: 'categoryId', label: '栏目' },
                    { prop: 'isType', label: '类型' },
                    { prop: 'creater', label: '创建人' },
                    { prop: 'createTime', label: '创建时间' },
				],
				categoryIdOptions : [],
				isTypeOptions:[{"fullName":"普通文章","id":"0"},{"fullName":"幻灯片","id":"1"},{"fullName":"首页栏目","id":"2"}],
			}
        },
		computed: {},
		created() {
            this.getTreeView()
			this.getcategoryIdOptions();
		},
		methods: {
			getcategoryIdOptions(){
				getDictionaryDataSelector('f8b80c5a03a44fa0b42a8bbb0ff01d52').then(res => {
					this.categoryIdOptions = res.data.list
				});
			},
			sortChange({ column, prop, order }) {
                this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
                this.listQuery.sidx = !order ? '' : prop
                this.initData()
			},
			initData() {
                this.listLoading = true;
                let _query = {
                    ...this.listQuery,
                    ...this.query
                };
                let query = {}
                for (let key in _query) {
                    if (Array.isArray(_query[key])) {
                        query[key] = _query[key].join()
                    } else {
                        query[key] = _query[key]
                    }
                }
                request({
                    url: `/api/SubDev/ZyOaArticle`,
                    method: 'get',
                    data: query
                }).then(res => {
                    this.list = res.data.list
                    this.total = res.data.pagination.total
                    this.listLoading = false
                })
            },
			handleDel(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/SubDev/ZyOaArticle/${id}`,
                        method: 'DELETE'
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => {
                });
            },
			handleSelectionChange(val) {
                const res = val.map(item => item.id)
                this.multipleSelection = res
            },
            handleBatchRemoveDel() {
                if (!this.multipleSelection.length) {
                    this.$message({
                        type: 'error',
                        message: '请选择一条数据',
                        duration: 1500,
                    })
                    return
                }
                const ids = this.multipleSelection
                this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/SubDev/ZyOaArticle/batchRemove`,
                        method: 'POST',
                        data: ids ,
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => { })
            },
			addOrUpdateHandle(id, isDetail) {
                this.formVisible = true
                this.$nextTick(() => {
                    this.$refs.NCCForm.init(id, isDetail)
                })
            },
            handleAddEdit(id) {
                this.formVisible = true
                this.$nextTick(() => {
                this.$refs.NCCForm.init(id)
               })
            },
            handleView(id) {
               this.viewVisible = true
               this.$nextTick(() => {
               this.$refs.View.init(id)
               })
            },
            handlePublish(id) {
               this.$confirm('您确定要发布当前信息, 是否继续?', '提示', {
               type: 'warning'
            }).then(() => { 
               request({
                 url: `/api/SubDev/ZyOaArticle/${id}/Actions/Release`,
                 method: 'PUT'
             }).then(res => {
                this.$message({
                type: 'success',
                message: res.msg,
                duration: 1500,
                onClose: () => {
                  this.initData()
                }
               })
              })
            }).catch(() => { })
           },
            handleNodeClick(data){
			    this.treeActiveId = data.id
				for (let key in this.query) {
				    this.query[key] = undefined
				}
				this.query.categoryId = data.id
				this.listQuery = {
				    currentPage: 1,
					pageSize: 20,
					sort: "desc",
					sidx: "",
				}
				this.initData()
			},
			getTreeView() {
                getDictionaryDataSelector('f8b80c5a03a44fa0b42a8bbb0ff01d52').then(res => {
				    this.treeData = res.data.list
					this.initData()
				})
			},
			search() {
                this.listQuery = {
                    currentPage: 1,
                    pageSize: 20,
                    sort: "desc",
                    sidx: "createTime",
                }
                this.initData()
            },
            refresh(isrRefresh) {
                this.formVisible = false
                if (isrRefresh) this.reset()
            },
            reset() {
                for (let key in this.query) {
                    this.query[key] = undefined
                }
                this.listQuery = {
                    currentPage: 1,
                    pageSize: 20,
                    sort: "desc",
                    sidx: "createTime",
                }
                this.initData()
            }
		}
    }
</script>