<template>
  <div>
    <el-card class="box-card">
      <!-- <el-select v-model="selectoptionValue" size="small" style="width: 1.5rem;" class="handle-input mr10"
        placeholder="请选择数据中心" @change="selectTenantId" v-if="userInfo.type==0">
        <el-option v-for="item in selectoption" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select> -->
	  <selectoption></selectoption>
      <el-input size="small" type="text" class="w300" placeholder="请输入部门名称" clearable v-model="enterpriseName">
      </el-input>
      <el-button type="primary" size="small" @click="serachFun">搜索</el-button>
      <el-table :data="tableData" style="width: 100%" :height="height" :border="true">
        <el-table-column prop="name" label="企业名称">
        </el-table-column>
        <el-table-column prop="quota" label="允许接入设备数量">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="editConfig(scope.$index,scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="deleteConfig(scope.$index,scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
        :page-sizes="[10,20,100, 200, 300, 400]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="pagetotal">
      </el-pagination>
      <el-dialog :title="title" :visible.sync="enterpriseidedit" width="30%" class="eldialog">
        <el-form ref="form1" :model="Enterprise1" label-width="70px">
          <el-form-item label="名称">
            <el-input v-model="Enterprise1.label" :clearable="true" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="Enterprise1.value" type="text" maxlength="16" :clearable="true"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="enterpriseidedit = false">取 消</el-button>
            <el-button type="primary" @click="confirmEditConfig" v-if="modifyStatus">修 改</el-button>
            <el-button type="primary" @click="confirmEditConfig" v-else>确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
  import {
    httpGet,
    httpPost
  } from "../api/index";
  export default {
    inject: ['reload'],
    name: "quantityConfiguration",
    data() {
      return {
        selectoption: [],
        selectoptionValue: '',
        tableData: [],
        enterpriseidedit: false,
        modifyStatus: false,
        Enterprise1: {
          value: '',
          label: ''
        },
        title: "添加",
        config: {},
        row: {},
        index: '',
        enterpriseName: '',
        pageSize: 10,
        pageNum: 1,
        pagetotal: 0,
        userInfo: {
          type: ''
        }
      };
    },
    components: {},
    created() {
      this.userInfo = JSON.parse(localStorage.getItem('info'))
      this.height = document.documentElement.clientHeight - 200
      this.getConfig()
      // this.selectoption = JSON.parse(localStorage.getItem('selectoption'))
      // this.selectoptionValue = localStorage.getItem('tenantId')
    },
    methods: {
      // selectTenantId(e) {
      //   localStorage.setItem('tenantId', e)
      //   this.reload()
      // },
      serachFun() {
        this.pageNum = 1
        this.tableData = []
        this.getConfig()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.getConfig()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.getConfig()
      },
      addConfig() {
        this.Enterprise1 = {
            value: '',
            label: ''
          },
          this.title = "添加"
        this.enterpriseidedit = true
        this.modifyStatus = false
      },
      getConfig() {
        var str = {
          name: this.enterpriseName
        }
        var data = {
          q: JSON.stringify(str),
          // pageSize: this.pageSize,
          // pageNum: this.pageNum,
          pageSize: 1,
          pageNum: 1
        }
        httpGet(data, this.$api.getEnterprise).then(res => {
          if (res.success) {
            this.tableData = res.data.list
            this.pagetotal = res.data.total
          } else {
            this.$message.error("获取失败")
          }
        })
      },
      editConfig(index, row) {
        this.row = row
        this.Enterprise1.value = row.quota
        this.Enterprise1.label = row.name
        this.index = index
        this.title = "编辑"
        this.enterpriseidedit = true
        this.modifyStatus = true
      },
      deleteConfig(index, row) {
        this.$confirm("确定要删除吗？", "提示", {
            type: "warning",
          })
          .then(() => {
            var tableData = JSON.parse(JSON.stringify(this.tableData))
            tableData.splice(index, 1)
            var data = {
              list: tableData
            }
            httpPost(data, this.$api.editConfig).then(res => {
              if (res.success) {
                this.tableData = res.data
                this.enterpriseidedit = false
                this.getConfig()
              } else {
                this.$message.error("删除失败")
              }
            })
          })
      },
      confirmEditConfig() {
        if (this.Enterprise1.value == '' || this.Enterprise1.label == '') {
          this.$message.error("请填写完整信息")
        } else {
          if(this.userInfo.type==0 && this.row.id==this.userInfo.enterpriseId){
            var data=this.Enterprise1.value
            httpPost(data,this.$api.edutLicense).then(res=>{
              if (res.success) {
                this.pageNum = 1
                this.tableData = []
                this.getConfig()
                this.$message.success("修改成功")
                this.enterpriseidedit = false
              } else {
                this.$message.error("修改失败")
              }
            }).catch(err=>{
              console.log(err)
                this.$message.error(err.message)
            })
          }else{
            httpGet('', `/v1/device/edit/quota/${this.row.id}/${this.Enterprise1.value}`).then(res => {
              if (res.success) {
                this.pageNum = 1
                this.tableData = []
                this.getConfig()
                this.$message.success("修改成功")
                this.enterpriseidedit = false
              } else {
                this.$message.error("修改失败")
              }
            }).catch(err=>{
              console.log(err)
                this.$message.error(err.message)
            })
          }
        }
      },
    },
  };
</script>
<style scoped>
  .w300 {
    width: 300px;
  }
</style>
