<template>
  <div class="container">
    <!-- 查询配置 -->
    <div style="padding: 0px 20px">
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="内容管理" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Input style="width: 200px; margin-right: 10px" v-model="searchFields.KeyWord" placeholder="关键字搜索" />
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
            </div>
          </slot>
        </LxHeader>
      </div>

      <el-row :gutter="20">
        <el-col :span="3">
          <Card class="box-card">
            <div style="overflow: auto;" :style="{height:tableHeight-32+'px'}">
              <el-scrollbar style="flex: 1">
                <el-tree class="filter-tree" highlight-current @node-click="menuClick" accordion :data="options"
                  :props="defaultProps" ref="tree">
                </el-tree>
              </el-scrollbar>
            </div>
          </Card>
        </el-col>
        <el-col :span="21">
          <div class="table-tree">
            <el-table :data="tableData" class="v-table" :height="tableHeight" border style="width: 100%">
              <el-table-column type="index" align="center">
              </el-table-column>
              <el-table-column prop="menu" align="left" label="归属模块" width="180">
                <template slot-scope="scope">
                  <el-tag>{{scope.row.menu?scope.row.menu.name:"无"}}</el-tag>
                </template>
              </el-table-column>

              <el-table-column prop="title" label="标题">
                <template slot-scope="scope">
                  {{scope.row.title}}
                </template>
              </el-table-column>
              <el-table-column prop="created_at" align="center" label="创建日期" width="180">
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="300">
                <template slot-scope="scope">
                  <Button ghost size="small" @click="show(scope.row)" type="primary"
                    style="margin-left: 10px;">查看</Button>
                </template>
              </el-table-column>
            </el-table>
            <div class="pagination">
              <el-pagination background @current-change="handleCurrentChange" :current-page="pagination.page"
                :page-size="pagination.page_size" layout="total,prev, pager, next" :total="pagination.total">
              </el-pagination>
            </div>
          </div>

        </el-col>
      </el-row>

    </div>
    <viewInfo ref="viewInfo"></viewInfo>
  </div>

</template>

<script>
  import {
    getToken
  } from '@/utils/auth'
  import LxHeader from "@/components/LxHeader/index.vue";
  import Treeselect from "@riophae/vue-treeselect";
  import Tinymce from '@/components/Tinymce'
  import viewInfo from './components/viewInfo.vue'
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {
    save,
    listarticle,
    store,
    del,
    get
  } from "@/api/lawsfile/article.js";
  import {
    listregulation
  } from "@/api/lawsfile/regulation.js";

  export default {
    components: {
      LxHeader,
      Treeselect,
      Tinymce,
      viewInfo
    },
    created() {
      var that = this;
      that.fileList = [];
      that.uploadOther.token = getToken();
      that.initLoad();
      that.loadregulation();
      that.load();
    },
    mounted() {},
    data() {
      return {
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        isShowViewFile: false,
        fileUrl: "",
        uploadOther: {
          token: ""
        },
        menu_children:[],
        dialogViewVisible: false,
        dialogFormVisible: false,
        formLabelWidth: "120px",
        form: {
          title: "",
          menu_id: null,
          content: "",
          file_list: ""
        },
        options: [],
        normalizer(node) {
          return {
            id: node.id,
            label: node.name,
            children: node.children,
          }
        },
        rules: {
          title: [{
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }],
          menu_id: [{
            required: true,
            message: '请选择归属的模块',
            trigger: 'blur'
          }]
        },
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          KeyWord: "",
          menu_id: ""
        },
        tableData: [],
        pagination: {
          page: 1,
          page_size: 15,
          total: 0
        },
        clientHeight: 0,
        fileList: [],
        filterText: ""
      }
    },
    methods: {

      menuClick(data) {

        let that = this;
        this.menu_children = []
        this.menu_children.push(data)
        if(data.children.length>0){
          this.hasChildren(data)
        }
        let menuids = ''
        if(this.menu_children.length>0){
          this.menu_children.map(item=>{
            menuids+=item.id+','
          })
          menuids = menuids.slice(0,-1)
        }
        that.searchFields.menu_id = menuids;
        that.load();
      },
      hasChildren(arr){
        if(arr.children.length>0){
          arr.children.map(item=>{
            this.menu_children.push(item)
            if(item.children.length>0){
              this.hasChildren(item)
            }
          })
        }else{
          this.menu_children.push(arr)
        }
      },
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      initLoad() {
        var that = this;
        var clientHeight = document.documentElement.clientHeight
        var lxHeader_height = 96.5; //查询 头部
        var paginationHeight = 37; //分页的高度
        var topHeight = 50; //页面 头部
        let tableHeight = clientHeight - lxHeader_height - topHeight - paginationHeight - 20;
        that.tableHeight = tableHeight;
      },
      handleCurrentChange(val) {
        this.pagination.page = val;
        this.load();
      },
      loadregulation() {
        var that = this;
        // let type = parseInt(this.$route.path.split("_")[1]);
         // let type  =  '2,1,3,12'
         let type = parseInt(this.$route.path.split("_")[1]);
        let type1 = parseInt(this.$route.path.split("_")[2]);
        // if(type1){
        //   type = type1
        // }else{
        //   // if(type==10){
        //   //   type = 10
        //   // }else{
        //     type  =  '2,1,3,12'
        //   // }
        // }
        listregulation(1, type).then(response => {
          that.options = response;
        }).catch(error => {
          console.log(error)
          //reject(error)
        })
      },
      load(istrue) {
        let type = parseInt(this.$route.path.split("_")[1]);
        var that = this;
        let type1 = parseInt(this.$route.path.split("_")[2]);
        // if(type1){
        //   type = type1
        // }else{
        //   // if(type==10){
        //   //   type = 10
        //   // }else{
        //     type  =  '2,1,3,12'
        //   // }
        // }
        listarticle(that.pagination.page, that.searchFields.menu_id, that.searchFields.KeyWord, that.pagination
          .page_size, type).then(response => {
          that.tableData = response.data;
          that.pagination.total = response.total;
        }).catch(error => {
          console.log(error)
          //reject(error)
        })
      },
      show(obj) {
        this.$refs.viewInfo.dialogViewVisible = true;
        this.$refs.viewInfo.getinfo(obj.id);

      },
      info(obj) {
        var that = this;
        get(obj.id).then(response => {
          let result = Object.assign(that.form, response);
          that.form = result;
          let _files = [];
          for (var mod of result.files) {
            let m = Object.assign({}, mod);
            m.name = mod.original_name;
            _files.push(m);
          }
          // that.fileList = _files;
          that.form.file_list = _files;
        }).catch(error => {
          //reject(error)
        })
      },
    }
  };
</script>

<style>
  .dialogConcent {
    overflow-y: auto;
  }
  .el-tree-node__label{
    text-wrap:wrap;
  }
  .el-tree-node__content{
    height:auto;
  }
</style>
