<script>
import handle from "@/until/handle";
import querystring from "querystring";

export default {
  name: "sipBind",
  props: {
    sipDomain: {
      default: ""
    },
    sipId: {
      default: "",
    }
  },
  watch: {
    sipId: {
      handler(newName, oldName) {
        this.init()
      },
      immediate: true,
    }
  },
  data() {
    return {
      loading: false,
      sipAdminsLoaded: false,
      loadedAll: false,
      allAdmins: [],
      sipAdmins: [],
      adminId: ''
    }
  },
  methods: {
    closePopHandle() {
      this.$emit(`exitPop`)
    },
    init() {
      this.loadAllAdmin()
      this.loadSipBindAdmin()
    },
    async loadAllAdmin() {
      this.loading = true;
      let url = "/api/user/all"
      let [err, res] = await handle(this.$axios.get(url))
      this.loading = false;
      if (err || res.data.code != 0) {
        let errStr = err ? err.message : res.data.msg
        console.log(`[获取管理员信息] ${errStr}`)
        this.$message.error(`获取管理员信息失败 ${errStr}`);
        return;
      }
      this.computeAdmins(res.data.data)
    },
    async loadSipBindAdmin() {
      this.loading = true;
      this.sipAdminsLoaded = false;
      let url = "/api/sip/admins"
      url += `?sipId=${this.sipId}`
      let [err, res] = await handle(this.$axios.get(url))
      this.loading = false;
      if (err || res.data.code != 0) {
        let errStr = err ? err.message : res.data.msg
        console.log(`[获取管理员信息] ${errStr}`)
        this.$message.error(`获取管理员信息失败 ${errStr}`);
        return;
      }
      this.sipAdmins = res.data.data;
      this.sipAdminsLoaded = true;
      this.computeAdmins()
    },
    async executeBindSip() {
      if (!this.loadedAll) {
        console.log(`基础数据未加载`);
        return this.$message.error(`基础数据未加载`);
      }
      if (!this.checkAdminId(this.adminId)) {
        return this.$message.error(`管理员id异常,无法被添加`);
      }
      this.loading = true;
      let url = "/api/sip/bind"
      let param = {
        sipId: this.sipId,
        adminId: this.adminId,
      }
      let [err, res] = await handle(
          this.$axios.post(url, querystring.stringify(param)))
      this.loading = false;
      if (err || res.data.code != 0) {
        let errStr = err ? err.message : res.data.msg
        console.log(`[绑定管理员信息] ${errStr}`)
        this.$message.error(`绑定管理员信息失败 ${errStr}`);
        return;
      }
      console.log('绑定管理员信息成功')
      this.$message.success(`绑定管理员信息成功`);
      this.closePopHandle()
    },
    async executeUnbindSip(adminId) {
      if (!this.loadedAll) {
        console.log(`基础数据未加载`);
        return this.$message.error(`基础数据未加载`);
      }
      if (!this.checkSipAdminId(adminId)) {
        return this.$message.error(`管理员id异常, 被解绑`);
      }
      this.loading = true;
      let url = "/api/sip/unbind"
      let param = {
        sipId: this.sipId,
        adminId: this.adminId,
      }
      let [err, res] = await handle(
          this.$axios.get(url, querystring.stringify(param)))
      this.loading = false;
      if (err || res.data.code != 0) {
        let errStr = err ? err.message : res.data.msg
        console.log(`[解绑配置] ${errStr}`)
        this.$message.error(`解绑配置失败 ${errStr}`);
        return;
      }
      console.log('解绑配置成功')
      this.$message.success(`解绑配置成功`);
      this.closePopHandle()
    },

    computeAdmins(adminArr) {
      console.log(`computeAdmins`)
      console.log(this.sipAdminsLoaded)
      console.log(adminArr)
      if (!this.sipAdminsLoaded || !adminArr) {
        return console.log(`数据暂时未加载完成`)
      }
      let allAdmins = []
      adminArr.forEach(admin => {
        if (!this.sipAdmins.find(val => val.id == admin.id)) {
          allAdmins.push(admin)
        }
      })
      console.log(allAdmins);
      this.allAdmins = allAdmins;
      this.loadedAll = true;
    },
    checkAdminId(adminId) {
      if (this.allAdmins.find(val => val.id == adminId)) {
        return true
      }
      return false
    },
    checkSipAdminId(adminId) {
      if (this.sipAdmins.find(val => val.id == adminId)) {
        return true
      }
      return false
    },

    handleUnbind(item) {
      this.executeUnbindSip(item.id)
    },
    handleAdminChange() {

    },
    handleAddAdmin() {
      this.executeBindSip()
    }
  }
}
</script>

<template>
  <div class="sips">
    <div class="page-header">
      <div class="page-title">sip分配</div>
      <div class="page-header-btn">
        <el-select v-model="adminId"
                   placeholder="管理员"
                   @change="handleAdminChange"
                   size="mini"
        >
          <el-option
              v-for="item in allAdmins"
              :key="item.id"
              :label="item.username"
              :value="item.id">
          </el-option>
        </el-select>
        <el-button
            class="ml-2" icon="el-icon-plus" size="mini" style="margin-right: 1rem;" type="primary"
            @click="handleAddAdmin">绑定用户
        </el-button>
      </div>
    </div>
    <el-table :data="sipAdmins" style="width: 100%;font-size: 12px;" header-row-class-name="table-header">
      <el-table-column prop="username" label="名称" min-width="160">
      </el-table-column>
      <el-table-column label="操作" min-width="450" fixed="right">
        <template slot-scope="scope">
          <el-button size="medium" icon="el-icon-edit" type="text" @click="handleUnbind(scope.row)">解绑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<style scoped>

</style>
