<template>
  <div class="shareCategory">
    <template v-if="IsShowCategory">
      <div class="head-title">
        发圈素材分类
        <el-button @click="addFenlei" style="float:right;margin-top: -5px;" size="small" type="primary">
          新增分类
        </el-button>
      </div>
      <div class="content">
        <div class="searchInput">
          <el-input style="display:inline-block;width:225px;height:30px" placeholder="请输入分类名称" v-model="msg.Name"
            size="small" clearable @keyup.enter.native="msg.pageIndex=1,getList()" @clear="msg.pageIndex=1,getList()">
          </el-input>
          <span class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"
            @click="msg.pageIndex=1,getList()"> </span>
        </div>
        <br>
        <el-button @click="saveSort()" size="small" type="primary">保存排序</el-button>
        <div v-loading="loading" flex="dir:left box:mean" class="cat-list" style="margin-top:20px;">
          <el-card class="card-item-box" shadow="never">
            <div slot="header" class="clearfix">
              <span>一级分类</span>
            </div>
            <draggable flex v-model="dataFirst">
              <div @click="getChild(item,index)" v-for="(item,index) in dataFirst"
                :class="{'itemActive':activeIndex==index}" class="ShareList" :key="index">
                <el-row style="padding:7px 0;height:50px;box-sizing:border-box">
                  <el-col :span="4" style="padding-top:7px">
                    <el-tooltip class="item" effect="dark" :content="item.Id+''" placement="top">
                      <div class="cat-id">{{item.Id}}</div>
                    </el-tooltip>
                  </el-col>
                  <el-col :span="11" flex="cross:center" style="padding-top:7px">
                    <div class="cat-name-info">
                      <el-tooltip class="item" effect="dark" :content="item.Name" placement="top">
                        <span>{{item.Name}}</span>
                      </el-tooltip>
                    </div>
                  </el-col>
                  <el-col :span="7">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                      <img @click="getCategory(item.Id)" style="margin-right:9px"
                        src="../../assets/img/userman/edit.png" alt="">
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除" placement="top">
                      <img @click="removeCategory(item.Id)" src="../../assets/img/userman/del.png" alt="">
                    </el-tooltip>
                  </el-col>
                </el-row>
              </div>
            </draggable>
          </el-card>
          <el-card class="card-item-box" shadow="never">
            <div slot="header" class="clearfix">
              <span>二级分类</span>
            </div>
            <draggable flex v-model="dataSecond">
              <div @click="getSecond(item,index)" v-for="(item,index) in dataSecond"
                :class="{'itemActive':activeIndex2==index}" class="ShareList" :key="index">
                <el-row style="padding:7px 0;height:50px;box-sizing:border-box">
                  <el-col :span="4" style="padding-top:7px">
                    <el-tooltip class="item" effect="dark" :content="item.Id+''" placement="top">
                      <div class="cat-id">{{item.Id}}</div>
                    </el-tooltip>
                  </el-col>
                  <el-col :span="11" flex="cross:center" style="padding-top:7px">
                    <div class="cat-name-info">
                      <el-tooltip class="item" effect="dark" :content="item.Name" placement="top">
                        <span>{{item.Name}}</span>
                      </el-tooltip>
                    </div>
                  </el-col>
                  <el-col :span="7">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                      <img @click="getCategory(item.Id)" style="margin-right:9px"
                        src="../../assets/img/userman/edit.png" alt="">
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除" placement="top">
                      <img @click="removeCategory(item.Id)" src="../../assets/img/userman/del.png" alt="">
                    </el-tooltip>
                  </el-col>
                </el-row>
              </div>
            </draggable>
          </el-card>
          <el-card class="card-item-box" shadow="never">
            <div slot="header" class="clearfix">
              <span>三级分类</span>
            </div>
            <draggable flex v-model="dataThird">
              <div @click="getThird(index)" v-for="(item,index) in dataThird"
                :class="{'itemActive':activeIndex3==index}" class="ShareList" :key="index">
                <el-row style="padding:7px 0;height:50px;box-sizing:border-box">
                  <el-col :span="4" style="padding-top:7px">
                    <el-tooltip class="item" effect="dark" :content="item.Id+''" placement="top">
                      <div class="cat-id">{{item.Id}}</div>
                    </el-tooltip>
                  </el-col>
                  <el-col :span="11" flex="cross:center" style="padding-top:7px">
                    <div class="cat-name-info">
                      <el-tooltip class="item" effect="dark" :content="item.Name" placement="top">
                        <span>{{item.Name}}</span>
                      </el-tooltip>
                    </div>
                  </el-col>
                  <el-col :span="7">
                    <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                      <img @click="getCategory(item.Id)" style="margin-right:9px"
                        src="../../assets/img/userman/edit.png" alt="">
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除" placement="top">
                      <img @click="removeCategory(item.Id)" src="../../assets/img/userman/del.png" alt="">
                    </el-tooltip>
                  </el-col>
                </el-row>
              </div>
            </draggable>
          </el-card>
        </div>
      </div>
    </template>
    <template v-else>
      <div class="head-title">
        <span @click="IsShowCategory=true" style="color:rgb(64, 158, 255);cursor:pointer;">发圈素材分类</span><span
          style="margin:0 9px;color:#C0C4CC">/</span><span>编辑</span>
      </div>
      <div class="content">
        <div class="conten_indder">
          <el-form label-width="150px">

            <el-form-item label="选择分类级别">
              <template v-if="editState">
                <el-radio :disabled="editState" v-model="addMsg.Level" :label="1">一级分类</el-radio>
                <el-radio :disabled="editState" v-model="addMsg.Level" :label="2">二级分类</el-radio>
                <el-radio :disabled="editState" v-model="addMsg.Level" :label="3">三级分类</el-radio>
              </template>
              <template v-else>
                <div @click="SelectTier(1)" style="display:inline-block;margin-right:18px">
                  <el-radio :disabled="editState" v-model="addMsg.Level" :label="1">一级分类</el-radio>
                </div>
                <div @click="SelectTier(2)" style="display:inline-block;margin-right:18px">
                  <el-radio :disabled="editState" v-model="addMsg.Level" :label="2">二级分类</el-radio>
                </div>
                <div @click="SelectTier(3)" style="display:inline-block">
                  <el-radio :disabled="editState" v-model="addMsg.Level" :label="3">三级分类</el-radio>
                </div>
              </template>
            </el-form-item>
            <el-form-item v-if="addMsg.FirstLevelName && (addMsg.Level==2 || addMsg.Level==3)" label="一级分类">
              <span>{{addMsg.FirstLevelName}}</span>
              <span @click="SelectTier(2)" v-if="addMsg.Level==2 && !editState" style="margin-left:10px"
                class="blue point">修改</span>
            </el-form-item>
            <el-form-item v-if="addMsg.SecondLevelName && addMsg.Level==3" label="二级分类">
              <span>{{addMsg.SecondLevelName}}</span>
              <span v-if="!editState" @click="SelectTier(3)" style="margin-left:10px" class="blue point">修改</span>
            </el-form-item>
            <el-form-item label="分类名称">
              <el-input type="text" size="small" v-model="addMsg.Name"></el-input>
            </el-form-item>
            <el-form-item label="排序">
              <el-input type="text" size="small" v-model="addMsg.SortNum"
                @keyup.native="checkInteger(addMsg,'SortNum')"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-button size="small" style="margin-top:20px;padding:9px 25px;" type="primary" @click="submitform()">保存
      </el-button>
    </template>

    <!-- 分类弹窗 -->
    <el-dialog :destroy-on-close="true" :title="titleDig" :visible.sync="tierDig" :width="tierWidth">
      <div>
        <div style="margin-bottom:20px">
          <div class="searchInput" style="width:250px">
            <el-input style="display:inline-block;width:225px;height:30px" placeholder="请输入搜索内容" v-model="msg.Name"
              size="small" clearable>
            </el-input>
            <span @click="getList" class="el-icon-search"
              style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
          </div>
        </div>
        <ul class="tierUl">
          <li style="cursor:pointer" @click="SelectTier1(item,index,2)" :class="active1==index ? 'SelectLi' :''"
            v-for="(item,index) in dataList" :key="index">
            <span>{{item.Name}}</span>
            <span v-if="addMsg.Level==3 && item.children.length>0" style="float:right">
              <i class="el-icon-arrow-right"></i>
            </span>
          </li>
          <div v-show="dataList.length==0" class="empty-text">
            暂无数据
          </div>
        </ul>
        <ul v-if="addMsg.Level==3" class="tierUl" style="margin-left:10px">
          <li style="cursor:pointer" @click="SelectTier2(item,index,3)" :class="active2==index ? 'SelectLi' :''"
            v-show="tableData2 && tableData2.children" v-for="(item,index) in tableData2.children" :key="index">
            <span>{{item.Name}}</span>
          </li>
          <div v-show="tableData2.children && tableData2.children.length==0" class="empty-text">
            暂无数据
          </div>
        </ul>
        <div style="text-align:right;margin-top:20px" slot="footer" class="dialog-footer">
          <el-button size="small" @click="tierDig=false">取消</el-button>
          <el-button type="primary" size="small" @click="tierDig=false">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import draggable from "vuedraggable"
  export default {
    components: {
      draggable
    },
    data() {
      return {
        loading: false,
        //是否显示新增分类
        IsShowCategory: true,
        dataList: [],
        msg: {
          Name: "", //页面名称
          pageIndex: 1,
          pageSize: 10,
        },
        addMsg: {
          Id: 0,
          Name: '', //分类名称
          Level: 1, //层级
          ParentId: 0, //父节点编号
          SortNum: 0, //排序
          FirstLevelName: "", //一级分类名称
          SecondLevelName: "", //二级分类名称
        },
        //第一层数据
        dataFirst: [],
        //第二层
        dataSecond: [],
        //第三层
        dataThird: [],
        activeIndex: 0,
        activeIndex2: -1,
        activeIndex3: -1,

        titleDig: '选择归属的一级分类',
        tierWidth: '31%',
        editState: false,
        tierDig: false,
        tableData: [], //弹窗选分类数据
        tableData2: [],
        active1: -1,
        active2: -1,
      };
    },
    methods: {
      //获取分类列表
      getList() {
        this.dataFirst = [];
        this.dataSecond = [];
        this.dataThird = [];
        this.loading = true;
        this.apipost("/api/Share/GetShareCategoryList", this.msg,
          res => {
            this.loading = false;
            if (res.data.resultCode === 1) {
              this.dataList = res.data.data;
              this.dataFirst = res.data.data;
              if (this.dataList && this.dataList.length > 0) {
                this.dataSecond = this.dataList[0].children;
                if (this.dataList[0].children && this.dataList[0].children.length > 0) {
                  this.dataThird = this.dataList[0].children[0].children
                }
              }
            }
          },
          null
        );
      },
      //新增修改分类
      submitform() {
        this.apipost("/api/Share/SetShareCategory", this.addMsg,
          res => {
            this.loading = false;
            if (res.data.resultCode === 1) {
              this.getList();
              this.Success(res.data.message);
              this.IsShowCategory = true;
              this.clearMsg();
            } else {
              this.Error(res.data.message);
            }
          },
          null
        );
      },
      //获取分类
      getCategory(Id) {
        this.apipost("/api/Share/GetShareCategory", {
            Id: Id
          },
          res => {
            if (res.data.resultCode === 1) {
              var tempObj = res.data.data;
              if (tempObj) {
                this.addMsg.Id = tempObj.Id;
                this.addMsg.Name = tempObj.Name;
                this.addMsg.Level = tempObj.Level;
                this.addMsg.ParentId = tempObj.ParentId;
                this.addMsg.FirstLevelName = tempObj.FirstLevelName;
                this.addMsg.SecondLevelName = tempObj.SecondLevelName;
                this.IsShowCategory = false;
              }
            } else {
              this.Error(res.data.message);
            }
          },
          null
        );
      },
      //删除分类
      removeCategory(Id) {
        let that = this;
        that.Confirm("确定要删除分类吗?", function () {
          that.apipost("/api/Share/RemoveShareCategory", {
              Id: Id
            },
            res => {
              if (res.data.resultCode === 1) {
                that.getList();
              } else {
                that.Error(res.data.message);
              }
            },
            null
          );
        });
      },
      clearMsg() {
        this.addMsg.Id = 0;
        this.addMsg.Name = "";
        this.addMsg.Level = 1;
        this.addMsg.ParentId = 0;
      },
      //点击一级分类
      getChild(item, index) {
        this.activeIndex = index;
        this.activeIndex2 = -1;
        this.activeIndex3 = -1;
        this.dataSecond = item.children;
        this.dataThird = [];
      },
      //点击第二季分类
      getSecond(item, index) {
        this.activeIndex2 = index;
        this.activeIndex3 = -1;
        this.dataThird = item.children;
      },
      //点击三级
      getThird(index) {
        this.activeIndex3 = index;
      },
      //点击新增分类
      addFenlei() {
        this.IsShowCategory = false;
        this.editState = false;
      },
      //跳转只修改
      goEdit(item) {
        this.editState = true;
      },
      //点击切换1 2 3级分类
      SelectTier(num) {
        if (num == 1) {
          this.active1 = -1;
          this.active2 = -1;
        }
        if (num == 2) {
          this.tierDig = true;
          this.titleDig = '选择归属的一级分类';
          this.tierWidth = '20%'
        }
        if (num == 3) {
          this.tierDig = true;
          this.titleDig = '选择归属的二级分类';
          this.tierWidth = '600px'
        }
      },
      //点击弹窗选择一级分类
      SelectTier1(item, index, num) {
        this.tableData2 = this.dataList[index];
        this.addMsg.FirstLevelName = item.Name;
        this.addMsg.ParentId = item.Id;
        this.active1 = index;
        this.active2 = -1;
      },
      //点击弹窗选择二级分类
      SelectTier2(item, index, num) {
        this.addMsg.SecondLevelName = item.Name;
        this.active2 = index;
        this.addMsg.ParentId = item.Id;
      },
      //批量修改排序
      saveSort() {
        this.sortList = [];
        this.dataFirst.forEach((item, index) => {
          item.children.forEach((item2, index2) => {
            let obj = {
              Id: item2.Id,
              SortNum: index2+1,
            }
            this.sortList.push(obj)
          })
          let obj = {
            Id: item.Id,
            SortNum: index+1,
          }
          this.sortList.push(obj)
        })
        this.apipost("/api/Share/SetShareCategorySortList", this.sortList, res => {
          if (res.data.resultCode == 1) {
            this.Success(res.data.message)
            this.getList();
          }
        })
      },
    },
    mounted() {
      this.getList();
    }
  };

