<template>
  <div class="app-container">
    <div class="voice-layout">
      <!-- 左侧分组列表 -->
      <div class="voice-left-panel">
        <div class="group-title">
          <span>语音分组</span>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            circle
            @click="handleAddGroup"
          ></el-button>
        </div>
        <div class="group-list">
          <div
            class="group-item"
            :class="{ active: currentGroupId === null }"
            @click="selectGroup(null)"
          >全部</div>
          <div
            v-for="item in groupList"
            :key="item.id"
            class="group-item"
            :class="{ active: currentGroupId === item.id }"
            @click="selectGroup(item.id)"
          >
            <span class="group-item-name">{{ item.voiceGroupName }}</span>
            <i class="el-icon-edit group-item-edit" @click.stop="handleEditGroup(item)"></i>
          </div>
        </div>
      </div>
      <!-- 右侧内容区 -->
      <div class="voice-right-panel">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
          <el-form-item label="语音文本" prop="voiceTxt">
            <el-input
              v-model="queryParams.voiceTxt"
              placeholder="请输入语音文本"
              clearable
              size="small"
              @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="['qw:QwSopTempVoice:add']"
            >新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table border v-loading="loading" :data="QwSopTempVoiceList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="语音文本" align="center" prop="voiceTxt" />
          <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-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['qw:QwSopTempVoice: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"
        />
      </div>
    </div>

    <!-- 添加或修改模板对应的销售语音文件对话框 -->
    <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="groupId">
          <el-select
            v-model="form.groupId"
            placeholder="请选择语音分组"
            clearable
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.voiceGroupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="语音文本" prop="voiceTxt">
          <el-input type="textarea" :rows="6" v-model="form.voiceTxt" 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 { listQwSopTempVoice, getQwSopTempVoice, delQwSopTempVoice, addQwSopTempVoice, updateQwSopTempVoice, exportQwSopTempVoice } from "@/api/qw/QwSopTempVoice";
import { removeSidebar } from "@/api/qw/QwSopTempVoice";
import { listQwSopTempVoiceGroup, addQwSopTempVoiceGroup, updateQwSopTempVoiceGroup } from "@/api/qw/QwSopTempVoiceGroup";

export default {
  name: "QwSopTempVoice",
  data() {
    return {
      // 分组列表
      groupList: [],
      // 当前选中的分组ID，null表示全部
      currentGroupId: null,

      // 是否录制完成字典
      recordTypeOptions: [
        { dictLabel: '是', dictValue: '1' },
        { dictLabel: '否', dictValue: '2' }
      ],

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模板对应的销售语音文件表格数据
      QwSopTempVoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyUserId: null,
        companyId: null,
        qwUserId: null,
        tempId: null,
        ruleId: null,
        dayId: null,
        contentId: null,
        groupId: null,
        voiceTxt: null,
        voiceUrl: null,
        userVoiceUrl: null,
        recordType: null,
        duration: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupId: [
          { required: true, message: "请选择语音分组", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getGroupList();
    this.getList();
  },
  methods: {
    /** 获取语音分组列表 */
    getGroupList() {
      listQwSopTempVoiceGroup({ pageNum: 1, pageSize: 999 }).then(response => {
        this.groupList = response.rows;
      });
    },
    /** 选择分组 */
    selectGroup(groupId) {
      this.currentGroupId = groupId;
      this.queryParams.groupId = groupId;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增分组 */
    handleAddGroup() {
      this.$prompt('请输入语音分组名称', '新增分组', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '分组名称不能为空'
      }).then(({ value }) => {
        addQwSopTempVoiceGroup({ voiceGroupName: value }).then(() => {
          this.msgSuccess('新增成功');
          this.getGroupList();
        });
      }).catch(() => {});
    },
    /** 修改分组名称 */
    handleEditGroup(item) {
      if (!item) return;
      this.$prompt('请输入新的分组名称', '修改分组', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '分组名称不能为空',
        inputValue: item.voiceGroupName
      }).then(({ value }) => {
        updateQwSopTempVoiceGroup({ id: item.id, voiceGroupName: value }).then(() => {
          this.msgSuccess('修改成功');
          this.getGroupList();
        });
      }).catch(() => {});
    },
    /** 查询模板对应的销售语音文件列表 */
    getList() {
      this.loading = true;
      listQwSopTempVoice(this.queryParams).then(response => {
        this.QwSopTempVoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyUserId: null,
        companyId: null,
        qwUserId: null,
        tempId: null,
        ruleId: null,
        dayId: null,
        contentId: null,
        groupId: null,
        voiceTxt: null,
        voiceUrl: null,
        userVoiceUrl: null,
        recordType: null,
        duration: null,
        createTime: null,
        updateTime: null,
        status: 0
      };
      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
      getQwSopTempVoice(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) {
            updateQwSopTempVoice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQwSopTempVoice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除模板对应的销售语音文本---"' + row.voiceTxt + '"---的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return removeSidebar(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有模板对应的销售语音文件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportQwSopTempVoice(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.app-container {
  position: relative;
  height: calc(100vh - 150px);
  overflow: hidden;
  box-sizing: border-box;
}
.voice-layout {
  position: absolute;
  top: 20px;
  left: 20px;
  right: 20px;
  bottom: 20px;
  display: flex;
}
.voice-left-panel {
  width: 200px;
  min-width: 200px;
  background: #fff;
  border-right: 1px solid #e8e8e8;
  display: flex;
  flex-direction: column;
}
.group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 16px;
  font-weight: bold;
  padding: 16px 16px 12px;
  border-bottom: 1px solid #e8e8e8;
  flex-shrink: 0;
}
.group-list {
  padding: 8px 8px 0;
  overflow-y: auto;
  flex: 1;
}
.group-item {
  padding: 10px 12px;
  cursor: pointer;
  border-radius: 4px;
  font-size: 14px;
  color: #333;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.group-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.group-item-edit {
  font-size: 13px;
  color: #999;
  flex-shrink: 0;
  margin-left: 6px;
}
.group-item-edit:hover {
  color: #1890ff;
}
.group-item:hover {
  background: #f0f2f5;
}
.group-item.active {
  background: #e6f7ff;
  color: #1890ff;
  font-weight: bold;
}
.voice-right-panel {
  flex: 1;
  padding-left: 16px;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
