<template>
  <div class="app-container calendar-list-container" v-loading.body="showLoadingBody">
    <div v-if="!popupDialogVisible">
      <div class="filter-container" ref="filter-container">
        <el-form :inline="inline" ref="queryForm" :model="listQuery" label-width="100px">
          <el-row>
            <!--            <el-col :span="8"></el-col>-->
            <!--            <el-form-item label="新闻内容">-->
            <!--              <el-input v-model.number="listQuery.newsContent" placeholder="请输入新闻内容"></el-input>-->
            <!--            </el-form-item>-->

          </el-row>
        </el-form>
        <!--        <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>-->
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate"
                   type="primary" icon="edit">添加
        </el-button>
      </div>
      <el-table :key='tableKey' :data="list" border fit highlight-current-row
                style="width: 100%">
        <el-table-column
          type="index"
          align="center" label="序号" width="65">
        </el-table-column>
        <el-table-column align="center" label="封面图/标题">
          <template slot-scope="scope">
            <img :src="scope.row.coverImage" style="width: 30px;"/>
            <span>{{scope.row.title}}</span>
          </template>
        </el-table-column>

        <el-table-column width="200" align="center" label="文章摘要">
          <template slot-scope="scope">
            <span>{{scope.row.epitome}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="作者">
          <template slot-scope="scope">
            <span>{{scope.row.author}}</span>
          </template>
        </el-table-column>

        <el-table-column width="200" align="center" label="发布人">
          <template slot-scope="scope">
            <span>{{scope.row.publisher}}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" align="center" label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.statusString}}</span>
          </template>
        </el-table-column>
        <el-table-column width="150" align="center" label="文章发布网站">
          <template slot-scope="scope">
            <span>{{scope.row.typeString}}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" align="center" label="排序">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="clickRank(scope.row)">{{scope.row.weight?scope.row.weight:0}}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column align="center" width="200" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" class="el-button el-button--text el-button--small" @click="handleUpdate(scope.row)">
              {{'编辑'}}
            </el-button>
            <el-button size="small" class="el-button el-button--text el-button--small" style="color: #6ce26c;"
                       v-if="scope.row.status==2|| scope.row.status==0" @click="upStatus(scope.row,1)">上架
            </el-button>
            <el-button size="small" class="el-button el-button--text el-button--small" style="color: #1d90e6"
                       v-if="scope.row.status==1" @click="upStatus(scope.row,2)">下架
            </el-button>
            <el-button class="el-button el-button--text el-button--small" style="color:red;" size="small"
                       @click="deleteHandler(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </div>
    </div>
    <!-- 文章编辑-->
    <popup v-if="popupDialogVisible" :oneRow="oneRow"
           :popupTitle="modalTitle" v-on:oneDialogEvent="oneDialogEvent"
    ></popup>

    <!-- 排序 modal弹窗 -->
    <el-dialog :title="modalTitle" :visible.sync="rankDialogVisible">
      <el-form :model="labelForm" ref="labelForm" label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="排序" prop="rank">
              <el-input v-model="labelForm.weight" placeholder="请输入排序值"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelHandel">取 消</el-button>
        <el-button type="primary" @click="updateParentLabelRank('labelForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import popup from './modal/popup'// 车型
  import 'static/css/uploadImg.css';// 引入图片上传组件对话框

  import {mapGetters} from 'vuex';


  import {
    page,
    add,
    update,
    putaway,
    soldOut,
    remove
  } from 'src/api/officialWebsite/news';

  import Element1 from "../admin/menu/components/element";
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";

  export default {
    name: 'newsList',
    components: {
      ElCol,
      ElRow,
      Element1,
      popup
    },
    data() {
      return {
        // 0-所有,1-新欣房车官网,2-滴房车官网
        // brandlist: [{id:1,name:'所有网站'},{id:2,name:'天德房车官网'},{id:3,name:'滴房车官网'}],
        modalTitle: "创建",
        BASE_API: process.env.BASE_API,
        popupDialogVisible: false,//添加、编辑弹框
        showLoadingBody: false,
        form: {
          tagNames: "",//标签
          name: undefined,//旅游名称
          saleCount: undefined,//销量
          stock: undefined,//总数量
        },
        list: null,
        total: null,
        publishWebSite: {
          0: '所有',
          1: '天德房车官网',
          2: '滴房车官网'
        },
        status: {
          0: '下架',
          1: '上架'
        },
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          newsContent: undefined,//新闻包含内容
          type: undefined
        },
        inline: true,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        labelForm: {},
        tableKey: 0,
        departureDialogVisible: false,
        arrivalDialogVisible: false,
        rankDialogVisible: false//排序
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements',
      ])
    },
    methods: {
      /**
       * 添加
       * */
      handleCreate() {
        this.modalTitle = '创建';
        this.oneRow = {};
        this.popupDialogVisible = true;
      },

      upStatus(row, type) {
        if (this.$utils.isInteger(type)) {
          if (type == 1) {
            putaway(row.id).then(response => {
              if (response.status === 200) {
                this.$notify({
                  title: '成功',
                  message: '成功',
                  type: 'success',
                  duration: 2000
                });
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            });
          }

          if (type == 2) {
            soldOut(row.id).then(response => {
              if (response.status === 200) {
                this.$notify({
                  title: '成功',
                  message: '成功',
                  type: 'success',
                  duration: 2000
                });
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            });
          }
        }
      },
      /**
       * 删除
       * */
      deleteHandler(row) {
        this.$confirm('确定删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(row.id).then(response => {
            if (response.status === 200) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              // this.bannerDialogVisible = false;
              this.getList();
            } else {
              this.$notify({
                title: '删除失败',
                message: '操作失败！',
                type: 'error',
                duration: 2000
              });
            }
          });
        })
      },

      /**
       * 编辑
       * */
      handleUpdate(row) {
        this.modalTitle = '编辑';
        this.oneRow = row;
        this.popupDialogVisible = true;
      },
      /**
       * 排序点击事件
       * */
      clickRank(row) {
        this.modalTitle = "编辑";
        this.labelForm = {weight: row.weight, id: row.id};
        this.rankDialogVisible = true;
      },
      /**
       * 弹框-取消
       * */
      cancelHandel() {
        this.rankDialogVisible = false;
      },
      /**
       * 编辑排序
       * */
      updateParentLabelRank(formName) {
        const set = this.$refs;
        let params = {id: this.labelForm.id,weight: this.labelForm.weight};
        update(params).then(response => {
          if (response.status === 200) {
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            });
            this.rankDialogVisible = false;
            this.getList();
          } else {
            this.$notify({
              title: '编辑失败',
              message: '操作失败！',
              type: 'error',
              duration: 2000
            });
          }
        });
      },

      /**
       * modal传递回来的数据
       * */
      oneDialogEvent(e) {
        this.popupDialogVisible = false;
        if (e) {
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      handleAvatarSuccess(res, file) {
        this.form.drivingLicensePath = res.data;
        this.showLoadingBody = false;
      },


      /**
       * 获取新闻列表
       * */
      getList() {
        this.listLoading = true;
        page(this.listQuery).then(response => {
          let totalCountRs = undefined;
          let listRs = undefined;
          let publishWebSite = this.publishWebSite;
          let status = this.status
          if (!this.$utils.isEmpty(response.data.list) && this.$utils.isInteger(response.data.total)) {
            response.data.list.map(function (item) {
              item.coverImage = item.coverImage ? item.coverImage : '';
              item.typeString = publishWebSite[item.type] ;
              item.statusString = status[item.status] ;
            });
            listRs = response.data.list;
            totalCountRs = response.data.total;
          }
          this.listLoading = false;
          this.list = listRs;
          this.total = totalCountRs;
        })

      },
      handleFilter() {
        this.listQuery.page = 1;
        this.$refs.queryForm.validate(valid => {
          if (valid) {
            this.getList();
          } else {
            return false;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      }
    }
  }
</script>
