<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="用户信息" prop="params.userKeyword">
              <el-input v-model="queryParams.params.userKeyword" placeholder="请输入用户名或手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备信息" prop="params.deviceKeyword">
              <el-input v-model="queryParams.params.deviceKeyword" placeholder="请输入设备名称或设备编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备类型" prop="params.deviceType">
              <el-select v-model="queryParams.params.deviceType" placeholder="请选择设备类型" clearable >
                <el-option v-for="dict in aqu_device_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
              <el-form-item label="告警类型" prop="params.warnType">
              <el-select v-model="queryParams.params.warnType" placeholder="请选择告警类型" clearable >
                <el-option v-for="dict in warn_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="呼叫电话" prop="mobilePhone">
              <el-input v-model="queryParams.mobilePhone" placeholder="请输入呼叫电话" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="呼叫状态" prop="callStatus">
              <el-select v-model="queryParams.callStatus" placeholder="请选择呼叫状态" clearable >
                <el-option v-for="dict in call_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['fishery:callNotice:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['fishery:callNotice:edit']">修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['fishery:callNotice:remove']">删除</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['fishery:callNotice:export']">导出</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="callNoticeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户名" align="center" prop="userName" width="120"/>
        <!-- <el-table-column label="手机号" align="center" prop="mobilePhone"  width="120"/> -->
        <el-table-column label="设备编号" align="center" prop="serialNum"  width="150"/>
         <!-- <el-table-column label="设备名称" align="center" prop="deviceName" width="120"/> -->
          <el-table-column label="设备类型" align="center" prop="deviceType" width="120">
          <template #default="scope">
            <dict-tag :options="aqu_device_type" :value="scope.row.deviceType"/>
          </template>
        </el-table-column>
        <el-table-column label="呼叫电话" align="center" prop="mobilePhone" width="120"/>
         <el-table-column label="呼叫时间" align="center" prop="callTime"  width="180"/>
          <el-table-column label="告警类型" align="center" prop="warnType" width="150">
          <template #default="scope">
            <dict-tag :options="warn_type" :value="scope.row.warnType"/>
          </template>
        </el-table-column>
         <el-table-column label="告警标题" align="center" prop="warnTitle" width="100"/>
        <el-table-column label="呼叫状态" align="center" prop="callStatus">
          <template #default="scope">
            <dict-tag :options="call_status" :value="scope.row.callStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="主叫号码" align="center" prop="caller" />
         <!-- <el-table-column label="呼叫发起时间" align="center" prop="originateTime" /> -->
          <!-- <el-table-column label="通话接通时间" align="center" prop="startTime" />
           <el-table-column label="通话结束时间" align="center" prop="endTime" /> -->
        <!-- <el-table-column label="通话时长" align="center" prop="duration" width="90">
          <template #default="scope">
            {{ scope.row.duration ? scope.row.duration + '秒' : '' }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="呼叫结果状态码" align="center" prop="statusCode" /> -->
        <el-table-column label="结果描述" align="center" prop="statusMsg" />
        <!-- <el-table-column label="通话类型" align="center" prop="tollType">
          <template #default="scope">
            <dict-tag :options="toll_type" :value="scope.row.tollType"/>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center"   width="80"  class-name="small-padding fixed-width">
          <template #default="scope">
            <!-- <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fishery:callNotice:edit']"></el-button>
            </el-tooltip> -->
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fishery:callNotice:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改告警电话通知记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="callNoticeFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="呼叫电话" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入呼叫电话" />
        </el-form-item>
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="设定的呼叫时间" prop="callTime">
          <el-date-picker clearable
            v-model="form.callTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择设定的呼叫时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="呼叫的CallId" prop="callId">
          <el-input v-model="form.callId" placeholder="请输入呼叫的CallId" />
        </el-form-item>
        <el-form-item label="呼叫状态" prop="callStatus">
          <el-select v-model="form.callStatus" placeholder="请选择呼叫状态">
            <el-option
                v-for="dict in call_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主叫号码" prop="caller">
          <el-input v-model="form.caller" placeholder="请输入主叫号码" />
        </el-form-item>
        <el-form-item label="通话时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入通话时长" />
        </el-form-item>
        <el-form-item label="通话结束时间" prop="endTime">
          <el-input v-model="form.endTime" placeholder="请输入通话结束时间" />
        </el-form-item>
        <el-form-item label="挂断方向" prop="hangupDirection">
          <el-input v-model="form.hangupDirection" placeholder="请输入挂断方向" />
        </el-form-item>
        <el-form-item label="呼叫发起时间" prop="originateTime">
          <el-input v-model="form.originateTime" placeholder="请输入呼叫发起时间" />
        </el-form-item>
        <el-form-item label="扩展字段回传" prop="outId">
          <el-input v-model="form.outId" placeholder="请输入扩展字段回传" />
        </el-form-item>
        <el-form-item label="被叫响铃时间" prop="ringTime">
          <el-input v-model="form.ringTime" placeholder="请输入被叫响铃时间" />
        </el-form-item>
        <el-form-item label="通话接通时间" prop="startTime">
          <el-input v-model="form.startTime" placeholder="请输入通话接通时间" />
        </el-form-item>
        <el-form-item label="呼叫结果状态码" prop="statusCode">
          <el-input v-model="form.statusCode" placeholder="请输入呼叫结果状态码" />
        </el-form-item>
        <el-form-item label="结果描述" prop="statusMsg">
          <el-input v-model="form.statusMsg" placeholder="请输入结果描述" />
        </el-form-item>
        <el-form-item label="通话类型" prop="tollType">
          <el-select v-model="form.tollType" placeholder="请选择通话类型">
            <el-option
                v-for="dict in toll_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="话单类型" prop="voiceType">
          <el-select v-model="form.voiceType" placeholder="请选择话单类型">
            <el-option
                v-for="dict in voice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="塘口名称" prop="pondName">
          <el-input v-model="form.pondName" placeholder="请输入塘口名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CallNotice" lang="ts">
import { listCallNotice, getCallNotice, delCallNotice, addCallNotice, updateCallNotice } from '@/api/fishery/callNotice';
import { CallNoticeVO, CallNoticeQuery, CallNoticeForm } from '@/api/fishery/callNotice/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { toll_type, call_status, voice_type,aqu_device_type,warn_type } = toRefs<any>(proxy?.useDict('toll_type', 'call_status', 'voice_type','aqu_device_type','warn_type'));

const callNoticeList = ref<CallNoticeVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const callNoticeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: CallNoticeForm = {
  id: undefined,
  userId: undefined,
  mobilePhone: undefined,
  deviceId: undefined,
  callTime: undefined,
  callId: undefined,
  callStatus: undefined,
  caller: undefined,
  duration: undefined,
  endTime: undefined,
  hangupDirection: undefined,
  originateTime: undefined,
  outId: undefined,
  ringTime: undefined,
  startTime: undefined,
  statusCode: undefined,
  statusMsg: undefined,
  tollType: undefined,
  voiceType: undefined,
  pondName: undefined,
}
const data = reactive<PageData<CallNoticeForm, CallNoticeQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    mobilePhone: undefined,
    deviceId: undefined,
    duration: undefined,
    endTime: undefined,
    hangupDirection: undefined,
    tollType: undefined,
    callStatus: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键id不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
    mobilePhone: [
      { required: true, message: "呼叫电话不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备id不能为空", trigger: "blur" }
    ],
    callTime: [
      { required: true, message: "设定的呼叫时间不能为空", trigger: "blur" }
    ],
    callStatus: [
      { required: true, message: "呼叫状态不能为空", trigger: "change" }
    ],
    pondName: [
      { required: true, message: "塘口名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询告警电话通知记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCallNotice(queryParams.value);
  callNoticeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  callNoticeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: CallNoticeVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加告警电话通知记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: CallNoticeVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getCallNotice(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改告警电话通知记录";
}

/** 提交按钮 */
const submitForm = () => {
  callNoticeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateCallNotice(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addCallNotice(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: CallNoticeVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除选中的数据项？').finally(() => loading.value = false);
  await delCallNotice(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('fishery/callNotice/export', {
    ...queryParams.value
  }, `callNotice_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
