<template>
    <div>
        <crumbs title="免责声明维护">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="标题：">
                    <el-input size="mini" v-model="filter.title" placeholder="输入标题"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="warning" @click="addDisclInfo" style="margin:0 10px">添加</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="contents">
            <el-table :data="list"
                      border
                      style="width: 100%">
                <el-table-column
                        type="index"
                        :index="indexMethod"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="gmtCreate"
                        label="操作时间"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="modifier"
                        label="操作人">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="标题"
                        show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                        prop="disclaimerCode"
                        label="归属">
                </el-table-column>
                <el-table-column
                        label="状态">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" :class="{forbid:scope.row.status==0}" @click="toOpen(scope.row)">启用</el-button>
                        <!-- <span style="margin:0 3px;">|</span>
                        <el-button type="text" size="small" :class="['delete',{'forbid':scope.row.status==1}]" @click="toClose(scope.row)">停用</el-button> -->
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="toEditDiscl(scope.row)">修改</el-button>
                        <span style="margin:0 3px;">|</span>
                        <el-button type="text" size="small" :class="['delete',{'forbid':scope.row.status==1}]" @click="showDelDialog(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                        label="详情">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="getDetail(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-if="total>pageSize"
                           :current-page.sync="currentPage"
                           @current-change="currentChange"
                           background
                           :page-size="pageSize"
                           layout="total,prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
  import api from '@api/icss.js';

  export default {
    name: 'DisclaimerInformation',
    data: function () {
      return {
        list: [],
        cacheData: {},
        currentPage: 1,
        pageSize: 10,
        total: 0,
        linkIn:[],
        pays:[],
        searched: false,
        filter: {
          title: ''
        }
      }
    },
    created() {
      const that = this;
      //返回时避免参数未赋值就获取列表
      setTimeout(function(){
        that.getDataList();
      });
    },
    watch: {
      'filter': {
        handler: function () {
          this.searched = false;
        },
        deep: true
      }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        //const pm = to.param;
        Object.assign(vm, to.params);
      })
    },
    methods: {
      addDisclInfo(){
        const pam = this.searched ? {
          currentPage: this.currentPage,
          filter: this.filter
        } : {currentPage: this.currentPage};
        this.$router.push({name: 'AddDisclInfo', params: pam});
      },
      toEditDiscl(row){
        const pam = this.searched ? {
          currentPage: this.currentPage,
          filter: this.filter
        } : {currentPage: this.currentPage};
        this.$router.push({
          name:'AddDisclInfo',
          params: Object.assign(pam, {info:row})
        })
      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList(isTurnPage) {
        const param = this.getFilterItems(isTurnPage);
        this.searched = true;
        const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        api.discInformation(param).then((res) => {
          loading.close()
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records;
            this.cacheData[param.current] = data.records;
            this.total = data.total;
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      getDetail(item) {
        this.$router.push({name:'DiscInfoDetail', params:{info: item}})
      },
      getFilterItems(isTurnPage) {
        //翻页时筛选条件没点确定则清空
        if(isTurnPage&&!this.searched){
          this.filter.title='';
        };
        const param = {
          title: this.filter.title.trim(),
          current: this.currentPage,
          size: this.pageSize
        };
        return param;
      },
      indexMethod(index) {
        return ((this.currentPage - 1) * this.pageSize) + index + 1;
      },
      currentChange(next) {
        this.currentPage = next;
        /*if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
          this.list = this.cacheData[next];
        } else {*/
          this.getDataList(true);
        //}
      },
      warning(msg,type){
        this.$message({
          showClose: true,
          message:msg,
          type:type||'warning'
        })
      },
      showConfirmDialog(msg,resolve){
        this.$alert(msg, '提示', {
          confirmButtonText: '确定',
          type: 'warning'
        }).then(() => {
          resolve();
        }).catch(() => {});
      },
      showDelDialog(item){
        // 启用状态下不能删除
        if(item.status==1){return}
        this.showConfirmDialog('是否删除该免责声明？',()=>{
          api.delDiscInformation({id:item.id}).then((res)=>{
            if(res.data.code=='0'){
              if(!this.searched){
                //未点确认时清空搜索条件
                this.filter={
                  title: ''
                };
              }
              this.warning(res.data.msg||'操作成功','success');
              this.getDataList();
            }else{
              this.warning(res.data.msg);
            }
          }).catch((error)=>{
            this.warning(error);
          })
        });
      },
      toOpen(item){
        // status=0停用，status=1启用
        if(item.status==0){
          this.showConfirmDialog('是否启用该免责声明？',()=>{
            api.openInformation({id:item.id}).then((res)=>{
              if(res.data.code==0){
                //恢复成功后跳转到筛选条件的首页
                this.currentPage = 1;
                this.$message({
                  type:'success',
                  message:res.data.msg ||"启用成功"
                })
                this.getDataList();
              }
            }).catch((error)=>{
              this.warning(error);
            })
          });
        }
      },
      toClose(item){
        if(item.status==1){
          this.showConfirmDialog('是否停用该免责声明？',()=>{
            api.closeInformation({id:item.id}).then((res)=>{
              if(res.data.code==0){
                this.$message({
                  type:'success',
                  message:res.data.msg ||"停用成功"
                })
                this.getDataList();
              }
            }).catch((error)=>{
              this.warning(error);
            })
          });
        }
      }
    }
  }
</script>

<style lang="less" scoped>
    @import "../../less/admin.less";
    .delete{
      color: red !important;
    }
    .forbid{
      color: #BFBFBF !important;
    }
</style>