<!-- 新闻管理 by wangshxc -->
<template>
  <div>
    <div class="tab-item">
      <div class="tab-button"
           align="left">
        <ws-button class="tab-kuan"
                   type="primary"
                   round
                   @click="addGoing"> 新增</ws-button>
        <ws-button class="tab-kuan"
                   type="primary"
                   round
                   @click="setNewType"> 设置类型</ws-button>
      </div>
      <ws-dialog title="设置类型"
                 :visible.sync="setDialogFormVisible"
                 width="550px"
                 center
                 :close-on-click-modal="false">
        <div class="adddepartment">
          <el-tag :key="tag.parameterValue"
                  v-for="(tag,index) in dynamicTags.length"
                  :title="tag.parameterValue"
                  :disable-transitions="true">
            <div style="width: 100%;display: flex">
              <div v-show="!dynamicTags[index].edit"
                   style="padding: 0 10px;overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 80%;"
                   :title="dynamicTags[index].parameterValue">
                {{dynamicTags[index].parameterValue}}
              </div>
              <ws-input v-show="dynamicTags[index].edit"
                        type="text"
                        v-model="dynamicTags[index].parameterValue"
                        style="top: -1px;width: 80%;"
                        ref="tagInput"
                        size="small"
                        maxlength="8"></ws-input>
              <div style="width: 20%">
                <i class="el-tag__close el-icon-close"
                   @click="handleClose(dynamicTags[index])"></i>
                <i v-show="!dynamicTags[index].edit"
                   class="el-tag__close el-icon-edit editStyle"
                   @click="handleEdit(dynamicTags[index])"></i>
                <i v-show="dynamicTags[index].edit"
                   class="el-tag__close el-icon-check editStyle"
                   @click="handleSave(dynamicTags[index])"></i>
              </div>
            </div>
          </el-tag>
          <el-input class="input-new-tag"
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    maxlength="8"
                    @keyup.enter.native="handleInputConfirm"
                    @blur="handleInputConfirm">
          </el-input>
          <el-button v-else
                     class="input-new-tag"
                     size="small"
                     @click="showInput">新增</el-button>
        </div>
        <span slot="footer"
              class="dialog-footer"
              style="padding-top: 20px;">
          <ws-button @click="setDialogFormVisible = false">取消</ws-button>
          <ws-button type="primary"
                     @click="submitNewType">确定</ws-button>
        </span>
      </ws-dialog>
      <!-- 模糊搜索 Start -->
      <div class="searchThree"
           align="right">
        <span style="font-size: 14px; color: #606266;">发布日期:</span>
        <ws-date-picker v-model="deptBudgetDate"
                        @change="getDeptBudgetDate"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        style="width: 40%" />
        <ws-input v-model="deptBudgetPage.searchKeyword"
                  maxlength="50"
                  placeholder="请输入标题"
                  @clear="resetSearch"
                  @keyup.enter.native="getDeptBudgetList"
                  style="width: 35%;margin-right: -3px;" />
        <svg-icon icon-class="gen"
                  class="searchIcon"
                  @click="getDeptBudgetList" />
      </div>
    </div>
    <div>
      <!--一览页页面 Start -->
      <ws-normal-table v-loading="listLoading"
                       stripe
                       @sort-change="sortChange"
                       :data="deptBudgetList"
                       @cell-click="jumpDeptBudget">
        <ws-table-column align="center"
                         prop="crtNo"
                         label="序号"
                         width="100">
          <template slot-scope="scope">
            {{ (deptBudgetPage.currentPage - 1)*deptBudgetPage.pageSize + scope.$index + 1 }}
          </template>
        </ws-table-column>
        <ws-table-column align="center"
                         prop="title"
                         label="标题"
                         width="150" />
        <ws-table-column align="center"
                         prop="typeName"
                         label="类型"
                         width="150" />
        <ws-table-column align="center"
                         prop="issueUserName"
                         label="发布人"
                         width="150" />
        <ws-table-column align="center"
                         prop="issueDate"
                         label="发布日期"
                         width="150" />
        <ws-table-column align="center"
                         prop="tenantName"
                         label="发布范围">
          <template slot-scope="scope">
            <div :title="scope.row.tenantName">{{ scope.row.tenantName }}</div>
          </template>
        </ws-table-column>
        <ws-table-column align="center"
                         prop="statusStr"
                         label="状态"
                         width="100" />
        <ws-table-column align="center"
                         prop="defaultNewsName"
                         sortable="custom"
                         label="新/老用户"
                         width="100" />
        <ws-table-column prop="remark"
                         align="center"
                         label="操作"
                         width="100">
          <template slot-scope="scope">
            <i class="el-icon-edit aaa"
               type="primary"
               @click="compile(scope.row)"
               v-show="scope.row.edit"></i>
            <i class="el-icon-delete aaa"
               type="primary"
               @click="handleDelete(scope.$index,scope.row)"></i>
          </template>
        </ws-table-column>
      </ws-normal-table>
      <pagination v-show="deptBudgetTotal>0"
                  :total="deptBudgetTotal"
                  :page.sync="deptBudgetPage.currentPage"
                  :limit.sync="deptBudgetPage.pageSize"
                  @pagination="getDeptBudgetList" />
      <!--一览页页面 End -->
    </div>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
