<template>
  <div class="admin-client">
    <el-form :inline="true">
      <el-form-item v-if="$permission('/admin/client/check')">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList2" v-if="$permission('/admin/client/check')">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd()" v-if="$permission('/admin/client/add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
      <el-table-column type="index" label="#" width="55" align="center"></el-table-column>
      <el-table-column prop="name" label="租户名称" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="contacts" label="联系人" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="address" label="联系人地址" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="type" label="类型" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="dictionaries.TENANT_TYPE">
            {{(dictionaries.TENANT_TYPE.filter(item=>item.dictChildValue==scope.row.type))[0].dictChildLabel}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" show-overflow-tooltip>
         <template slot-scope="scope">
        <span v-if="scope.row.state==0">否</span>
        <span v-if="scope.row.state==1">是</span>
      </template>
      </el-table-column>
      <el-table-column prop="describe" 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" >
            <p v-if="$permission('/admin/client/modify')" @click="handleEdit(scope.row)">编辑</p>
            <p v-else :disabled="true">编辑</p>
        <!-- <el-button type="primary" size="small"  @click="handleEdit(scope.row)" v-if="$permission('/admin/client/modify')">
				<span>编辑</span>
			</el-button>
			<el-button type="primary" size="small" v-else :disabled="true">
				 <span >编辑</span>
			</el-button> -->
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <!-- <el-dropdown-item @click.native="handleDetails(scope.row)" v-if="$permission('/admin/client/detail')">详情</el-dropdown-item> -->
              <el-dropdown-item @click.native="handleReset(scope.row)" v-if="$permission('/admin/client/reset')">重置密码</el-dropdown-item>
              <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" divided v-if="$permission('/admin/client/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="110px" label-position="left">

        <!-- <el-form-item label="租户名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入租户名称"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea"></el-input>
        </el-form-item> -->

        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="租户名称" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
            </el-form-item>
          </el-col>
         
        </el-row>
         <el-row :gutter="24">
          <el-col :span="24">
              <el-form-item label="联系人" prop="contacts">
              <el-input v-model="ruleForm.contacts" placeholder="请输入联系人"></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
         <el-row :gutter="24">
          <el-col :span="24">
              <el-form-item label="联系人邮箱" prop="email">
              <el-input v-model="ruleForm.email" placeholder="请输入联系人邮箱"></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
          <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="联系地址" prop="address">
              <el-input type="textarea" v-model="ruleForm.address" placeholder="请输入联系地址"></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="类型" prop="type">
              <!-- <el-input v-model="ruleForm.type" placeholder="请输入类型"></el-input> -->
              <el-select v-model="ruleForm.type" :filterable="true" placeholder="请选择类型" style="width: 100%" @change="getType">
                <el-option v-for="item in dictionaries.TENANT_TYPE" :key="item.id" :value="item.dictChildValue" :label="item.dictChildLabel"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
         
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="描述" prop="describe">
              <el-input type="textarea" v-model="ruleForm.describe" placeholder="请输入描述" maxlength="100" show-word-limit></el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="用户账号配额" prop="number" >
              <el-input-number style="width:100%" controls-position="right" :max="99" :min="1" v-model="ruleForm.number" :step="10" placeholder="请输入用户账号配额"></el-input-number>
            </el-form-item>
          </el-col>
        
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="管理员账号" prop="username">
              <el-input v-model="ruleForm.username" placeholder="请输入管理员账号"></el-input>
            </el-form-item>
          </el-col>
         
        </el-row>
        <el-row :gutter="24" v-if="pswdshow">
          <el-col :span="24">
            <el-form-item label="管理员密码" prop="password">
              <el-input v-model="ruleForm.password" placeholder="请输入管理员密码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="会员类别" prop="tenantVipType">
              <el-col  style="padding-left: 0px;padding-right: 0px;">
                <el-select v-model="ruleForm.tenantVipType" placeholder="请选择会员类别" :filterable="true" style="width: 260px; margin-right: 20px;">
                  <el-option v-for="item in memberTypes"
                  :key="item.id"
                  :label="item.tenantVipName"
                  :value="item.id">
                  </el-option>
                </el-select>
                <el-tooltip content="点击查看当前选择的会员类别及功能权限" placement="top">
                  <!-- <el-link type="primary" @click="memberTypeBtn">会员类别</el-link> -->
                  <el-button @click="memberTypeBtn">查看</el-button>
                </el-tooltip>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="功能权限" prop="function">
           <el-cascader
              ref="FunctionCascader"
              v-model="ruleForm.function"
              :options="authority"
              :props="{multiple:true,value:'id',label:'name' }"
              collapse-tags
              placeholder="请输入功能权限"
              clearable
              @change="downloadChange"
              style="width:100%"></el-cascader>
        </el-form-item>
          </el-col>
        </el-row> -->
        <el-form-item label="是否启用" prop="state">
                  <el-switch
          :active-value="1" 
          :inactive-value="0"
           v-model="ruleForm.state"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
          <!-- <el-input v-model="ruleForm.state" placeholder="请输入状态" type="textarea"></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>

    <el-dialog :title="title" :visible.sync="showvisible" width="700px" class="my-dialog-name" contentStyle="width:400px;" :before-close="close" v-if="ruleForm.apply!=null">
        <el-form label-position="left"  class="demo-table-expand" label-width="110px">
          <el-form-item label="租户名称 :" class="marginB">
            <span>{{ruleForm.apply.name}}</span>
          </el-form-item>
          <el-form-item label="联系人 :" class="marginB">
            <span>{{ruleForm.apply.contacts}}</span>
          </el-form-item>
          <el-form-item label="联系人邮箱 :" class="marginB">
            <span>{{ruleForm.apply.email}}</span>
          </el-form-item>
          <el-form-item label="联系地址 :" class="marginB">
            <span>{{ruleForm.apply.address}}</span>
          </el-form-item>
          <el-form-item label="描述 :" class="marginB">
            <span>{{ruleForm.apply.describe}}</span>
          </el-form-item>

          <el-form-item label="用户账号配额 :" class="marginB">
            <span>{{ruleForm.apply.number}}</span>
          </el-form-item>

          <el-form-item label="管理员 :" class="marginB">
            <span>{{ruleForm.apply.username}}</span>
          </el-form-item>
          <el-form-item label="可用功能权限 :" class="marginB">
            <span v-for="item in ruleForm.apply.functionList" :key="item.id">{{item.name}} </span>
          </el-form-item>
          <el-form-item label="租户状态 :" class="marginB">
            <span>{{ruleForm.apply.state === 1 ? '已启用' : '未启用'}}</span>
          </el-form-item>

          <el-form-item label="租户类型 :" class="marginB">
            <span v-if="dictionaries.TENANT_TYPE">
               {{((dictionaries.TENANT_TYPE).filter(item=>item.dictChildValue==ruleForm.apply.type))[0].dictChildLabel}}
            </span>
          </el-form-item>
          <!-- <el-form-item label="备注 :" class="marginB">
            <span>{{ruleForm.apply.describe}}</span>
          </el-form-item> -->
        </el-form>

    </el-dialog>
    <!-- 会员类别管理弹窗 -->
    <el-dialog title="会员类别查看" :visible.sync="memberVisible" :before-close="closeMember" append-to-body width="500px">
        <!-- <MemberType></MemberType> -->
        <el-form  :model="memberTableData"  label-width="80px" label-position="left">
          <el-form-item label="会员类型" prop="tenantVipName">
            <!-- <span>{{ memberTableData.tenantVipName }}</span> -->
            <el-input v-model="memberTableData.tenantVipName" placeholder="请输入会员类型名称" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建人" prop="createPersonName">
            <!-- <span>{{ memberTableData.createPersonName }}</span> -->
            <el-input v-model="memberTableData.createPersonName" placeholder="请输入创建人" disabled></el-input>
          </el-form-item>
          <el-form-item label="功能权限" prop="functions" style="margin-top: 20px;">
                        <el-cascader
                              ref="FunctionCascader"
                              v-model="memberTableData.functions"
                              :options="authority"
                              :props="{multiple:true,value:'id',label:'name' }"
                              collapse-tags
                              placeholder="请选择功能权限"
                              clearable
                              style="width: 100%;"></el-cascader>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <!-- <span>{{ memberTableData.remark }}</span> -->
            <el-input v-model="memberTableData.remark" placeholder="请输入备注" type="textarea" maxlength="100"
                      show-word-limit disabled></el-input>
          </el-form-item>
        </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="closeMember">取 消</el-button>
        <el-button type="primary" @click="submitMember">确 定</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
// import MemberType from "@/views/memberType/index.vue"
export default {
  components: {
    // MemberType,
  },
  data() {
    return {
      saveNodeTmp:[],
      functions:[],
      trans: [],
      authority: [],
      select:'',
      pswdshow: false,
      showvisible: false,
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      memberTypes: [],
      memberVisible: false,
      MemberLoading: false,
      memberTableData: {},
      title: '',
      queryParams: {
        size: 10,
        current: 1,
        name: ''
      },
      ruleForm: {
      },
     rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' },],
        contacts:[{ required: true, message: '请输入联系人', trigger: 'blur' },],
        // email:[{ required: true, message: '请输入联系人邮箱', trigger: 'blur' },],
        address: [{ required: true, message: '请输入联系地址', trigger: 'blur' },],
        number: [{ required: true, message: '请输入用户账号配额', trigger: 'blur' },],
        username:[{ required: true, message: '请输入管理员账号', trigger: 'blur' },],
        password:[{ required: true, message: '请输入管理员密码', trigger: 'blur' },],
        type:[{ required: true, message: '请选择类型', trigger: 'change' },],
        	email:[{required:true,message: "请输入邮箱", trigger: "blur"},
      		{pattern:/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
       		 message:'请输入正确的邮箱格式',trigger:'blur'}],
      },
    }
  },
  mounted() {
    this.getList()
    this.getFunction()
    this.getListMemberType()
    // this.translate()
  },
  created(){
    // this.select = this.$store.state.admin.dictionaries
  },
    computed: {
    dictionaries() {
      return this.$store.state.admin.dictionaries
    },
   
  },
  methods: {
    getType(val){
        // this.getFunction(val)
    },
     //根据条件查询
     getList2(){
        this.queryParams.current=1
        this.getList()
      },
    handleAdd() {
      this.title = '新增租户'
      this.visible = true
      this.pswdshow = true
      this.ruleForm = {
         state:1
      }
    },
    handleEdit(row) {
      this.title = '编辑租户'
      this.visible = true
      this.pswdshow = false
        // this.authority = []
        // this.getFunction(row.type)
       this.ruleForm = { ...row }
    },
    handleDetails(row) {
      this.title = '租户详情'
      this.showvisible = true
      this.ruleForm.apply = row
    },
    close() {
      this.visible = false
      this.showvisible = false
      this.pswdshow = false
    },
    // translate() {
    //   for (let i = 0; i < this.select.length; i++) {
    //     this.trans.push(this.select[i].dictChildLabel)
    //   }
    // },
    getList() {
      this.loading = true
      this.$api.getTenantList(this.queryParams).then(response => {
        this.tableData = response.data
        this.total = response.pageColumn.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      });
    },
    // 请求会员类别权限
    getListMemberType() { 
      this.$api.queryTenantVipType({}).then(res => {
        if (res.code == 200) {
          this.memberTypes = res.data.list
        }
      })
    },
    //会员类别查看按钮
    memberTypeBtn() {
      this.memberTableData=this.memberTypes.filter(item => {
          return  item.id==this.ruleForm.tenantVipType
      })[0]
      // this.memberTableData = a[0]
      this.memberVisible=true
      
    },
    // 会员类别弹窗确定
    // submitMember() {
    //   this.closeMember()
    // },
    // 会员类别弹窗取消
    closeMember() {
      this.memberVisible = false
      this.memberTableData={}
    },
    // getCheckedNodes(){
    //  var List = this.$refs.FunctionCascader.getCheckedNodes()
    //  List.forEach(item => {
    //  });

    // },
    findAllNode(data) {
      if (!this.saveNodeTmp.includes(data.path)) { 
        if(data.path.length>1){
          this.saveNodeTmp.push(data.path)
        }   
        
      }
      if (data.parent) {
        this.findAllNode(data.parent)
      }
    },
    // change时
    downloadChange(data) {
      let readSelectedAllNode = this.$refs.FunctionCascader.getCheckedNodes()
      this.saveNodeTmp = []
      readSelectedAllNode.forEach(item => {
        this.findAllNode(item)
      })
      this.functions = [...this.saveNodeTmp]
      
    },

    getFunction() {
      this.$api.getAllFunctionList({}).then(response => {
        this.authority = []
        this.authority = response.data
        // this.tableData = response.data
        // this.total=response.pageColumn.total
        // this.loading = false
      }).catch(error => {
        this.loading = true
      })
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
        //   this.ruleForm.function = this.functions
        //    this.ruleForm.functions=[]
        //    if(this.ruleForm.hasOwnProperty('function')){
        //      for(var i =0;i<this.ruleForm.function.length;i++){
        //   var a =this.ruleForm.function[i].join(',')
        //   this.ruleForm.functions.push(a)
        // }
        //    }
      
          this.btnLoading = true
          if (this.ruleForm.id) {
            this.$api.editTenant(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            this.$api.addTenant(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          return false;
        }
      });
    },
     handleReset(row) {
				this.$confirm("确认将用户密码重置吗？", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
				}).then(() => {
					this.loading = true;
					this.$api
						.ResetPassword({ userId: row.personnelId })
						.then((response) => {
							this.$message.success("重置成功");
							this.loading = false;
							this.getList();
						})
						.catch((error) => {
							this.loading = false;
						});
				});
			},
    handleDelete(row) {
      this.$confirm('该操作将删除该租户下所有数据，无法撤回，请慎重考虑！是否删除？', '注意！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.loading = true
        this.$api.deleteTenant({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
  .el-input-number .el-input__inner{
    text-align: left;
    padding-left: 0;
  }
</style>
<style lang="scss" scoped>

.admin-client {

}
.marginB {
  margin-bottom: 0;
}
</style>