</script>
<style>
  .shareCategory .templat-list {
    border: 1px solid #EBEEF5;
  }

  .shareCategory .templat-item {
    line-height: 40px;
    height: 40px;
    display: flex;
    font-size: 13px;
  }

  .templat-item:nth-child(odd) {
    background-color: #f7f7f7;
  }

  .templat-item:nth-child(even) {
    background-color: #fff;
  }

  .shareCategory .templat-item div {
    width: 50%;
    padding-left: 20px;
  }

  .shareCategory .Custom_item {
    border: 1px solid #EBEEF5;
    background-color: #FFF;
    color: #303133;
    -webkit-transition: .3s;
    transition: .3s;
    margin-bottom: 10px;
  }

  .shareCategory .Custom_item>div {
    padding: 20px;
  }

  .shareCategory .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }

  .shareCategory .conten_indder {
    padding: 20px;
    background-color: #fff;
    padding-right: 50%;
    min-width: 1100px;
  }

  .shareCategory .cat-list .card-item-box {
    margin-right: 5px;
  }

  .shareCategory .itemActive {
    background-color: #F5F5F5;
  }

  .shareCategory .ShareList {
    cursor: pointer;
    padding: 0 10px;
  }

  .shareCategory .el-card__body {
    padding: 0 !important;
    height: 500px;
    overflow: auto;
  }

  .shareCategory .tierUl .empty-text {
    line-height: 60px;
    color: #909399;
    text-align: center;
    border-bottom: 1px solid #EBEEF5;
  }

  .shareCategory .tierUl .SelectLi {
    background-color: #ecf5ff !important;
  }

  .shareCategory .tierUl li:hover {
    background-color: #F5F7FA;
  }

  .shareCategory .tierUl {
    border: 1px solid #E3E3E3;
    height: 250px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    width: 260px;
    overflow-y: auto;
    display: inline-block;
  }

  .shareCategory .tierUl li {
    border-bottom: 1px solid #EBEEF5;
    padding: 8px;
    box-sizing: border-box;
  }

  li {
    list-style: none;
  }

  .shareCategory .app-gallery-item .el-button {
    position: absolute;
    right: -8px;
    top: -8px;
    padding: 4px 4px !important;
  }

  .shareCategory .app-gallery-item {
    width: 80px;
    height: 80px;
    border: 1px solid #e3e3e3;
    border-radius: 2px;
    margin-right: 10px;
    margin-top: 10px;
    position: relative;
  }

  .shareCategory .setTable .el-table__body .cell {
    display: flex;
    align-items: center;

  }

  .shareCategory .discount .el-form-item__label {
    padding-right: 30px;
  }

  .shareCategory .el-form-item .elzk {
    position: absolute;
    left: -25px;
    top: 12px;
  }

</style>
