<template>
    <div class="app-container">
        <!-- 查询区域 -->
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="80px"
            @submit.native.prevent
        >
            <el-form-item label="对象类型" prop="targetType">
                <el-select
                    v-model="queryParams.targetType"
                    placeholder="请选择对象类型"
                    clearable
                    size="small"
                    style="width: 160px"
                >
                    <el-option label="手机号码" :value="1" />
                    <el-option label="客户ID" :value="2" />
                    <el-option label="企微客户ID" :value="3" />
                </el-select>
            </el-form-item>

            <el-form-item label="对象值" prop="targetValue">
                <el-input
                    v-model="queryParams.targetValue"
                    placeholder="请输入手机号/客户ID/企微客户ID"
                    clearable
                    size="small"
                    style="width: 240px"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="业务类型" prop="businessType">
                <el-input
                    v-model="queryParams.businessType"
                    placeholder="请输入业务类型"
                    clearable
                    size="small"
                    style="width: 180px"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请选择状态"
                    clearable
                    size="small"
                    style="width: 140px"
                >
                    <el-option label="生效" :value="1" />
                    <el-option label="失效" :value="0" />
                </el-select>
            </el-form-item>

            <el-form-item label="来源" prop="source">
                <el-select
                    v-model="queryParams.source"
                    placeholder="请选择来源"
                    clearable
                    size="small"
                    style="width: 140px"
                >
                    <el-option label="手动添加" :value="1" />
                    <el-option label="外部系统同步" :value="2" />
                </el-select>
            </el-form-item>

<!--            <el-form-item label="公司ID" prop="companyId">-->
<!--                <el-input-->
<!--                    v-model="queryParams.companyId"-->
<!--                    placeholder="请输入公司ID"-->
<!--                    clearable-->
<!--                    size="small"-->
<!--                    style="width: 160px"-->
<!--                    @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-button
                size="mini"
                type="primary"
                icon="el-icon-plus"
                @click="handleAdd"
                v-hasPermi="['company:voiceRoboticCallBlacklist:add']"
            >
                新增
            </el-button>

            <el-button
                size="mini"
                type="success"
                icon="el-icon-edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
            >
                编辑
            </el-button>

            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                :disabled="multiple"
                @click="handleDeleteBatch"
                v-hasPermi="['company:voiceRoboticCallBlacklist:remove']"
            >
                批量删除
            </el-button>

            <el-button
                size="mini"
                type="primary"
                plain
                :disabled="multiple"
                @click="handleBatchEnable"
                v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
            >
                批量启用
            </el-button>

            <el-button
                size="mini"
                type="warning"
                plain
                :disabled="multiple"
                @click="handleBatchDisable"
                v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
            >
                批量失效
            </el-button>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
        </el-row>

        <!-- 表格区域 -->
        <el-table
            v-loading="loading"
            :data="blacklistList"
            border
            height="500"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="ID" align="center" prop="id" width="80" />

