<template>
    <el-drawer v-model="data.show" title="人脸机设备管理" size="960px" class="ubi-device-drawer">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['manage:attendance_ubi_device:add']">新增</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="attendance_ubi_deviceList" @selection-change="handleSelectionChange">
            <el-table-column type="index" width="50" label="序号"/>
            <el-table-column label="公司" align="center" prop="compName" v-if="1==2"/>
            <el-table-column label="所属项目" align="center" prop="projectName" />
            <el-table-column label="设备序列号" align="center" prop="deviceNo">
                <template #default="scope"><span style="color: var(--el-color-warning);">{{scope.row.deviceNo}}</span></template>
            </el-table-column>
            <el-table-column label="通道编号" align="center" prop="channel" />
            <el-table-column label="方向" align="center" prop="direction">
                <template #default="scope">
                    {{ scope.row.direction==0?'进':'出' }}
                </template>
            </el-table-column> 
            <el-table-column label="用途" align="center" prop="tag" />
            <el-table-column label="设备模式" align="center" prop="deviceModel" v-if="false" />
            <el-table-column label="设备状态" align="center" prop="deviceState" v-if="false" >
                <template #default="scope">
                    <span v-if="scope.row.deviceState == 1">未绑定</span>
                    <span v-if="scope.row.deviceState == 2">已绑定</span>
                    <span v-if="scope.row.deviceState == 3">已禁用</span>
                </template>
            </el-table-column>
            <el-table-column label="比对模式" align="center" prop="recType" v-if="false" >
                <template #default="scope">
                    <span v-if="scope.row.recType == 1">本地识别</span>
                    <span v-if="scope.row.recType == 2">云端识别</span>
                </template>
            </el-table-column>
            <el-table-column label="在线状态" align="center" prop="onlineState">
                <template #default="scope">
                    <span v-if="scope.row.onlineState == 1" style="color:green;">
                        <el-icon style="vertical-align: middle;"><SuccessFilled /></el-icon>
                        在线</span>
                    <span v-if="scope.row.onlineState == 2" style="color: red;">
                        <el-icon style="vertical-align: middle;"><CircleCloseFilled /></el-icon>离线</span>
                </template>
            </el-table-column>
            <el-table-column label="版本号" align="center" prop="versionNo" v-if="false" />
            <el-table-column label="最后激活时间" align="center" prop="lastActiveTime" width="180" v-if="false" >
                <template #default="scope">
                    <span>{{ parseTime(scope.row.lastActiveTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="注册状态" align="center" prop="hasRegister" v-if="false" >
                <template #default="scope">
                    <span v-if="scope.row.onlineState == 1">已注册</span>
                    <span v-else>未注册</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
                <template #default="scope">
                    <el-button link type="primary" icon="ChromeFilled" @click="handleAuth(scope.row)" v-if="1==2"
                        v-hasPermi="['manage:attendance_ubi_device:edit']">同步</el-button>
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['manage:attendance_ubi_device:edit']">修改</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['manage:attendance_ubi_device:remove']">删除</el-button>
                </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-dialog :title="title" v-model="open" width="600px" append-to-body :close-on-click-modal="false"
            :close-on-press-escape="false">
            <el-form ref="attendance_ubi_deviceRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="所属项目">
                    <el-tag effect="plain">{{ form.projectName }}</el-tag>
                </el-form-item>
                <el-form-item label="设备序列号" prop="deviceNo">
                    <el-input v-model="form.deviceNo" placeholder="请输入设备序列号" />
                </el-form-item>    
                <el-form-item label="通道编号" prop="channel">
                    <el-input v-model="form.channel" placeholder="请输入通道编号" />
                </el-form-item>            
                <el-form-item label="设备用途" prop="tag">
                    <el-select v-model="form.tag" placeholder="请选择设备用途" clearable>
                        <el-option v-for="(dict,idx) in ['考勤','培训']" :key="idx" :label="dict"
                            :value="dict" />
                    </el-select>
                </el-form-item>

                <el-form-item label="方向设置" prop="direction">
                    <el-radio-group v-model="form.direction">
                        <el-radio :value="0" :label="0">进</el-radio>
                        <el-radio :value="1" :label="1">出</el-radio>
                    </el-radio-group>
                </el-form-item>
               
                <el-form-item label="设备来源" prop="source" v-if="1==2">
                    <el-select v-model="form.source" placeholder="请选择设备来源" clearable>
                        <el-option v-for="dict in ubi_device_source" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>
              
                <el-form-item label="备注" prop="remark" v-if="false">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

    </el-drawer>
</template>

<script setup name="DeviceDrawer">
import { listAttendance_ubi_device, getAttendance_ubi_device, delAttendance_ubi_device, addAttendance_ubi_device, updateAttendance_ubi_device, authAttendance_ubi_device } from "@/api/manage/attendanceUbiDevice";

import { findMyProjectList } from "@/api/publics";
const { proxy } = getCurrentInstance();

const { ubi_device_source } = proxy.useDict('ubi_device_source');
const attendance_ubi_deviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,       
        projectId: null,
    },
    rules: {
        direction: [{ required: true, trigger: ['blur', 'change'], message: "请选择方向设置" }],
        tag: [{ required: true, trigger: ['blur', 'change'], message: "请选择设备用途" }],
        channel: [{ required: true, trigger: ['blur', 'change'], message: "请选择通道编号" }],
        deviceNo: [{ required: true, trigger: ['blur', 'change'], message: "请输入设备序列号" }],
    },
    projects: [],
    currentProId: '',
    mode: '',
    show: false,
    row: null
})
const { queryParams, form, rules } = toRefs(data);

