<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="租户名称" prop="companyName">
          <el-input
            v-model="queryParams.companyName"
            placeholder="请输入租户名称"
            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-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" border size="small" style="width:100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="手机号" align="center" prop="phone" min-width="130" />
      <el-table-column label="所属租户" align="center" prop="companyName" min-width="140" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="160" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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="dialogTitle" :visible.sync="dialogOpen" width="500px" append-to-body>
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-width="100px" size="small">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone" placeholder="请输入手机号" maxlength="11" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="editForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editForm.remark" type="textarea" :rows="3" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVoiceRoboticCallBlacklist,
  getVoiceRoboticCallBlacklist,
  addVoiceRoboticCallBlacklist,
  updateVoiceRoboticCallBlacklist,
  delVoiceRoboticCallBlacklist,
  changeVoiceRoboticCallBlacklistStatus
} from '@/api/company/companyVoiceRoboticCallBlacklist'

export default {
  name: 'AdminVoiceBlacklist',
  data() {
    return {
      loading: true,
      exportLoading: false,
      showSearch: true,
      total: 0,
      dataList: [],
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        companyName: null
      },
      // 弹窗
      dialogOpen: false,
      dialogTitle: '',
      editForm: {
        blacklistId: null,
        phone: null,
        status: 1,
        remark: null
      },
      editRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listVoiceRoboticCallBlacklist(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.blacklistId)
      this.multiple = !selection.length
    },
    handleAdd() {
      this.resetEditForm()
      this.dialogTitle = '新增黑名单'
      this.dialogOpen = true
    },
    handleUpdate(row) {
      this.resetEditForm()
      const id = row.blacklistId
      getVoiceRoboticCallBlacklist(id).then(response => {
        this.editForm = response.data
        this.dialogTitle = '编辑黑名单'
        this.dialogOpen = true
      })
    },
    submitForm() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          if (this.editForm.blacklistId) {
            updateVoiceRoboticCallBlacklist(this.editForm).then(response => {
              this.$message.success('修改成功')
              this.dialogOpen = false
              this.getList()
            })
          } else {
            addVoiceRoboticCallBlacklist(this.editForm).then(response => {
              this.$message.success('新增成功')
              this.dialogOpen = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const blacklistIds = row.blacklistId ? [row.blacklistId] : this.ids
      this.$confirm('是否确认删除选中的黑名单记录?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delVoiceRoboticCallBlacklist(blacklistIds)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    handleStatusChange(row) {
      changeVoiceRoboticCallBlacklistStatus({
        blacklistId: row.blacklistId,
        status: row.status
      }).then(() => {
        this.$message.success('状态修改成功')
      }).catch(() => {
        row.status = row.status === 1 ? 0 : 1
      })
    },
    handleExport() {
      this.exportLoading = true
      this.download('/admin/voice-blacklist/export', { ...this.queryParams }).then(() => {
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    },
    resetEditForm() {
      this.editForm = {
        blacklistId: null,
        phone: null,
        status: 1,
        remark: null
      }
      this.$nextTick(() => {
        if (this.$refs['editForm']) {
          this.$refs['editForm'].clearValidate()
        }
      })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
