<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="新闻标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.noticeTitle"
          placeholder="请输入新闻标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" placeholder="请选择新闻类型" clearable>
          <el-option
            v-for="dict in dict.type.noticetype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐私种类 " prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择隐私种类" clearable>
          <el-option
            v-for="dict in dict.type.type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发件人姓名" prop="senderName">
        <el-input
          v-model="queryParams.senderName"
          placeholder="请输入发件人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xiaoYuan:notice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xiaoYuan:notice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xiaoYuan:notice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xiaoYuan:notice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="新闻缩略图" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="defaultSettings.urls + '/prod-api'+ scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="新闻标题" align="center" prop="noticeTitle" />
      <el-table-column label="新闻类型" align="center" prop="noticeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.noticetype" :value="scope.row.noticeType"/>
        </template>
      </el-table-column>
      <el-table-column label="新闻内容" align="center" prop="noticeContent" width="120" show-overflow-tooltip>
		  <template slot-scope="scope">
		      <div v-html="scope.row.noticeContent"></div>
		  </template>
	 </el-table-column>
      <el-table-column label="新闻状态" align="center" prop="status" />
      <el-table-column label="隐私种类" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="发件人姓名" align="center" prop="senderName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xiaoYuan:notice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xiaoYuan:notice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改校园新闻对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
		  <el-row>
			  <el-col :span="24">
			  				<el-form-item label="新闻标题" prop="noticeTitle">
			  				  <el-input v-model="form.noticeTitle" placeholder="请输入新闻标题" />
			  				</el-form-item>  
			  </el-col>
			  <el-col :span="8">
			  				  <el-form-item label="班级名称" prop="senderDept">
			  				  <el-select v-model="form.senderDept" placeholder="请选择班级名称">
			  				    <el-option
			  				      v-for="dict in kiuesegh"
			  				      :key="dict.deptId"
			  				      :label="dict.deptName"
			  				      :value="dict.deptId"
			  				    ></el-option>
			  				  </el-select>
			  				  </el-form-item>
							  <!-- @click.native="mjyhess(dict)" -->
			  </el-col>
			  
			  <el-col :span="8">
			  	<el-form-item label="新闻类型" prop="noticeType">
			  	  <el-select v-model="form.noticeType" placeholder="请选择新闻类型">
			  	    <el-option
			  	      v-for="dict in dict.type.noticetype"
			  	      :key="dict.value"
			  	      :label="dict.label"
			  	      :value="dict.value"
			  	    ></el-option>
			  	  </el-select>
			  	</el-form-item>			  
			  </el-col>
			  <el-col :span="8">
			  		<el-form-item label="隐私种类 " prop="type">
			  		  <el-select v-model="form.type" placeholder="请选择隐私种类 0:全部 1:本班 2:仅自己">
			  		    <el-option
			  		      v-for="dict in dict.type.type"
			  		      :key="dict.value"
			  		      :label="dict.label"
			  		      :value="dict.value"
			  		    ></el-option>
			  		  </el-select>
			  		</el-form-item>		  
			  </el-col>
			  <el-col :span="24">
			  		<el-form-item label="新闻内容">
			  		  <editor v-model="form.noticeContent" :min-height="192"/>
			  		</el-form-item>		  
			  </el-col>
			  
			  <el-col :span="24">
			  		<el-form-item label="新闻缩略图" prop="image">
			  		  <image-upload v-model="form.image"/>
			  		</el-form-item>		  
			  </el-col>
			  <el-col :span="24">
			  		<el-form-item label="备注" prop="remark">
			  		  <el-input v-model="form.remark" placeholder="请输入备注" />
			  		</el-form-item>		  
			  </el-col>
		  </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/zhihuixy/notice";
import { listDept} from "@/api/system/dept";
const defaultSettings = require("@/settings.js");
export default {
  name: "Notice",
  dicts: ['noticetype', 'type'],
  data() {
    return {
      // 遮罩层
      loading: true,
	  defaultSettings:defaultSettings,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 校园新闻表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        image: null,
        noticeTitle: null,
        noticeType: null,
        noticeContent: null,
        status: null,
        type: null,
        senderId: null,
        senderName: null,
        senderDept: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
		  senderDept: [
		    { required: true, message: "新闻标题不能为空", trigger: "blur" }
		  ],
        noticeTitle: [
          { required: true, message: "新闻标题不能为空", trigger: "blur" }
        ],
        noticeType: [
          { required: true, message: "新闻类型不能为空", trigger: "change" }
        ],
        noticeContent: [
          { required: true, message: "新闻内容不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "新闻状态不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "隐私种类 0:全部 1:本班 2:仅自己不能为空", trigger: "change" }
        ],
      },
	  deptOptions:[],
	  kiuesegh:[],
	  kiueseghs:[]
    };
  },
  created() {
    this.getList();
	this.getdept(0)
  },
  methods: {
	  getdept(val){
	  		  let kuesf={'parentId':val}
	  		  // this.kiuesegh = []
	  		listDept(kuesf).then(response => {
	  		  this.deptOptions = this.handleTree(response.data, "deptId");
	  		  this.deptOptions.filter(rou =>{
	  			  if(rou.children != undefined){
	  			  	rou.children.filter(rour =>{
	  			  					  this.kiuesegh.push(rour)
	  			  	})		  
	  			  }
	  			  // this.kiuesegh = rou.children
	  			  // this.kiuesegh.push(rou.children)
	  			  console.log(rou.children)
	  		  })
	  		  this.kiueseghs = this.kiuesegh
	  		  // this.deptOptions = response.data
	  		  console.log(this.kiuesegh)
	  		});  
	  },
    /** 查询校园新闻列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: null,
        image: null,
        noticeTitle: null,
        noticeType: null,
        noticeContent: null,
        status: null,
        type: null,
        senderId: null,
        senderName: null,
        senderDept: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加校园新闻";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data;
		if(this.form.senderDept !== null){
		  this.form.senderDept   = this.form.senderDept.split(',')
		}
        this.open = true;
        this.title = "修改校园新闻";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
			 this.form.senderDept =  this.queryParams.dq.join(',')
          if (this.form.noticeId != null) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids;
	  delNotice(noticeIds).then(response => {
	   this.getList();
	   this.$modal.msgSuccess("删除成功");
	  });
      // this.$modal.confirm('是否确认删除校园新闻编号为"' + noticeIds + '"的数据项？').then(function() {
      //   return delNotice(noticeIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xiaoYuan/notice/export', {
        ...this.queryParams
      }, `notice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