<!--            <el-table-column label="公司ID" align="center" prop="companyId" width="100" />-->

            <el-table-column label="业务类型" align="center" prop="businessType" min-width="120" />

            <el-table-column label="对象类型" align="center" prop="targetType" width="120">
                <template slot-scope="scope">
                    <span v-if="scope.row.targetType === 1">手机号</span>
                    <span v-else-if="scope.row.targetType === 2">客户ID</span>
                    <span v-else-if="scope.row.targetType === 3">企微客户ID</span>
                    <span v-else>-</span>
                </template>
            </el-table-column>

            <el-table-column label="对象值" align="center" prop="targetValue" min-width="180" show-overflow-tooltip />

            <el-table-column label="拉黑原因" align="center" prop="reason" min-width="180" show-overflow-tooltip />

            <el-table-column label="来源" align="center" prop="source" width="100">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.source === 1" size="mini">手动添加</el-tag>
                    <el-tag v-else-if="scope.row.source === 2" type="info" size="mini">系统同步</el-tag>
                    <span v-else>-</span>
                </template>
            </el-table-column>

            <el-table-column label="状态" align="center" prop="status" width="100">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status === 1" type="danger" size="mini">生效</el-tag>
                    <el-tag v-else type="success" size="mini">失效</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="备注" align="center" prop="remark" min-width="180" show-overflow-tooltip />

            <el-table-column label="创建人" align="center" prop="createBy" width="120" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
            <el-table-column label="更新人" align="center" prop="updateBy" width="120" />
            <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
                    >
                        修改
                    </el-button>

                    <el-button
                        v-if="scope.row.status === 0"
                        size="mini"
                        type="text"
                        @click="handleChangeStatus(scope.row, 1)"
                        v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
                    >
                        启用
                    </el-button>

                    <el-button
                        v-if="scope.row.status === 1"
                        size="mini"
                        type="text"
                        @click="handleChangeStatus(scope.row, 0)"
                        v-hasPermi="['company:voiceRoboticCallBlacklist:edit']"
                    >
                        失效
                    </el-button>

                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['company:voiceRoboticCallBlacklist: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="680px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="95px">
                <el-row>
<!--                    <el-col :span="12">-->
<!--                        <el-form-item label="公司ID" prop="companyId">-->
<!--                            <el-input v-model="form.companyId" placeholder="请输入公司ID" />-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->

                    <el-col :span="12">
                        <el-form-item label="业务类型" prop="businessType">
                            <el-input v-model="form.businessType" placeholder="请输入业务类型" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="对象类型" prop="targetType">
                            <el-select v-model="form.targetType" placeholder="请选择对象类型" style="width: 100%">
                                <el-option label="手机号码" :value="1" />
                                <el-option label="客户ID" :value="2" />
                                <el-option label="企微客户ID" :value="3" />
                            </el-select>
                        </el-form-item>
                    </el-col>

<!--                    <el-col :span="12">-->
<!--                        <el-form-item label="来源" prop="source">-->
<!--                            <el-select v-model="form.source" placeholder="请选择来源" style="width: 100%">-->
<!--                                <el-option label="手工添加" :value="1" />-->
<!--                                <el-option label="系统" :value="2" />-->
<!--                            </el-select>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                </el-row>

                <el-form-item label="对象值" prop="targetValue">
                    <el-input
                        v-model="form.targetValue"
                        placeholder="请输入手机号 / 客户ID / 企微客户ID"
                        maxlength="128"
                        show-word-limit
                    />
                </el-form-item>

                <el-form-item label="拉黑原因" prop="reason">
                    <el-input
                        v-model="form.reason"
                        type="textarea"
                        :rows="3"
                        placeholder="请输入拉黑原因"
                        maxlength="255"
                        show-word-limit
                    />
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        :rows="3"
                        placeholder="请输入备注"
                        maxlength="500"
                        show-word-limit
                    />
                </el-form-item>

                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">生效</el-radio>
                        <el-radio :label="0">失效</el-radio>
                    </el-radio-group>
                </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 {
    listVoiceRoboticCallBlacklist,
    getVoiceRoboticCallBlacklist,
    addVoiceRoboticCallBlacklist,
    updateVoiceRoboticCallBlacklist,
    delVoiceRoboticCallBlacklist,
    changeVoiceRoboticCallBlacklistStatus
} from "@/api/company/companyVoiceRoboticCallBlacklist";

export default {
    name: "VoiceRoboticCallBlacklist",
    data() {
        return {
            loading: true,
            ids: [],
            selectedRows: [],
            single: true,
            multiple: true,
            showSearch: true,
            total: 0,
            blacklistList: [],
            title: "",
            open: false,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                companyId: null,
                businessType: null,
                targetType: null,
                targetValue: null,
                status: null,
                source: null
            },
            form: {},
            rules: {
                companyId: [
                    { required: true, message: "公司ID不能为空", trigger: "blur" }
                ],
                targetType: [
                    { required: true, message: "对象类型不能为空", trigger: "change" }
                ],
                targetValue: [
                    { required: true, message: "对象值不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询列表 */
        getList() {
            this.loading = true;
            listVoiceRoboticCallBlacklist(this.queryParams).then(response => {
                this.blacklistList = response.rows || [];
                this.total = response.total || 0;
                this.loading = false;
            }).catch(() => {
                this.loading = false;
            });
        },

        /** 表单重置 */
        reset() {
            this.form = {
                id: null,
                companyId: null,
                businessType: null,
                targetType: 1,
                targetValue: null,
                reason: null,
                source: 1,
                status: 1,
                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.selectedRows = selection;
            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[0];
            getVoiceRoboticCallBlacklist(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改外呼黑名单";
            });
        },

        /** 提交 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (!valid) {
                    return;
                }

                if (this.form.id != null) {
                    updateVoiceRoboticCallBlacklist(this.form).then(response => {
                        if (response.code === 200) {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        } else {
                            this.msgError(response.msg);
                        }
                    });
                } else {
                    addVoiceRoboticCallBlacklist(this.form).then(response => {
                        if (response.code === 200) {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        } else {
                            this.msgError(response.msg);
                        }
                    });
                }
            });
        },

        /** 取消 */
        cancel() {
            this.open = false;
            this.reset();
        },

        /** 单个删除 */
        handleDelete(row) {
            const id = row.id;
            this.$confirm('是否确认删除黑名单编号为 "' + id + '" 的数据项？', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                return delVoiceRoboticCallBlacklist(id);
            }).then(() => {
                this.msgSuccess("删除成功");
                this.getList();
            }).catch(() => {});
        },

        /** 批量删除 */
        handleDeleteBatch() {
            this.$confirm("是否确认删除选中的黑名单数据项？", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(async () => {
                for (let i = 0; i < this.ids.length; i++) {
                    await delVoiceRoboticCallBlacklist(this.ids[i]);
                }
                this.msgSuccess("批量删除成功");
                this.getList();
            }).catch(() => {});
        },

        /** 修改状态 */
        handleChangeStatus(row, status) {
            const text = status === 1 ? "启用" : "失效";
            this.$confirm("确认要" + text + "该黑名单吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                return changeVoiceRoboticCallBlacklistStatus({
                    id: row.id,
                    status: status
                });
            }).then(response => {
                if (response.code === 200) {
                    this.msgSuccess(text + "成功");
                    this.getList();
                } else {
                    this.msgError(response.msg);
                }
            }).catch(() => {});
        },

        /** 批量启用 */
        handleBatchEnable() {
            this.batchChangeStatus(1);
        },

        /** 批量失效 */
        handleBatchDisable() {
            this.batchChangeStatus(0);
        },

        /** 批量改状态 */
        batchChangeStatus(status) {
            const text = status === 1 ? "启用" : "失效";
            this.$confirm("确认要批量" + text + "选中的数据吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(async () => {
                for (let i = 0; i < this.selectedRows.length; i++) {
                    await changeVoiceRoboticCallBlacklistStatus({
                        id: this.selectedRows[i].id,
                        status: status
                    });
                }
                this.msgSuccess("批量" + text + "成功");
                this.getList();
            }).catch(() => {});
        }
    }
};
</script>

<style scoped>
.app-container {
    height: 87.5vh;
}
</style>
