<template>
  <div class="manage-root organize-manage">
    <Header />
    <div class="manage-root-contain">
        <nav-menu class="manage-root-contain-left" :activeMenuIndex="activeMenuIndex"></nav-menu>
        <div class="manage-root-contain-right personnel-manage-right">
            <breadcrumb :breadcrumbList="breadcrumbList" class="breadcrumb-box"></breadcrumb>
            <div class="personal-inner">
                <div class="common-title-box">
                    <h3>机构列表</h3>
                    <div class="btn-box">
                        <el-button type="primary" size="small" @click="handleEdit">创建机构</el-button>
                    </div>
                </div>
                <div class="search-box">
                    <div class="search-item">
                        <label>机构名称</label>
                        <el-input
                            placeholder="输入搜索内容"
                            v-model="searchInput">
                            <i slot="suffix" class="el-input__icon el-icon-search" @click="getList(1)" style="cursor: pointer;"></i>
                        </el-input>
                    </div>
                    <div class="search-item">
                        <label>机构类型</label>
                        <el-select v-model="searchType" @change="getList(1)" placeholder="请选择">
                            <el-option
                                v-for="item in $orgTypeAll"
                                :key="item.type"
                                :label="item.type_name"
                                :value="item.type">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="search-item">
                        <label>状态</label>
                        <el-select v-model="searchStatus" @change="getList(1)" placeholder="请选择">
                            <el-option
                                v-for="item in searchStatusList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="search-item">
                        <label>所在地区</label>
                        <el-cascader
                            size="medium"
                            :options="provinceAndCityData"
                            v-model="searchArea"
                            @change="getList(1)">
                        </el-cascader>
                    </div>
                    <div class="search-item">
                        <label>创建人</label>
                        <el-select v-model="searchCreator" @change="getList(1)" placeholder="请选择">
                            <el-option
                                v-for="item in searchCreatorList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </div>
                <el-table
                    class="search-table"
                    :data="tableData"
                    style="width: 100%"
                    :max-height="tableHeight">
                    <el-table-column
                        prop="name"
                        label="机构名称"
                        sortable
                        min-width="268">
                    </el-table-column>
                    <el-table-column
                        prop="type_name"
                        label="机构类型"
                        width="112">
                    </el-table-column>
                    <el-table-column
                        prop="person_count_audited"
                        label="成员数"
                        width="85">
                    </el-table-column>
                    <el-table-column
                        prop="status"
                        label="状态"
                        width="80" >
                        <template slot-scope="scope">
                            <div class="status-box">
                                <span :style="{background:statusList[scope.row.status].bg}"></span>
                                <b :style="{color:statusList[scope.row.status].color}">{{statusList[scope.row.status].text}}</b>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="city_name"
                        label="所在地区"
                        width="132" >
                    </el-table-column>
                    <el-table-column
                        prop="creator_name"
                        label="创建人"
                        min-width="88">
                    </el-table-column>
                    <el-table-column
                        prop="manager_name"
                        label="管理员"
                        width="88">
                    </el-table-column>
                    <el-table-column
                        prop="create_time"
                        label="创建时间"
                        sortable
                        width="165">
                        <template slot-scope="scope">
                            {{scope.row.create_time?scope.row.create_time.substring(0,16):'-'}}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="effective_date_end"
                        label="到期时间"
                        sortable
                        width="165">
                    </el-table-column>
                    <el-table-column
                        fixed="right"
                        label="操作"
                        width="220">
                        <template slot-scope="scope">
                            <el-button
                                @click.native.prevent="handleEdit(scope.row)"
                                type="text"
                                size="small"
                                class="primary-btn">
                                信息
                            </el-button>
                            <el-button
                                @click.native.prevent="handlePerson(scope.row)"
                                type="text"
                                size="small"
                                class="primary-btn">
                                人员管理
                            </el-button>
                            <el-button
                                @click.native.prevent="handleUp(scope.row, scope.$index)"
                                type="text"
                                size="small"
                                class="primary-btn"
                                v-if="scope.row.status===0">
                                开启
                            </el-button>
                            <el-button
                                @click.native.prevent="handleUp(scope.row, scope.$index)"
                                type="text"
                                size="small"
                                class="red-btn"
                                v-else-if="scope.row.status===1">
                                停用
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNumber"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="pageSize"
                    layout="total, prev, pager, next, sizes, jumper"
                    :total="total_count">
                </el-pagination>
            </div>
        </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其它文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》from ‘《组件路径》';
