<template>
  <div>
    <el-tabs v-model="PartType" type="border-card" style="height: 800px;padding: 0">
      <el-tab-pane label="PC端菜单" name="PC端菜单">
        <el-row>
          <el-col :span="7">
            <el-card style="margin-bottom: 10px">
              <el-button size="small" type="distribution" @click="addFisrtLevel">创建一级菜单</el-button>
            </el-card>
            <el-card>
              <div slot="header">
                <el-input v-model="filterTextLeft" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-table ref="multipleTableLeft" :data="fisrtLevelFilter" stripe highlight-current-row size="mini" height="500" style="height:calc(100vh - 330px);" @selection-change="handleSelectionChangeLeft">
                <el-table-column :selectable="selectable" align="center" type="selection" width="55"/>
                <el-table-column align="center" label="模块名称">
                  <template slot-scope="scope">
                    {{ scope.row.title }}
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
          <el-col :span="1">
            <el-tooltip class="item" effect="dark" content="将选中一级菜单分配给该角色" placement="top-start">
              <el-button :disabled="!roleName || selectLeft.length === 0" size="mini" type="primary" icon="el-icon-right" style="margin: 300px auto 0 auto;display: block" @click="giveFirstLevel"/>
            </el-tooltip>
          </el-col>
          <el-col :span="8">
            <el-card style="margin-bottom: 10px">
              <span>角色:</span>
              <el-select v-model="roleName" placeholder="请选择" size="small" style="width:200px;margin-left:10px" filterable @change="getModule">
                <el-option
                  v-for="item in roleNames"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-card>
            <el-card style="height: 610px;overflow: auto">
              <div slot="header">
                <el-input v-model="filterText" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-tree ref="moduleTree" :data="moduleTree" :filter-node-method="filterNode" check-strictly class="moduleTree" show-checkbox node-key="id" default-expand-all @check-change="treeCheck">
                <span slot-scope="{ node, data }" class="custom-tree-node">
                  <span>{{ node.label }}</span>
                  <span>
                    <el-button
                      type="text"
                      size="mini"
                      icon="el-icon-top"
                      @click="() => moveUp(node, data)"/>
                    <el-button
                      type="text"
                      size="mini"
                      icon="el-icon-bottom"
                      @click="() => moveDown(node, data)"/>
                    <el-button
                      :disabled="data.children && data.children.length!==0"
                      type="text"
                      size="mini"
                      icon="el-icon-right"
                      @click="() => returnModule(node, data)"/>
                  </span>
                </span>
              </el-tree>
            </el-card>
          </el-col>
          <el-col :span="1">
            <el-tooltip class="item" effect="dark" content="将选中二级菜单分配给该角色选中的一级菜单" placement="top-start">
              <el-button :disabled="treeCheckList.length !== 1 || selectRight.length === 0 || treeCheckListJudge" size="mini" type="primary" icon="el-icon-back" style="margin: 300px auto 0 auto;display: block" @click="giveSecondLevel"/>
            </el-tooltip>
          </el-col>
          <el-col :span="7">
            <el-card style="margin-bottom: 10px">
              <!--<el-button size="small" type="distribution" @click="addSecondLevel">创建二级菜单</el-button>-->
              <el-button size="small" type="success" @click="RoleNameasdShow=!RoleNameasdShow">模块分配情况</el-button>
            </el-card>
            <el-card>
              <div slot="header">
                <el-input v-model="filterTextRight" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-table ref="multipleTableRight" :data="secondLevelFilter" stripe highlight-current-row size="mini" height="500" style="height:calc(100vh - 330px);" @selection-change="handleSelectionChangeRight">
                <el-table-column :selectable="selectable" align="center" type="selection" width="55"/>
                <el-table-column align="center" label="模块名称">
                  <template slot-scope="scope">
                    {{ scope.row.title }}
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="手机端菜单" name="手机端菜单">
        <el-row>
          <el-col :span="7">
            <el-card style="margin-bottom: 10px">
              <el-button size="small" type="distribution" @click="addFisrtLevel1">创建一级菜单</el-button>
            </el-card>
            <el-card>
              <div slot="header">
                <el-input v-model="filterTextLeft1" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-table ref="multipleTableLeft1" :data="fisrtLevelFilter1" stripe highlight-current-row size="mini" height="500" style="height:calc(100vh - 330px);" @selection-change="handleSelectionChangeLeft1">
                <el-table-column :selectable="selectable1" align="center" type="selection" width="55"/>
                <el-table-column align="center" label="模块名称">
                  <template slot-scope="scope">
                    {{ scope.row.title }}
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
          <el-col :span="1">
            <el-tooltip class="item" effect="dark" content="将选中一级菜单分配给该角色" placement="top-start">
              <el-button :disabled="!roleName1 || selectLeft1.length === 0" size="mini" type="primary" icon="el-icon-right" style="margin: 300px auto 0 auto;display: block" @click="giveFirstLevel1"/>
            </el-tooltip>
          </el-col>
          <el-col :span="8">
            <el-card style="margin-bottom: 10px">
              <span>角色:</span>
              <el-select v-model="roleName1" placeholder="请选择" size="small" style="width:200px;margin-left:10px" filterable @change="getModule1">
                <el-option
                  v-for="item in roleNames"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-card>
            <el-card style="height: 610px;overflow: auto">
              <div slot="header">
                <el-input v-model="filterText1" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-tree ref="moduleTree1" :data="moduleTree1" :filter-node-method="filterNode1" check-strictly class="moduleTree" show-checkbox node-key="id" default-expand-all @check-change="treeCheck1">
                <span slot-scope="{ node, data }" class="custom-tree-node">
                  <span>{{ node.label }}</span>
                  <span>
                    <el-button
                      type="text"
                      size="mini"
                      icon="el-icon-top"
                      @click="() => moveUp1(node, data)"/>
                    <el-button
                      type="text"
                      size="mini"
                      icon="el-icon-bottom"
                      @click="() => moveDown1(node, data)"/>
                    <el-button
                      :disabled="data.children && data.children.length!==0"
                      type="text"
                      size="mini"
                      icon="el-icon-right"
                      @click="() => returnModule1(node, data)"/>
                  </span>
                </span>
              </el-tree>
            </el-card>
          </el-col>
          <el-col :span="1">
            <el-tooltip class="item" effect="dark" content="将选中二级菜单分配给该角色选中的一级菜单" placement="top-start">
              <el-button :disabled="treeCheckList1.length !== 1 || selectRight1.length === 0 || treeCheckListJudge1" size="mini" type="primary" icon="el-icon-back" style="margin: 300px auto 0 auto;display: block" @click="giveSecondLevel1"/>
            </el-tooltip>
          </el-col>
          <el-col :span="7">
            <el-card style="margin-bottom: 10px">
              <!--<el-button size="small" type="distribution" @click="addSecondLevel">创建二级菜单</el-button>-->
              <el-button size="small" type="success" @click="RoleNameasdShow1=!RoleNameasdShow1">模块分配情况</el-button>
            </el-card>
            <el-card>
              <div slot="header">
                <el-input v-model="filterTextRight1" size="small" placeholder="输入关键字进行过滤" clearable/>
              </div>
              <el-table ref="multipleTableRight1" :data="secondLevelFilter1" stripe highlight-current-row size="mini" height="500" style="height:calc(100vh - 330px);" @selection-change="handleSelectionChangeRight1">
                <el-table-column :selectable="selectable1" align="center" type="selection" width="55"/>
                <el-table-column align="center" label="模块名称">
                  <template slot-scope="scope">
                    {{ scope.row.title }}
                  </template>
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>
    <el-row v-show="RoleNameasdShow">
      <el-card>
        <div slot="header">
          <el-select v-model="TitleValue" placeholder="请选择" size="small" style="width:200px;margin-left:10px" filterable @change="getTitle">
            <el-option
              v-for="item in Title"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </div>
        <el-table :data="RoleNameasd" stripe size="mini" height="200" border style="width: 465px">
          <el-table-column label="序号" align="center" type="index" width="50"/>
          <el-table-column align="center" label="角色名称">
            <template slot-scope="scope">
              {{ scope.row.角色名称 }}
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-row>

    <el-row v-show="RoleNameasdShow1">
      <el-card>
        <div slot="header">
          <el-select v-model="TitleValue1" placeholder="请选择" size="small" style="width:200px;margin-left:10px" filterable @change="getTitle1">
            <el-option
              v-for="item in Title1"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </div>
        <el-table :data="RoleNameasd1" stripe size="mini" height="200" border style="width: 465px">
          <el-table-column label="序号" align="center" type="index" width="50"/>
          <el-table-column align="center" label="角色名称">
            <template slot-scope="scope">
              {{ scope.row.角色名称 }}
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-row>

    <el-dialog :visible.sync="dialogVisible1" title="新增一级模块" center style="min-height: 300px" width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="模块名称" prop="moduleName">
          <el-input
            v-model="form1.moduleName"
            size="small"
            style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1=false">取消</el-button>
        <el-button type="primary" @click="submitAdd()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible2" title="新增一级模块" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="模块名称" prop="moduleName">
          <el-input
            v-model="form2.moduleName"
            size="small"
            style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2=false">取消</el-button>
        <el-button type="primary" @click="submitAdd1()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ExecDatabase } from '@/main'
