<template>
  <div class="app-container">
    <div style="background-color: #fff;padding: 20px;border-radius: 10px;">
      <p style="margin: 0; font-weight: 800;font-size: 16px;color: #343434;display: flex;align-items: center;margin-bottom: 20px;">
        <img src="../../../assets/images/icon_htgl_btzs.png" alt="" style="width: 16px;height: 20px;">
       <span style="margin-left: 10px;">党建资讯</span>
      </p>
      <div style="background: #F3F3F3;padding: 20px 15px; padding-bottom: 0;margin-bottom: 20px;">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="党建资讯标题" prop="partyTitle">
                    <el-input
                      v-model="queryParams.partyTitle"
                      placeholder="请输入党建资讯标题"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
          </el-col>

          <el-col :span="8">
           <el-form-item label="党建资讯类型" prop="communityType">
                   <el-select v-model="queryParams.partyType" placeholder="请选择党建资讯类型" clearable>
                             <el-option
                               v-for="dict in dict.type.article_type"
                               :key="dict.value"
                               :label="dict.label"
                               :value="dict.value"
                             />
                </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="8">
           <el-form-item label="发布时间" prop="publishTime">
                   <el-date-picker clearable
                     v-model="queryParams.publishTime"
                     type="date"
                     value-format="yyyy-MM-dd"
                     placeholder="请选择发布时间">
                   </el-date-picker>
                 </el-form-item>

          </el-col>
          <el-col :span="8">
           <el-form-item label="党建资讯状态" prop="status">
                   <el-select v-model="queryParams.status" placeholder="请选择党建资讯状态" clearable>
                     <el-option
                       v-for="dict in dict.type.zinxdunt"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"
                     />
                   </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="butt">
              <el-button type="primary"   @click="handleQuery">搜索</el-button>
              <el-button   @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>

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

    <div style="background: #F1F6FF;border-radius: 6px;border: 1px solid #196CFF;padding: 0 42px; display: flex;justify-content: flex-start;margin-top: 10px;margin-bottom: 10px;">
       <p style="color: #8592AA;">本社区党员共 <i style="color: #0156FE; font-weight: 700;">{{sgsename}} </i>人</p>
    </div>

    <el-table :max-height="tableMaxHeight" v-loading="loading" :data="partyNewsList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="标题" align="center" prop="partyTitle" width="200" show-overflow-tooltip />
      <!-- <el-table-column label="内容" align="center" prop="partyContent"  width="180" show-overflow-tooltip>
      <template slot-scope="scope">
          <div v-html="scope.row.partyContent">
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="类型" align="center" prop="partyType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.article_type" :value="scope.row.partyType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" key="status" v-hasPermi="['wuYe:partyNews:remove']">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="2"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.publishTime}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="作者" align="center" prop="author" /> -->
      <el-table-column label="收藏数量" align="center" prop="starsCount" width="100" show-overflow-tooltip/>
      <el-table-column label="封面图片" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="浏览次数" align="center" prop="viewCount" />
      <el-table-column label="是否置顶" align="center" prop="isTop">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isTop"/>
              </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column width="120" label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wuYe:partyNews:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #46CB99;"
            @click="handleUpdatef(scope.row)"
          >查看</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wuYe:partyNews:remove']"
          >关闭</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </div>

    <!-- 添加或修改社区资讯对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="党建资讯标题" prop="partyTitle">
              <el-input v-model="form.partyTitle" placeholder="请输入党建资讯标题" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
           <el-form-item label="是否置顶" prop="isTop">
             <el-select v-model="form.isTop" placeholder="请选择是否置顶">
                         <el-option
                           v-for="dict in dict.type.sys_yes_no"
                           :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="partyType">
              <el-select v-model="form.partyType" placeholder="请选择党建资讯类型">
                <el-option
                  v-for="dict in dict.type.community_typ"
                  :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="source">
              <el-input v-model="form.source" placeholder="请输入来源，记录资讯的来源" />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker clearable
              style="width:100%"
                v-model="form.publishTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择发布时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="社区资讯内容">
              <editor v-model="form.partyContent" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="isshone">
            <el-form-item label="封面图片" prop="coverImage">
              <image-upload v-model="form.coverImage"/>
            </el-form-item>
          </el-col>
        </el-row>







        <!-- <el-form-item label="浏览次数，记录资讯被查看的次数" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入浏览次数，记录资讯被查看的次数" />
        </el-form-item> -->

        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="isshone">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartyNews, getPartyNews, delPartyNews, addPartyNews, updatePartyNews, count} from "@/api/wuguan/partyNews";