import Header from "../../components/Header.vue";
import NavMenu from "../../components/NavMenu.vue"
import Breadcrumb from '../../components/Breadcrumb.vue';
import { getLogin } from "@/api/ajax";
import {
  provinceAndCityData
} from "element-china-area-data";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { Header, NavMenu, Breadcrumb },
  props: {},
  data() {
    //这里存放数据
    return {
        provinceAndCityData,
        activeMenuIndex: "organize_manage",
        breadcrumbList:[
            {
                icon:'school-line',
                url:'',
                text:''
            },
            {
                icon:'',
                url:'',
                text:'机构管理'
            }
        ],
        searchInput: '',
        searchType: -1,
        searchStatus: -1,
        searchCreator: '', 
        searchArea:"",
        searchStatusList:[
            {
                value:-1,
                label:'全部'
            },
            {
                value:1,
                label:'正常'
            },
            {
                value:0,
                label:'停用'
            }
        ],
        statusList:{
            '1':{
                text:'正常',
                bg:'#165DFF',
                color:''
            },
            '0':{
                text:'停用',
                bg:'#F53F3F',
                color:'#F53F3F'
            }
        },
        searchCreatorList:[
            {
                value:'',
                label:'全部'
            },
            {
                value:'1',
                label:'张三'
            },
            {
                value:'0',
                label:'李四'
            }
        ],
        tableData:[],
        pageSize: window.localStorage.getItem('pageSize')?Number(window.localStorage.getItem('pageSize')):10, 
        pageNumber: window.localStorage.getItem('pageNumber')?Number(window.localStorage.getItem('pageNumber')):1,
        tableHeight: "", // 表格高度
        total_count: 0
    }
  },
  //计算属性 类似于data概念
  computed: {
    
  },
  //监控data中数据变化
  watch: {},
  //方法集合
  methods: {
    // 查询列表
    getList(page){
        if(page){
            this.pageNumber = page
        }
        let MethodName = "/OrgServer/Manager/PageQuery/PageQueryOrgList";
        let data = {
            name: this.searchInput.trim(),
            type: this.searchType,
            status: this.searchStatus,
            area_id: this.searchArea,
            creator_id: this.searchCreator,
            page_capacity:this.pageSize,
            cur_page:this.pageNumber
        }
        getLogin(MethodName, data)
        .then((res) => {
            if(res.status===1){
               this.tableData = res.org_list
               this.total_count = res.total_count
            }
        })
        .catch(() => {
            this.loading = false
        });
    },
    // 创建机构或者编辑信息
    handleEdit(row){
        // 根据登录用户判断当前用户是不是超管 在table里加上disabled

        // 点击时记录页码和每页条数
        window.localStorage.setItem('pageSize',this.pageSize)
        window.localStorage.setItem('pageNumber',this.pageNumber)
        this.$router.push({
            path: "/createOrganize",
            query: {
                id: row?row.id:''
            },
        });
    },
    // 人员管理
    handlePerson(row){
        window.localStorage.setItem('pageSize',this.pageSize)
        window.localStorage.setItem('pageNumber',this.pageNumber)
        this.$router.push({
            path: "/peopleList",
            query: {
                id: row?row.id:''
            },
        });
    },
    // 停用 启用
    handleUp(row, index) {
      let Mname = "/OrgServer/Manager/OrgManager/EnableOrg";
      let updataData = JSON.parse(JSON.stringify(row));
      let data = {
        id: row.id
      };
      if (row.status === 0) {
        // 下架状态
        data.is_enable = "true";
        updataData.status = 1;
      } else if (row.status === 1) {
        data.is_enable = "false";
        updataData.status = 0;
      }
      getLogin(Mname, data).then(res => {
        this.$message.success("操作成功");
        this.$set(this.tableData, index, updataData);
      });
    },
    handleSizeChange(val) {
        this.pageSize = val
        this.pageNumber = 1
        this.getList()
    },
    handleCurrentChange(val) {
        this.pageNumber = val
        this.getList()
    },
    //计算table高度(动态设置table高度)
    getTableHeight() {
      let tableH = 370; //距离页面下方的高度
      let tableHeightDetil = window.innerHeight - tableH;
      if (tableHeightDetil <= 300) {
        this.tableHeight = 300;
      } else {
        this.tableHeight = window.innerHeight - tableH;
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getTableHeight();
    this.getList()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  //生命周期-创建之前
  beforeCreated() { },
  //生命周期-挂载之前
  beforeMount() { },
  //生命周期-更新之前
  beforUpdate() { },
  //生命周期-更新之后
  updated() { },
  //生命周期-销毁之前
  beforeDestory() { },
  //生命周期-销毁完成
  destoryed() { },
  //如果页面有keep-alive缓存功能，这个函数会触发
  activated() { }
}
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */

</style>
<style lang="scss">
.organize-manage{
    .el-cascader{
        width: 160px;
        height: 32px;
        line-height: 32px;
        .el-input{
            width: 100%;
            height: 32px;
        }
    }
}
</style>
