<template>
  <div class="yqrange-index-wrapper">
    <bread-crumb :curmbFirst="curmbFirst"></bread-crumb>
    <div class="yqrange-index-content screenSet" id="screenSet">
      <div class="header-title">云鹊小圈</div>
      <el-form ref="searchForm" :model="searchForm" label-width="75px" label-suffix=":" :inline="true">
        <el-row :gutter="30" type="flex" style="margin-top: 10px">
          <el-col :span="10">
            <el-form-item label="圈子名称">
              <el-input v-model="searchForm.name" size="small" placeholder="请输入圈子名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="创建时间">
              <el-date-picker
                  v-model="searchForm.startTime"
                  size="small"
                  type="datetime"
                  placeholder="请选择创建时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="text-align: right">
            <el-button type="primary" size="small" @click="searchList">查询</el-button>
            <el-button type="default" size="small" @click="resetForm" style="margin-left:0;">重置</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-button type="primary" size="small" @click="createRange" v-if="">新建小圈</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="tableData" class="item-table" style="width: 100%;margin-top: 10px;">
        <el-table-column prop="id" label="圈子ID" min-width="100" align="center"></el-table-column>
        <el-table-column prop="createdTime" label="创建时间" min-width="100" align="center"></el-table-column>
        <el-table-column prop="name" label="圈子名称" min-width="100" align="center"></el-table-column>
        <el-table-column prop="numberOfPeople" label="圈子人数" min-width="100" align="center"></el-table-column>
        <el-table-column prop="dynamicFlag" label="内容发布权限" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.dynamicFlag | dynamicFlagStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="圈子状态" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.status | rangeStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="370" align="center">
          <template slot-scope="scope">
            <el-button @click="publishRange(scope.row)" v-if="scope.row.status == 2" type="text" size="small">上线</el-button>
            <el-button @click="unPublishRange(scope.row)" v-if="scope.row.status == 1" type="text" size="small">下线</el-button>
            <el-button @click="liveManage(scope.row)" type="text" size="small">直播管理</el-button>
            <el-button @click="roleManage(scope.row)" type="text" size="small">成员管理</el-button>
            <el-button @click="editRange(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="deleteRange(scope.row)" type="text" size="small" v-if="showAllFlag">解散圈子</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleNumChange"
            :current-page="searchForm.pageNo"
            :page-sizes="[10, 30, 50, 100]"
            :page-size="searchForm.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalRows"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
  import { openLoading, closeLoading } from "../../utils/utils";