export default {
  name: "PartyNews",
  dicts: ['community_typ','sys_yes_no','article_type', 'publication_type','zinxdunt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 党建资讯表格数据
      partyNewsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        partyTitle: null,
        partyType: null,
        partyContent: null,
        status: null,
        publishTime: null,
        author: null,
        source: null,
        coverImage: null,
        viewCount: null,
        isTop: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partyTitle: [
            { required: true, message: "党建资讯标题不能为空", trigger: "blur" }
                ],
        partyType: [
             { required: true, message: "党建资讯类型不能为空", trigger: "change" }
        ],
      },
      isshone:false,
      systemTime:'',
      sgsename:0,
      tableMaxHeight:200
    };
  },
  created() {
    this.getList();
    this.getListnum()
    this.gettime()
  window.onresize = () => {
  	      this.changeTableMaxHeight()
  	    }
  	    this.changeTableMaxHeight()
  },
  mounted() {
  	window.onresize = () => {
  	          this.changeTableMaxHeight()
  	        }
  	        this.changeTableMaxHeight()
  },
  methods: {
    /** 查询社区资讯列表 */
    getListnum() {
      let snge={'residentAppearance':2}
            count(snge).then(response => {
              this.sgsename = response.data;
      });
    },
    /** 查询社区资讯列表 */
    getList() {
      this.loading = true;
            listPartyNews(this.queryParams).then(response => {
              this.partyNewsList = response.rows;
              this.total = response.total;
              this.loading = false;
      });
    },
    // 获取当前时间
    gettime(){
      let nowDate = new Date()
      				  let date = {
      				    year: nowDate.getFullYear(),
      				    month: nowDate.getMonth() + 1,
      				    date: nowDate.getDate()
      				  }

      				   if (parseInt(date.date) < 10) {
      				      date.date = '0' + date.date
      				    }
      				  this.systemTime = date.year + '-' + date.month + '-' + date.date

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
              partyId: null,
              partyTitle: null,
              partyType: null,
              partyContent: null,
              status: '1',
              publishTime: this.systemTime,
              author: null,
              source: null,
              coverImage: null,
              viewCount: null,
              isTop: 'N',
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              remark: null
            };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "1" ? "启用" : "停用";
      console.log(7)
      var that = this
      this.$modal.confirm('确认要"' + text + '"吗？').then(function() {
        console.log(5)
         that.getti(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        console.log(6)
        row.status = row.status === "1" ? "2" : "1";
      });
    },
    getti(row){
      console.log(3)
      updatePartyNews(row).then(response => {
        // this.$modal.msgSuccess("修改成功");
        // this.open = false;
        // this.getList();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加党建资讯";
      this.isshone = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
            const partyId = row.partyId || this.ids
            getPartyNews(partyId).then(response => {
              this.form = response.data;
              this.open = true;
              this.title = "修改党建资讯";
              this.isshone = true
      });
    },
    handleUpdatef(row){
      this.$router.push({
        path: '/wuguan/partyNewxq',
        query: {
        	'id':row.partyId,
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partyId != null) {
            updatePartyNews(this.form).then(response => {
                          this.$modal.msgSuccess("修改成功");
                          this.open = false;
                          this.getList();
           });
          } else {
            addPartyNews(this.form).then(response => {
                          this.$modal.msgSuccess("新增成功");
                          this.open = false;
                          this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
          const partyIds = row.partyId || this.ids;
          this.$modal.confirm('是否确认删除党建资讯编号为"' + partyIds + '"的数据项？').then(function() {
            return delPartyNews(partyIds);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
        },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wuYe/partyNews/export', {
              ...this.queryParams
      }, `partyNews_${new Date().getTime()}.xlsx`)
    },
    // 获取屏幕高度
    showFilterForm () {
          this.filterActive = !this.filterActive
          this.changeTableMaxHeight()
        },

        changeTableMaxHeight () {
          let height = document.body.offsetHeight // 网页可视区域高度
          // if (this.filterActive) {
          //   this.tableMaxHeight = height - 320
          // } else {
            this.tableMaxHeight = height - 150
          // }
          console.log(height)
        }
  }
};
</script>
<style  lang="scss">
  .butt{
    .el-button--small{
      padding: 9px 23px !important;
      font-size: 14px;
    }
  }
  .casr{
    .el-tabs__item.is-active{
      color: #196CFF;
    }
    .el-tabs--card > .el-tabs__header .el-tabs__item{
      padding: 0  30px !important;
    }
  }
  .el-table .el-table__header-wrapper th{
    background-color:#F5F6F8;
    color:#343434;
  }
  .el-table{
    color:#343434;
  }
</style>
<style scoped lang="scss">
  .goe{
    p{
      margin: 0;
      font-weight: 800;
      font-size: 16px;
      color: #161616;
      margin-top: 10px;
      margin-bottom: 3px;
      padding: 0 10px;
      overflow: hidden;
      text-overflow: ellipsis;
      display:-webkit-box; //作为弹性伸缩盒子模型显示。
      -webkit-box-orient:vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列
      -webkit-line-clamp:1; //显示的行
    }
    p:nth-child(2){
      // margin-bottom: 10px;
      margin-top: 0;
     font-weight: 400;
     font-size: 14px;
     color: #AAAAAA;
     display: flex;
     justify-content: space-between;
     align-items: center;
     padding-bottom: 10px;
     span:nth-child(2){
       padding: 3px 5px;
       background: #EDEDED;
       border-radius: 10px;
     }
    }
  }
  .ingwe{
    border: 2px solid #196CFF;
  }
  </style>
