<!--信息发布-->
<template>
  <div class="app-container content">
    <div class="content-page" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="发送对象" prop="messClass">
          <el-select v-model="queryParams.messClass" clearable placeholder="请选择发送范围" size="small">
            <!-- <el-option label="请选择发送范围" value="" /> -->
            <el-option label="人员" value=1 />
            <el-option label="实验室" value=3 />
          </el-select>
        </el-form-item>
        <el-form-item label="发送人" prop="sendName">
          <el-input
            clearable
            v-model="queryParams.sendName"
            placeholder="请输入发送人"
            size="small"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="dateRange" style="margin-left:10px;">
          <el-date-picker
            :clearable="false"
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item >
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="contentList" @selection-change="handleSelectionChange">
        <el-table-column label="发送人" align="left" prop="sendName" />
        <el-table-column label="发送对象" align="left" prop="messClass">
          <template slot-scope="scope">{{scope.row.messClass==1?'人员':(scope.row.messClass==3?'实验室':'')}}</template>
        </el-table-column>
        <el-table-column label="发送时间" align="left" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box">
              <p class="table-min-button"
                 @click="clickPage(2,scope.row)"
                 v-hasPermi="['laboratory:content:query']"
              >查看</p>
              <p class="table-min-button"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['laboratory:content:remove']"
              >删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        layout="total, prev, pager, next, sizes, jumper"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <info-page v-if="pageType == 2" :infoData="infoData"></info-page>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent, contentInfo } from "@/api/laboratory/content";
import { selectListUser } from "@/api/system/user";
import { treeselect as deptTreeselect} from "@/api/system/dept";
import infoPage from "./infoPage.vue"
export default {
  name: "Content",
  components: {
    infoPage
  },
  data() {
    var validateDepts = (rule, value, callback) => {
      let arr = this.$refs.dept.getCheckedKeys()
      if (arr.length == 0 || !arr) {
        callback(new Error("请选择学院"));
      } else {
        callback();
      }
    };
    return {
      tableButtonType:this.hasPermiDom(['laboratory:content:query','laboratory:content:remove']),
      pageType:1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息发布表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openTwo:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messType: null,
        sendMode: null,
        sendRange: null,
        content: null,
        deptId: null,
        deptName: null,
        userIds: null,
        userId: null,
      },
      dateRange:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          sendRange:[
            {required: true, message: '请选择发送范围', trigger: 'change'}
          ],
          deptName:[
            {required: true,  validator: validateDepts, trigger: 'change'}
          ],
          userIds:[
             {required: true, message: '请选择人员', trigger: 'blur'}
          ],
          content:[
             {required: true, message: '请输入发送内容', trigger: 'blur'},
            { required: true, message: "请输入发送内容", validator: this.spaceJudgmentHTML, trigger: "blur" }
          ]
      },
      //选择部门
      optionsDepartment: [],
      //选择人员
      optionsUser: [],
      //人员下拉查询条件
      queryUserListParams : {
        nickName : undefined
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      infoData:{},
    };
  },
  created() {
    this.getList();
    this.getDeptTreeselect();
  },
  methods: {
    clickPage(type,row){
      if(this.pageType != type){
        if(type == 1){
          this.getList();
          this.pageType = 1;
        }else if(type == 2){
          contentInfo(row.id).then(response => {
            this.infoData = response.data;
            let text = ""
            if(response.data.deptName){
              text = text + response.data.deptName
            }
            if(response.data.postName){
              text = text + response.data.postName
            }
            if(response.data.userNames){
              text = text + response.data.userNames
            }
            this.infoData.typeText = text;
            this.pageType = 2
          });
        }
      }
    },
    /** 查询消息发布列表 */
    getList() {
      this.loading = true;
      if(this.dateRange&&this.dateRange.length>0) {
        this.queryParams.startDate=this.dateRange[0]
        this.queryParams.endDate=this.dateRange[1]
      } else {
        this.queryParams.startDate=null;
        this.queryParams.endDate=null
      }
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        messType: null,
        sendMode: null,
        sendRange: null,
        content: null,
        deptIds: null,
        deptName: null,
        userIds: null,
        createTime: null,
        userId: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize: 10,
        messClass:"",
        sendName: '',
      });
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "消息发布";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息发布";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.sendRange==2)
          {
            this.form.deptIds = this.getDeptAllCheckedKeys().join();
          }else if(this.form.sendRange==3)
          {
            this.form.userIds= this.form.userIds.join();
          }

          if (this.form.id != null) {
            this.form.content = escape(this.form.content);
            console.log(this.form.content);
            updateContent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.content = escape(this.form.content);
            console.log(this.form.content);
            addContent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            this.form.content = unescape(this.form.content);
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/content/export', {
        ...this.queryParams
      }, `laboratory_content.xlsx`)
    },
    /** 下列人员-懒加载 */
    userSelectList(query) {
        if (query !== '' && query.length>1) {
          this.loading = true;
          this.userSelectList.nickName=query;
          selectListUser(this.userSelectList).then(response => {
            this.optionsUser = response.data;
            this.loading = false;
          });
        } else {
          this.optionsUser = [];
        }
      }
      ,
    // 树权限（父子联动）
    handleCheckedTreeConnect(value) {
       this.form.deptCheckStrictly = value ? true: false;
    },
    /** 查询部门树结构 */
    getDeptTreeselect() {
      deptTreeselect().then(response => {
        this.optionsDepartment = response.data;
      });
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.dept.getCheckedKeys();
      // 半选中的部门节点
      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
  }
};
</script>

<style scoped lang="scss">
  .content {
    display: flex!important;
    flex-direction: column;
    overflow: hidden;
    .content-page{
      padding:20px 20px 0 20px!important;
      flex:1;
      display: flex!important;
      flex-direction: column;
      overflow: hidden;
      .pagination-container{
        height:50px;
      }
    }
    .el-row{
      margin-bottom:20px;
    }
    .button-box{
      display: flex;
      p:nth-child(1){
        margin-right:20px;
      }
    }
  }
</style>