import BreadCrumb from "@/components/breadcrumb.vue";
import { getRangeList,updateRangeStatus } from "../../utils/yqrange/yqrangeApi";
export default {
  components: {
    BreadCrumb
  },
  data(){
    return{
      curmbFirst: '云鹊小圈',
      showAllFlag: false,
      searchForm: {
        name: '',
        startTime: '',
        endTime: '',
        userType: "", // 用户标识 1：内部用户 2：外部用户,
        status: "",
        pageNo: 1,
        pageSize: 10,
      },
      totalRows: 0,
      tableData: []
    }
  },
  created() {
    this.initPrivilege();
    this.searchList();
  },
  methods: {

    initPrivilege(){
      let idType = localStorage.getItem('storageIdType');
      if(idType == null || idType == ""){
        idType = 1;
      }
      this.searchForm.userType = idType;

      // 内部用户:运营人员
      if(idType == "1"){
        this.showAllFlag = true;
      }
      // 外部用户
      else if(idType == "2"){
        this.showAllFlag = false;
        this.getUserAuth();
      }

    },
    // 外部用户权限
    getUserAuth(){
      getAuth().then((res) => {
        closeLoading(this);
        if(res.code == "000000") {


        } else {



        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        });
      })

    },


    searchList() {
      openLoading(this);
      let params = this.searchForm;

      console.log('查询圈子')
      getRangeList(params).then((res) => {
        closeLoading(this);
        if(res.code == "000000") {
          this.tableData = res.data.circleList;
          // 开发用
          if(this.tableData.length == 0){
            this.tableData = [{
              "id" : 123456,
              "createdTime" : "2020-06-10 00:00:00",
              "name" : "第一个圈子",
              "numberOfPeople" : 550,
              "dynamicFlag" : 1,
              "status" : 1,
              "roleId": 1
            },
              {
                "id" : 123457,
                "createdTime" : "2020-06-19 00:00:00",
                "name" : "第二个圈子",
                "numberOfPeople" : 560,
                "dynamicFlag" : 1,
                "status": 2,
                "roleId": 2
              }

            ]
          }

          // this.initFormData();
          this.totalRows = res.data.count;
        } else {
          this.tableData = [];
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        });
      })
    },

    resetForm() {
      this.searchForm.name = "";
      this.searchForm.startTime = "";
      console.log('重置')
    },
    //新建小圈
    createRange() {
      let enterType = 1;//1:add 2 edit
      this.$router.push({
        path: `/create-range?enterType=${enterType}`
      })
    },
    //上线
    publishRange(row) {
      this.$confirm(`确定上线“${row.name}”吗？`, '', {
        confirmButtonText: '上线',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'range-make-box',
      }).then(() => {
        //  confirm
        this.updateStatus(row.id, "3");
        this.$message.success("上线成功");
      }).catch(() => {
        //  cancel
        this.$message.error("上线失败");
      });
    },
    //下线
    unPublishRange(row) {
      this.$confirm(`确定下线“${row.name}”吗？`, '', {
        confirmButtonText: '下线',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'range-make-box',
      }).then(() => {
        this.updateStatus(row.id, "1");
        //  confirm
      }).catch(() => {
        //  cancel

      });
    },
    // 更新圈子状态（上线，下线）
    updateStatus(id, status){
      let params = {"id": id, "status": status};
      updateRangeStatus(params).then((res) => {
        closeLoading(this);
        if(res.code == "000000") {
          if(status == "1"){
            this.$message.success("下线成功");
          }
          else if(status == "2") {
            this.$message.success("上线成功");
          }
        } else {
          if(status == "1"){
            this.$message.success("下线失败，请重试");
          }
          else if(status == "2") {
            this.$message.success("上线失败，请重试");
          }


        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        });
      })


    },



    //直播管理
    liveManage(row) {
      this.$router.push({
        path: '/live-manage',
      })
    },
    //成员管理
    roleManage(row) {
      this.$router.push({
        path: '/role-manage?id=' + row.id,
      })
    },
    //编辑
    editRange(row) {
      let enterType = 2;//1:add 2 edit
      this.$router.push({
        path: `/create-range?enterType`,
        query:{
          id:row.id,
          enterType:enterType
        }
      })
    },
    //解散圈子
    deleteRange(row) {
      this.$confirm(`确定解散“${row.name}”吗？`, '', {
        confirmButtonText: '解散',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'range-make-box',
      }).then(() => {
        this.updateStatus(row.id, "3");
        //  confirm
      }).catch(() => {
        //  cancel
      });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.searchForm.pageNo = 1;
      this.searchList();
    },
    handleNumChange(val) {
      this.searchForm.pageNo = val;
      this.searchList();
    },
  },
}
</script>
<style lang="scss">
  .yqrange-index-wrapper{
    .yqrange-index-content{
      background: #fff;
      padding: 10px;
      .header-title{
        padding: 10px 12px;
        font-size: 12px;
        color: #449284;
        border-bottom: 1px solid #efefef;
      }
    }
    .el-button--text{
      color: #449284;
      font-size: 14px;
      &::after{
        content: '';
        position: relative;
        height: 14px;
        /*border-right: 1px solid #EBEEF5;*/
        border-right: 1px solid #aaaaaa;
        padding-right: 10px;
      }
      &:last-of-type{
        &::after{
          content: '';
          position: relative;
          width: 1px;
          height: 14px;
          border-right: none;
        }
      }
    }
  }
  .range-make-box{
    .el-message-box__btns{
      margin-top: 30px;
    }
  }
</style>