import request from '@/utils/request'
export default {
  name: 'MenuManagement',
  data() {
    return {
      PartType: 'PC端菜单',
      roleName: null,
      roleName1: null,
      roleNames: [],
      filterText: '',
      filterText1: '',
      filterTextLeft: '',
      filterTextLeft1: '',
      filterTextRight: '',
      filterTextRight1: '',
      allModuleId: [],
      allModuleId1: [],
      moduleTree: [],
      moduleTree1: [],
      fisrtLevel: [],
      fisrtLevel1: [],
      fisrtLevelFilter: [],
      fisrtLevelFilter1: [],
      secondLevel: [],
      secondLevel1: [],
      secondLevelFilter: [],
      secondLevelFilter1: [],
      selectLeft: [],
      selectLeft1: [],
      selectRight: [],
      selectRight1: [],
      treeCheckList: [],
      treeCheckList1: [],
      treeCheckListJudge: null,
      treeCheckListJudge1: null,
      dialogVisible1: false,
      dialogVisible2: false,
      form1: {
        moduleName: ''
      },
      form2: {
        moduleName: ''
      },
      rules1: {
        moduleName: [{ required: true, message: '请输入', trigger: ['blur', 'change'] }]
      },
      rules2: {
        moduleName: [{ required: true, message: '请输入', trigger: ['blur', 'change'] }]
      },
      RoleNameasdShow: false,
      RoleNameasdShow1: false,
      TitleValue: '',
      TitleValue1: '',
      Title: [],
      Title1: [],
      RoleNameasd: [],
      RoleNameasd1: []
    }
  },
  watch: {
    filterText(val) {
      this.$refs.moduleTree.filter(val)
    },
    filterText1(val) {
      this.$refs.moduleTree1.filter(val)
    },
    filterTextLeft(val) {
      this.fisrtLevelFilter = this.fisrtLevel.filter(v => {
        return v.title.indexOf(val) > -1
      })
    },
    filterTextLeft1(val) {
      this.fisrtLevelFilter1 = this.fisrtLevel1.filter(v => {
        return v.title.indexOf(val) > -1
      })
    },
    filterTextRight(val) {
      this.secondLevelFilter = this.secondLevel.filter(v => {
        return v.title.indexOf(val) > -1
      })
    },
    filterTextRight1(val) {
      this.secondLevelFilter1 = this.secondLevel1.filter(v => {
        return v.title.indexOf(val) > -1
      })
    }
  },
  created() {
    this.getRoleName()
    this.initFisrtLevel()
    this.initSecondLevel()
    this.initFisrtLevel1()
    this.initSecondLevel1()
    this.getRoleTitle()
    this.getRoleTitle1()
  },
  methods: {
    // 撤回分配模块
    returnModule(node, data) {
      console.log(node, data, 123)
      this.$confirm('确定撤回分配模块?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', data.id]
        param[1] = ['角色编号', this.roleName]
        var Data = this.CreateData('12', '菜单管理_角色模块_删除数据', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.filterText = ''
            this.filterTextLeft = ''
            this.filterTextRight = ''
            this.getModule()
            this.initFisrtLevel()
            this.initSecondLevel()
            this.$message.success('操作成功')
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    returnModule1(node, data) {
      console.log(node, data, 123)
      this.$confirm('确定撤回分配模块?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', data.id]
        param[1] = ['角色编号', this.roleName1]
        var Data = this.CreateData('12', '菜单管理_角色模块_删除数据_手机端', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.filterText1 = ''
            this.filterTextLeft1 = ''
            this.filterTextRight1 = ''
            this.getModule1()
            this.initFisrtLevel1()
            this.initSecondLevel1()
            this.$message.success('操作成功')
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    moveUp(node, data) {
      var param = []
      param[0] = ['id', data.id]
      param[1] = ['角色编号', this.roleName]
      var Data = this.CreateData('11', '菜单管理_角色模块_上移数据_验证', param)
      ExecDatabase(Data).then(response => {
        if (Number(response.data[0].角色模块顺序) !== 1) {
          var Data1 = this.CreateData('12', '菜单管理_角色模块_上移数据', param)
          ExecDatabase(Data1).then(response => {
            if (response.data[0].result === '1') {
              this.filterText = ''
              this.filterTextLeft = ''
              this.filterTextRight = ''
              this.getModule()
              this.initFisrtLevel()
              this.initSecondLevel()
              this.initFisrtLevel1()
              this.initSecondLevel1()
              this.$message.success('上移成功')
            } else { this.$message.error('上移失败') }
          })
        } else { this.$message.error('当前已经排在第一') }
      })
    },
    moveUp1(node, data) {
      var param = []
      param[0] = ['id', data.id]
      param[1] = ['角色编号', this.roleName1]
      var Data = this.CreateData('11', '菜单管理_角色模块_上移数据_验证_手机端', param)
      ExecDatabase(Data).then(response => {
        if (Number(response.data[0].角色模块顺序) !== 1) {
          var Data1 = this.CreateData('12', '菜单管理_角色模块_上移数据_手机端', param)
          ExecDatabase(Data1).then(response => {
            if (response.data[0].result === '1') {
              this.filterText1 = ''
              this.filterTextLeft1 = ''
              this.filterTextRight1 = ''
              this.getModule1()
              this.initFisrtLevel1()
              this.initSecondLevel1()
              this.$message.success('上移成功')
            } else { this.$message.error('上移失败') }
          })
        } else { this.$message.error('当前已经排在第一') }
      })
    },
    moveDown(node, data) {
      var param = []
      param[0] = ['id', data.id]
      param[1] = ['角色编号', this.roleName]
      var Data = this.CreateData('11', '菜单管理_角色模块_下移数据_验证', param)
      ExecDatabase(Data).then(response => {
        if (response.data[0].result === 1) {
          var Data1 = this.CreateData('12', '菜单管理_角色模块_下移数据', param)
          ExecDatabase(Data1).then(response => {
            if (response.data[0].result === '1') {
              this.filterText = ''
              this.filterTextLeft = ''
              this.filterTextRight = ''
              this.getModule()
              this.initFisrtLevel()
              this.initSecondLevel()
              this.$message.success('操作成功')
            } else { this.$message.error('操作失败') }
          })
        } else {
          this.$message.error('当前已经排在最后')
        }
      })
    },
    moveDown1(node, data) {
      var param = []
      param[0] = ['id', data.id]
      param[1] = ['角色编号', this.roleName1]
      var Data = this.CreateData('11', '菜单管理_角色模块_下移数据_验证_手机端', param)
      ExecDatabase(Data).then(response => {
        if (response.data[0].result === 1) {
          var Data1 = this.CreateData('12', '菜单管理_角色模块_下移数据_手机端', param)
          ExecDatabase(Data1).then(response => {
            if (response.data[0].result === '1') {
              this.filterText1 = ''
              this.filterTextLeft1 = ''
              this.filterTextRight1 = ''
              this.getModule1()
              this.initFisrtLevel1()
              this.initSecondLevel1()
              this.$message.success('操作成功')
            } else { this.$message.error('操作失败') }
          })
        } else {
          this.$message.error('当前已经排在最后')
        }
      })
    },
    // 分配一级
    giveFirstLevel() {
      this.$confirm('确定将选中的一级模块分配给选中角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['一级模块id组', this.selectLeft]
        param[1] = ['角色编号', this.roleName]
        var Data = this.CreateData('12', '菜单管理_角色模块_一级_增加数据', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.filterText = ''
            this.filterTextLeft = ''
            this.filterTextRight = ''
            this.getModule()
            this.initFisrtLevel()
            this.initSecondLevel()
            this.$message.success('操作成功')
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    giveFirstLevel1() {
      this.$confirm('确定将选中的一级模块分配给选中角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['一级模块id组', this.selectLeft1]
        param[1] = ['角色编号', this.roleName1]
        var Data = this.CreateData('12', '菜单管理_角色模块_一级_增加数据_手机端', param)
        ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.filterText1 = ''
            this.filterTextLeft1 = ''
            this.filterTextRight1 = ''
            this.getModule1()
            this.initFisrtLevel1()
            this.initSecondLevel1()
            this.$message.success('操作成功')
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    // 分配二级
    giveSecondLevel() {
      if (this.$refs.moduleTree.getCheckedKeys().length === 1) {
        this.$confirm('确定将选中的二级模块分配给选中角色的选中一级模块?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var param = []
          param[0] = ['二级模块id组', this.selectRight]
          param[1] = ['角色编号', this.roleName]
          param[2] = ['pid', this.$refs.moduleTree.getCheckedKeys()]
          var Data = this.CreateData('12', '菜单管理_角色模块_二级_增加数据', param)
          ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.filterText = ''
              this.filterTextLeft = ''
              this.filterTextRight = ''
              this.getModule()
              this.initFisrtLevel()
              this.initSecondLevel()
              this.$message.success('操作成功')
            } else { this.$message.error('操作失败') }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          })
        })
      } else {
        this.$message.error('只可以分配给一个一级菜单')
      }
    },
    giveSecondLevel1() {
      if (this.$refs.moduleTree1.getCheckedKeys().length === 1) {
        this.$confirm('确定将选中的二级模块分配给选中角色的选中一级模块?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var param = []
          param[0] = ['二级模块id组', this.selectRight1]
          param[1] = ['角色编号', this.roleName1]
          param[2] = ['pid', this.$refs.moduleTree1.getCheckedKeys()]
          var Data = this.CreateData('12', '菜单管理_角色模块_二级_增加数据_手机端', param)
          ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.filterText1 = ''
              this.filterTextLeft1 = ''
              this.filterTextRight1 = ''
              this.getModule1()
              this.initFisrtLevel1()
              this.initSecondLevel1()
              this.$message.success('操作成功')
            } else { this.$message.error('操作失败') }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          })
        })
      } else {
        this.$message.error('只可以分配给一个一级菜单')
      }
    },
    // 控制某行是否可选
    selectable(row) {
      return !this.allModuleId.includes(row.id)
    },
    selectable1(row) {
      return !this.allModuleId1.includes(row.id)
    },
    // 树checkChange
    treeCheck() {
      const param = []
      this.treeCheckList = this.$refs.moduleTree.getCheckedKeys()
      this.fisrtLevel.map(v => {
        param.push(v.id)
      })
      this.treeCheckListJudge = !param.includes(parseInt(this.treeCheckList))
    },
    treeCheck1() {
      const param = []
      this.treeCheckList1 = this.$refs.moduleTree1.getCheckedKeys()
      this.fisrtLevel1.map(v => {
        param.push(v.id)
      })
      this.treeCheckListJudge1 = !param.includes(parseInt(this.treeCheckList1))
    },
    // leftChange
    handleSelectionChangeLeft(val) {
      this.selectLeft = []
      val.map(v => {
        this.selectLeft.push(v.id)
      })
    },
    handleSelectionChangeLeft1(val) {
      this.selectLeft1 = []
      val.map(v => {
        this.selectLeft1.push(v.id)
      })
    },
    // rightChange
    handleSelectionChangeRight(val) {
      this.selectRight = []
      val.map(v => {
        this.selectRight.push(v.id)
      })
    },
    handleSelectionChangeRight1(val) {
      this.selectRight1 = []
      val.map(v => {
        this.selectRight1.push(v.id)
      })
    },
    // 初始化一级
    async initFisrtLevel() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select id,title from 基础表_模块信息 where [path] = '/'`
        }
      }).then(res => {
        this.fisrtLevel = res.data
        this.fisrtLevelFilter = res.data
      })
    },
    async initFisrtLevel1() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select id,title from 基础表_模块信息_手机端 where [path] = '/'`
        }
      }).then(res => {
        this.fisrtLevel1 = res.data
        this.fisrtLevelFilter1 = res.data
      })
    },
    // 初始化二级
    async initSecondLevel() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select id,title from 基础表_模块信息 where [path] <> '/' and 是否禁用 = 0  `
        }
      }).then(res => {
        this.secondLevel = res.data
        this.secondLevelFilter = res.data
      })
    },
    async initSecondLevel1() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select id,title from 基础表_模块信息_手机端 where [path] <> '/' and 是否禁用 = 0  `
        }
      }).then(res => {
        this.secondLevel1 = res.data
        this.secondLevelFilter1 = res.data
      })
    },
    // 新增一级
    addFisrtLevel() {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.moduleName = ''
      this.dialogVisible1 = true
    },
    addFisrtLevel1() {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.form2.moduleName = ''
      this.dialogVisible2 = true
    },
    // 确认新增一级
    submitAdd() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          this.dialogVisible1 = false
          var param = []
          param[0] = ['模块名称', this.form1.moduleName]
          var Data = this.CreateData('12', '菜单管理_模块_一级_增加数据', param)
          ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('操作成功')
              this.dialogVisible1 = false
              this.initFisrtLevel()
            } else {
              this.$message.error('操作失败')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submitAdd1() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          this.dialogVisible2 = false
          var param = []
          param[0] = ['模块名称', this.form2.moduleName]
          var Data = this.CreateData('12', '菜单管理_模块_一级_增加数据_手机端', param)
          ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('操作成功')
              this.dialogVisible2 = false
              this.initFisrtLevel1()
            } else {
              this.$message.error('操作失败')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 新增二级
    addSecondLevel() {
      console.log(1)
    },
    // 递归
    fn(data, pid) {
      const result = []
      let temp
      for (let i = 0; i < data.length; i++) {
        if (data[i].pid === pid) {
          const obj = {
            'id': data[i].id,
            'label': data[i].title
          }
          temp = this.fn(data, data[i].id)
          if (temp.length > 0) {
            obj.children = temp
          }
          result.push(obj)
        }
      }
      return result
    },
    // 初始化角色
    getRoleName() {
      var param = []
      var Data = this.CreateData('11', '基础数据_角色_查询', param)
      ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.roleNames.push({
            value: response.data[i].角色编号,
            label: response.data[i].角色名称
          })
        }
      })
    },
    // 根据角色查模块
    async getModule() {
      this.$refs.multipleTableLeft.clearSelection()
      this.$refs.multipleTableRight.clearSelection()
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select * from 基础表_角色模块信息 inner join 基础表_模块信息 on 基础表_角色模块信息.id = 基础表_模块信息.id where 角色编号=${this.roleName} order by 角色模块顺序`
        }
      }).then(response => {
        this.allModuleId = []
        response.data.map(v => {
          this.allModuleId.push(v.id)
        })
        this.moduleTree = this.fn(response.data, 0)
      })
    },
    async getModule1() {
      this.$refs.multipleTableLeft1.clearSelection()
      this.$refs.multipleTableRight1.clearSelection()
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select * from 基础表_角色模块信息_手机端 inner join 基础表_模块信息_手机端 on 基础表_角色模块信息_手机端.id = 基础表_模块信息_手机端.id where 角色编号=${this.roleName1} order by 角色模块顺序`
        }
      }).then(response => {
        this.allModuleId1 = []
        response.data.map(v => {
          this.allModuleId1.push(v.id)
        })
        this.moduleTree1 = this.fn(response.data, 0)
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    filterNode1(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 初始化模块
    async getRoleTitle() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select distinct title from
              基础表_模块信息 inner join 基础表_角色模块信息 on 基础表_角色模块信息.id = 基础表_模块信息.id
              inner join 基础数据_角色 on 基础表_角色模块信息.角色编号 = 基础数据_角色.角色编号
              order by title`
        }
      }).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Title.push({
            value: response.data[i].title,
            label: response.data[i].title
          })
        }
      })
    },
    async getRoleTitle1() {
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select distinct title from
              基础表_模块信息_手机端 inner join 基础表_角色模块信息_手机端 on 基础表_角色模块信息_手机端.id = 基础表_模块信息_手机端.id
              inner join 基础数据_角色 on 基础表_角色模块信息_手机端.角色编号 = 基础数据_角色.角色编号
              order by title`
        }
      }).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Title1.push({
            value: response.data[i].title,
            label: response.data[i].title
          })
        }
      })
    },
    async getTitle() {
      this.RoleNameasd = []
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select distinct 角色名称 from
              基础表_模块信息 inner join 基础表_角色模块信息 on 基础表_角色模块信息.id = 基础表_模块信息.id
              inner join 基础数据_角色 on 基础表_角色模块信息.角色编号 = 基础数据_角色.角色编号
              where title=\'${this.TitleValue}\'`
        }
      }).then(response => {
        this.RoleNameasd = response.data
      })
    },
    async getTitle1() {
      this.RoleNameasd1 = []
      await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select distinct 角色名称 from
              基础表_模块信息_手机端 inner join 基础表_角色模块信息_手机端 on 基础表_角色模块信息_手机端.id = 基础表_模块信息_手机端.id
              inner join 基础数据_角色 on 基础表_角色模块信息_手机端.角色编号 = 基础数据_角色.角色编号
              where title=\'${this.TitleValue1}\'`
        }
      }).then(response => {
        this.RoleNameasd1 = response.data
      })
    }
  }
}
</script>

<style scoped>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
