<template>
  <div>
    <!-- 搜索标题 -->
    <div @click="openTeaching('create')"
         class="newBand">新建</div>
    <!-- 搜索标题 -->
    <save-form :inline="true"
             class="searchForm"
             :saveKey="'contentTraining'"
             @submit="search"
             :model="searchForm">
      <el-form-item prop="organIdList">
        <el-select class="multiple" clearable
                   filterable
                   collapse-tags
                   multiple
                   v-model.trim="searchForm.organIdList"
                   placeholder="请选择分部">
          <el-option v-for="(item,index) in selects.branchs"
                    :key="index"
                    :label="item.name"
                    :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
      </el-form-item>
    </save-form>
    <!-- 列表 -->
    <div class="tableWrap">
      <el-table :data="tableList"
                :header-cell-style="{background:'#EDEEF0',color:'#444'}">
        <el-table-column align="center"
                         label="轮播图">
          <template slot-scope="scope">
            <img class="bannerImg"
                 :src="scope.row.coverImage"
                 alt />
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="title"
                         label="标题"></el-table-column>
        <el-table-column align="center"
                         label="跳转连接">
          <template slot-scope="scope">
            <overflow-text :text="scope.row.linkUrl + '/' + scope.row.id"></overflow-text>
            <!-- {{ scope.row.linkUrl + '/' + scope.row.id }} -->
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="remark"
                         label="是否使用">
          <template slot-scope="scope">{{ scope.row.status == 1 ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column align="center"
                         prop="order"
                         label="排序"></el-table-column>
        <el-table-column align="center"
                         prop="remark"
                         label="适用分部">
          <template slot-scope="scope">
            <overflow-text :text="scope.row.organNameList"></overflow-text>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="openTeaching('update', scope.row)"
                       type="text">修改</el-button>
            <el-button v-if="scope.row.status == 1"
                       @click="onStop(scope.row, 0)"
                       type="text">停用</el-button>
            <el-button v-else
                       @click="onStop(scope.row, 1)"
                       type="text">启用</el-button>
            <el-button @click="onDel(scope.row)"
                       type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
       :saveKey="'contentTraining'"
      sync :total.sync="pageInfo.total"
                  :page.sync="pageInfo.page"
                  :limit.sync="pageInfo.limit"
                  :page-sizes="pageInfo.page_size"
                  @pagination="getList" />
    </div>
  </div>
</template>
<script>
import { newsList, newsUpdate, newsDel } from "@/api/contentManager";
import pagination from "@/components/Pagination/index";
export default {
  name: "training",
  components: {
    pagination
  },
  data () {
    return {
      searchForm: {
        organIdList: []
      },
      tableList: [],
      teacherId: this.$route.query.teacherId,
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 1, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      }
    };
  },
  mounted () {
    this.$store.dispatch("setBranchs");
    this.getList();
  },
  methods: {
    search() {
      this.pageInfo.page = 1
      this.getList()
    },
    getList () {
      let params = {
        clientName: 'manage',
        organIdList: this.searchForm.organIdList ? this.searchForm.organIdList.join(',') : null,
        rows: this.pageInfo.limit,
        page: this.pageInfo.page,
        type: 4
      };
      newsList(params).then(res => {
        if (res.code == 200) {
          this.tableList = res.data.rows;
          this.pageInfo.total = res.data.total;
        }
      });
    },
    openTeaching (type, rows) {
      let params = {};
      if (type == "update") {
        params.id = rows.id;
      }
      params.type = 4;
      params.pageType = type;
      this.$router.push({
        path: "/contentManager/contentOperation",
        query: params
      });
    },
    onDel (row) {
      // 删除
      this.$confirm("确定是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          newsDel({ id: row.id }).then(res => {
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => { });
    },
    onStop (row, status) {
      // 停止
      // newsUpdate
      let tempStr = ["停用", "启用"];
      newsUpdate({
        id: row.id,
        status: status
      }).then(res => {
        if (res.code == 200) {
          this.$message.success(tempStr[status] + "成功");
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.bannerImg {
  height: 60px;
}
</style>
