<template>
  <div class="admin-function" style="width:95%;margin:0 auto;border:1px solid #C0C0C0	;padding:20px">
    <el-form :inline="true" style="width:400px;">
      <el-form-item v-if="$permission('/admin/function/check')">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList" v-if="$permission('/admin/function/check')">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd(0)" v-if="$permission('/admin/function/add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="tableData.children"
        border
        header-row-class-name="custom-table-header"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="功能名称" align="" show-overflow-tooltip></el-table-column>
      <el-table-column label="所属应用" align="center" show-overflow-tooltip>
         <template slot-scope="scope">
              <span >{{tableData.applyName}}</span>
            </template>
      </el-table-column>
      <el-table-column prop="describe" label="功能描述" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sign" label="路径" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-dropdown split-button type="primary" size="small" >
            <span @click="handleEdit(scope.row)" v-if="$permission('/admin/function/modify')">编辑</span>
            <span v-else :disabled="true">编辑</span>
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <el-dropdown-item @click.native="handleAdd(scope.row.id,scope.row.apply,scope.row.name,scope.row)" v-if="$permission('/admin/function/add')">新增</el-dropdown-item>
              <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" divided v-if="$permission('/admin/function/delete')">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
      <div class="pagination">
      <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" :before-close="close">
      <el-form v-if="visible" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" label-position="left">
      
        <el-form-item label="所属功能" prop="function" v-if="show==true">
          <el-input v-model="ruleForm.last" placeholder="请输入功能名称" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="所属应用" prop="apply">
          <el-select v-model="ruleForm.apply" 
            filterable
            
            placeholder="请选择所属应用"
       
            :disabled="disabled"
            style="display:block">
            <el-option
              v-for="item in applyList"
              :key="item.value"
              :label="item.name"
              :value="item.id"
              >
              
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入功能名称"></el-input>
        </el-form-item>
        <el-form-item label="功能标识" prop="sign">
          <el-input v-model="ruleForm.sign" placeholder="请输入功能标识"></el-input>
        </el-form-item>
        <el-form-item label="功能描述" prop="describe">
          <el-input v-model="ruleForm.describe" placeholder="请输入" type="textarea" maxlength="100" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  props:{
    applyId:Number,
    required:true
  },
  data() {
    return {
      visible: false,
      loading: true,
      btnLoading: false,
      functionList:[],
      functionName:'',
      total: 0,
      show:true,
      disabled:false,
      tableData: [],
      title: '',
      applyList:[],
      queryParams: {
        size: 10,
        current: 1,
        name: '',
        apply:""
      },
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '请输入功能名称', trigger: 'blur' },],
        apply: [{ required: true, message: '请选择所属应用', trigger: 'change' },],
        sign: [{ required: true, message: '请输入功能标识', trigger: 'blur' },],
        // describe: [{ required: true, message: '请输入描述', trigger: 'blur' },],
      },
    }
  },
  watch:{
    "queryParams.apply"(val){
      console.log(val)
      this.queryParams.apply=val
    }
  },
  mounted() {
    console.log(this.applyId)
    this.queryParams.apply=this.applyId
    this.getList()
    
  },
  methods: {
      remoteMethod(query) {
        if (query !== '') {
          setTimeout(() => {
            this.functionList = this.functionList.filter(item => {
              return item.applicationName.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
            });
          }, 200);
        } else {
          this.$api.getApplyList().then(response => {
        console.log(response)
        this.applyList=response.data
      }).catch(error => {
        this.loading = false
      })
        }
      },
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
      }
    },
    getApplyList(){
     this.$api.getApplyList().then(response => {
        console.log(response.data)
        this.applyList=response.data
      }).catch(error => {
        this.loading = false
      })
    },
     handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    handleAdd(id,applyid,name,row) {
   
      this.title = '新增功能'
      this.visible = true
      if(id==0){
        this.show=false
        this.disabled=false
         this.ruleForm = {
         
        function: id
      }
      }else{
        this.show=true
        this.disabled=true
        // this.functionName=name
         this.ruleForm = {
           sign:row.sign,
        function: id,
        apply:applyid,
        path:row.path
      }
      if(row.path==0){
        this.ruleForm.last=name
      }else{
        this.ruleForm.last=row.last+'/'+name
      }
      }
     
    },
    handleEdit(row) {
      this.title = '编辑功能'
      this.visible = true
      // console.log(row)
      if(row.path==0){
        this.show=false
      }else{
        this.show=true
      }
      this.ruleForm = { ...row }
    },
    close() {
      this.visible = false
    },
    getList() {
      this.getApplyList()
      this.loading = true
      this.$api.getFunctionList(this.queryParams).then(response => {
        console.log(response)
        this.tableData = response.data
       var a = response.data.children
      for(var j = 0;j<a.length;j++){
        function l(data){
         if(data.hasOwnProperty('children')){
        for(var i = 0;i<data.children.length;i++){
         
          if(data.hasOwnProperty('last')){
            data.children[i].last=data.last+'/'+data.name
          }else{
            data.children[i].last=data.name
          }
          l(data.children[i])
        }
      }
      }
      // a[j].last=""
     l(a[j])
      }
        this.total=response.pageColumn.total
        this.loading = false
      }).catch(error => {
        this.loading = true
      })
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.ruleForm.id) {
            this.$api.editFunction(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            console.log(this.ruleForm)
            this.$api.addFunction(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deleteFunction({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-apply {

}
</style>