/**
 * 设备人员同步
 */
 function handleAuth(row){
  authAttendance_ubi_device(row).then(d=>{
    if(d.code==200){
      proxy.$modal.msgSuccess("同步成功!");
    }else{
      proxy.$modal.msgError("同步失败!");
    }
  });
}

/** 查询宇泛的设备信息列表 */
function getList() {
    queryParams.value.projectId=data.row.projectId
    loading.value = true;
    listAttendance_ubi_device(queryParams.value).then(response => {
        attendance_ubi_deviceList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    comId: null,
    projectId: null,
    name: null,
    tag: '考勤',
    direction:0,
    channel:'',
    sceneGuid: "",
    source: null,
    deviceNo: null,
    addition: null,
    bindDefaultScene: null,
    forceEmptyDevice: null,
    password: null,
    deviceModel: null,
    deviceState: null,
    recType: null,
    onlineState: null,
    versionNo: null,
    lastActiveTime: null,
    hasRegister: null,
    state: null,
    remark: null,
    isDel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("attendance_ubi_deviceRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {  
  reset();  
  form.value.projectId = data.row.projectId;
  form.value.projectName = data.row.projectName;
  form.value.source=ubi_device_source.value[0].value;
  open.value = true;
  title.value = "添加宇泛的设备信息";
}

/** 修改按钮操作 */
function handleUpdate(row) { 
  reset();
  const _id = row.id || ids.value
  getAttendance_ubi_device(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改宇泛的设备信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["attendance_ubi_deviceRef"].validate(valid => {
    if (valid) {
      form.value.comId=data.row.comId;
      form.value.name=form.value.deviceNo;
      if (form.value.id != null) {
        updateAttendance_ubi_device(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAttendance_ubi_device(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除宇泛的设备信息编号为"' + _ids + '"的数据项？').then(function () {
    return delAttendance_ubi_device(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

function showDrawer(row) {
    data.row = row;
    data.show = true;
    getList();    
}

defineExpose({
    showDrawer
})
</script>

<style lang="scss">
.ubi-device-drawer{
    .el-drawer__header{
        margin-bottom: 0px;
    }
}</style>