import { getPageByCondition, createOrUpdateNewType, getNewType, removeIssueInfo } from '../../api/new'

export default {
  name: 'newList',
  components: {
    Pagination
  },
  data () {
    return {
      listLoading: false,
      setDialogFormVisible: false,
      inputVisible: false,
      inputValue: '',
      allTypeName: '全部类型',
      shipList: [],
      deptBudgetDate: [], // 过滤时间数组
      deptBudgetList: [],
      deptBudgetPage: {
        currentPage: 1,
        pageSize: 10,
        typeId: '',
        dateStart: '',
        dateEnd: '',
        searchKeyword: '',
      },
      deptBudgetTotal: 0,
      id: '',
      dialogDeptBudgetFilter: false,
      addRemarkMsg: {
        id: ''
      },
      dynamicTags: [],
    }
  },
  created () {
    this.getDeptBudgetList()
  },
  mounted () {
    this.getDeptBudgetList();
  },
  methods: {
    // 排序
    sortChange (column) {
      if (column.order === 'ascending') {
        this.deptBudgetPage.orderColunm = 'defaultNews';
        this.deptBudgetPage.order = true
      }
      if (column.order === 'descending') {
        this.deptBudgetPage.orderColunm = 'defaultNews';
        this.deptBudgetPage.order = false
      }
      this.getDeptBudgetList()
    },
    handleClose (tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    handleEdit (tag) {
      this.$nextTick(_ => {
        this.$set(tag, 'edit', true)
      });
      this.$forceUpdate();
    },
    handleSave (tag) {
      this.$nextTick(_ => {
        this.$set(tag, 'edit', false)
      });
      this.$forceUpdate();
    },

    showInput () {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm () {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push({
          id: '',
          constCode: 'NEWS_TYPE',
          parameterKey: '',
          parameterValue: this.inputValue,
          parameterValueEn: this.inputValue,
          edit: false
        })
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    getclearTime () {
      this.deptBudgetPage.dateStart = ''
      this.deptBudgetPage.dateEnd = ''
      this.getDeptBudgetList()
    },
    // 一览页面信息
    getDeptBudgetList () {
      this.listLoading = true;
      getPageByCondition(this.deptBudgetPage).then(response => {
        this.deptBudgetList = response.data.records;
        this.deptBudgetList.forEach((item) => {
          this.id = item.id
          item.operate = false;
          item.statusStr = '';
          item.issueDate = item.issueDate && item.issueDate.length > 0 ? item.issueDate.substr(0, 16) : ''

          item.operate = true;
          if (item.isIssue === '1') {
            item.edit = false;
            item.statusStr = '已发布';
          } else {
            item.edit = true;
            item.statusStr = '待发布';
          }
        })
        this.deptBudgetTotal = response.data.total
        this.listLoading = false;
      })
    },
    // 公告类型数据
    BasicInformation () {
      noticeTypelist({ constCode: 'NEWS_TYPE' }).then(response => {
        this.shipList = response.data
        this.shipList.splice(0, 0, {
          id: '',
          parameterValue: '全部类型',
          parameterValueEn: 'All type'
        })
      })
    },

    // 日期筛选
    getDeptBudgetDate (val) {
      if (val != null && val.length === 2) {
        this.deptBudgetPage.dateStart = val[0] + ' 00:00:00'
        this.deptBudgetPage.dateEnd = val[1] + ' 23:59:59'
        this.getDeptBudgetList()
      } else {
        this.deptBudgetPage.dateStart = null
        this.deptBudgetPage.dateEnd = null
        this.getDeptBudgetList()
      }
    },

    // 重置
    resetSearch () {
      this.deptBudgetPage.searchKeyword = null
      this.getDeptBudgetList()
    },
    // 跳转到新增页面
    addGoing () {
      this.$router.push({
        name: 'newAdd'
      })
    },
    // 设置类型
    setNewType () {
      getNewType({ constCode: 'NEWS_TYPE' }).then(response => {
        this.dynamicTags = response.data
        if (this.dynamicTags) {
          for (let i = 0; i < this.dynamicTags.length; i++) {
            this.dynamicTags[i].edit = false
          }
        }
      })
      this.setDialogFormVisible = true
    },
    submitNewType () {
      createOrUpdateNewType(this.dynamicTags).then(response => {
        this.$message({
          type: "success",
          message: '保存成功!'
        });
      })
      this.setDialogFormVisible = false
    },
    // 一览页面跳转
    jumpDeptBudget (row, column) {
      if (column.property !== 'remark') {
        this.$router.push({
          name: 'newDetails',
          query: {
            id: row.id,
          }
        })
      }
    },

    //  待发布跳转到编辑页
    compile (row) {
      this.$router.push({
        name: 'newAdd',
        query: {
          id: row.id,
        }
      })
    },
    // 删除
    handleDelete (idx, row) {
      this.$confirm('此操作将永久删除该条数据，是否继续？', '提示', {
        confirmButtonText: '确 认',
        cancelButtonText: '取 消',
        type: 'warning'
      }).then((res) => {
        if (res) {
          this.loading = true
          this.addRemarkMsg.id = row.id
          removeIssueInfo(this.addRemarkMsg).then(response => {
            this.loading = false
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
            this.getDeptBudgetList();
          })
        } else {
          this.loading = false
          this.$message({
            message: '删除失败！',
            type: 'error'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除！'
        });
      });
    }
  }
}
</script>

<style scoped lang="scss">
.tabsMain .navBar {
  width: 100%;
}

.main-list {
  height: calc(81vh - 0px);
  padding: 0 !important;
}

.searchThree {
  width: 70%;
}

.searchThreeFide {
  display: flex;
}

.input-main {
  margin-top: 0 !important;
}

.nav-search {
  float: right;
  padding: 15px 0;
}

.tab-kuan {
  float: left;
}

.type {
  font-size: 12px;
  font-weight: 400;
  color: rgba(102, 102, 102, 1);
  margin-right: 10px;
}

.remove {
  margin-left: 10px;
  line-height: 0px;
  margin-top: 7px;
}

/deep/ .el-pagination__jump {
  margin-right: 46px;
}

.aaa {
  font-size: 16px;
  margin-left: 10px;
}

.searchIcon {
  height: 32px;
  width: 32px;
  float: right;
  position: relative;
}

.tab-button {
  width: 30%;
}

.adddepartment {
  height: 300px;
  overflow-y: auto;
  font-size: 0px;
}

.tab-item {
  display: flex;
  padding: 10px 0px 10px 0px;
}

.el-tag + .el-tag {
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

/deep/ .el-tag + .el-tag {
  margin-left: 2%;
  margin-top: 10px;
}

/deep/ .el-tag {
  width: 47%;
  margin-left: 2%;
  margin-top: 10px;
  padding: 0px 0px;
}

/deep/ .input-new-tag {
  width: 47%;
  margin-left: 2%;
  margin-top: 10px;
}

/deep/ .button-new-tag {
  width: 47%;
  margin-left: 2%;
  margin-top: 10px;
}

/deep/ .el-tag .el-icon-close {
  float: right;
  top: 9px;
  right: 5px;
}

.editStyle {
  width: 16px;
  height: 16px;
  border-radius: 100%;
  text-align: center;
  line-height: 16px;
  text-align: center;
  position: relative;
  cursor: pointer;
  font-size: 12px;
  vertical-align: middle;
  float: right;
  top: 9px;
  right: 10px;
}
// 添加table 上的排序以后样式改变的问题
/deep/ .el-table {
  td {
    padding: 0;
    line-height: 50px;
    height: 50px;
  }
  th {
    padding: 0;
    div {
      line-height: 50px;
      height: 50px;
    }
  }
}
</style>
