<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分机号" prop="extension">
        <el-input
          v-model="queryParams.extension"
          placeholder="请输入分机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SIP注册密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入SIP注册密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主叫显示名称" prop="callerName">
        <el-input
          v-model="queryParams.callerName"
          placeholder="请输入主叫显示名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主叫号码" prop="callerNumber">
        <el-input
          v-model="queryParams.callerNumber"
          placeholder="请输入主叫号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属域名或IP" prop="domain">
        <el-input
          v-model="queryParams.domain"
          placeholder="请输入所属域名或IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拨号上下文" prop="context">
        <el-input
          v-model="queryParams.context"
          placeholder="请输入拨号上下文"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许注册" prop="enableRegister">
        <el-input
          v-model="queryParams.enableRegister"
          placeholder="请输入是否允许注册"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许外呼" prop="enableOutbound">
        <el-input
          v-model="queryParams.enableOutbound"
          placeholder="请输入是否允许外呼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指定出局网关名，如supplier_a" prop="gatewayName">
        <el-input
          v-model="queryParams.gatewayName"
          placeholder="请输入指定出局网关名，如supplier_a"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cti:extension:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cti:extension:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cti:extension:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cti:extension:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="extensionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="分机号" align="center" prop="extension" />
      <el-table-column label="SIP注册密码" align="center" prop="password" />
      <el-table-column label="主叫显示名称" align="center" prop="callerName" />
      <el-table-column label="主叫号码" align="center" prop="callerNumber" />
      <el-table-column label="所属域名或IP" align="center" prop="domain" />
      <el-table-column label="拨号上下文" align="center" prop="context" />
      <el-table-column label="是否允许注册" align="center" prop="enableRegister" />
      <el-table-column label="是否允许外呼" align="center" prop="enableOutbound" />
      <el-table-column label="指定出局网关名，如supplier_a" align="center" prop="gatewayName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cti:extension:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cti:extension:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分机管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分机号" prop="extension">
          <el-input v-model="form.extension" placeholder="请输入分机号" />
        </el-form-item>
        <el-form-item label="SIP注册密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入SIP注册密码" />
        </el-form-item>
        <el-form-item label="主叫显示名称" prop="callerName">
          <el-input v-model="form.callerName" placeholder="请输入主叫显示名称" />
        </el-form-item>
        <el-form-item label="主叫号码" prop="callerNumber">
          <el-input v-model="form.callerNumber" placeholder="请输入主叫号码" />
        </el-form-item>
        <el-form-item label="所属域名或IP" prop="domain">
          <el-input v-model="form.domain" placeholder="请输入所属域名或IP" />
        </el-form-item>
        <el-form-item label="拨号上下文" prop="context">
          <el-input v-model="form.context" placeholder="请输入拨号上下文" />
        </el-form-item>
        <el-form-item label="是否允许注册" prop="enableRegister">
          <el-input v-model="form.enableRegister" placeholder="请输入是否允许注册" />
        </el-form-item>
        <el-form-item label="是否允许外呼" prop="enableOutbound">
          <el-input v-model="form.enableOutbound" placeholder="请输入是否允许外呼" />
        </el-form-item>
        <el-form-item label="指定出局网关名，如supplier_a" prop="gatewayName">
          <el-input v-model="form.gatewayName" placeholder="请输入指定出局网关名，如supplier_a" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExtension, getExtension, delExtension, addExtension, updateExtension } from "@/api/cti/extension"

export default {
  name: "Extension",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分机管理表格数据
      extensionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        extension: null,
        password: null,
        callerName: null,
        callerNumber: null,
        domain: null,
        context: null,
        enableRegister: null,
        enableOutbound: null,
        gatewayName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        extension: [
          { required: true, message: "分机号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "SIP注册密码不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询分机管理列表 */
    getList() {
      this.loading = true
      listExtension(this.queryParams).then(response => {
        this.extensionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        extension: null,
        password: null,
        callerName: null,
        callerNumber: null,
        domain: null,
        context: null,
        enableRegister: null,
        enableOutbound: null,
        gatewayName: null,
        createTime: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加分机管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExtension(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改分机管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExtension(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExtension(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除分机管理编号为"' + ids + '"的数据项？').then(function() {
        return delExtension(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cti/extension/export', {
        ...this.queryParams
      }, `extension_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
