<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="getList" 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=>{return 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" >
            <div v-if="$permission('/admin/client/modify')" @click="handleEdit(scope.row)">编辑</div>
            <span v-else :disabled="true">编辑</span>
        <!-- <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 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="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 v-model="ruleForm.number" placeholder="请输入用户账号配额"></el-input>
            </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="function">
           <el-cascader
           v-model="ruleForm.function"
              :options="authority"
              :props="{multiple:true, checkStrictly: true,value:'id',label:'name' }"
              collapse-tags
              placeholder="请输入功能权限"
              clearable
              style="width:100%"></el-cascader>
          <!-- <el-input v-model="ruleForm.function" placeholder="请输入功能权限"></el-input> -->
        </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-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 placeholder="请选择类型" style="width: 100%">
                <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-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>{{ruleForm.apply.function}}</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>
  </div>
</template>

<script>
export default {
  components: {
  },
  data() {
    return {
      trans: [],
      authority: [],
      select:'',
      pswdshow: false,
      showvisible: false,
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      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: 'blur' },],
        	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.translate()
    // console.log(this.select)
  },
  created(){
    // this.select = this.$store.state.admin.dictionaries
  },
    computed: {
    dictionaries() {
      // console.log(this.$store.state.admin.dictionaries)
      return this.$store.state.admin.dictionaries
    },
   
  },
  methods: {
    handleAdd() {
      this.title = '新增租户'
      this.visible = true
      this.pswdshow = true
      this.ruleForm = {
         state:1
      }
    },
    handleEdit(row) {
      this.title = '编辑租户'
      this.visible = true
      this.pswdshow = false
      this.ruleForm = { ...row }
      
    },
    handleDetails(row) {
      // console.log(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
      })
    },
    getFunction() {
      this.$api.getFunctionList().then(response => {
        // console.log(response.data)
        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.functions=[]
        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 {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).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" scoped>
.admin-client {

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