<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="deviceId">
        <el-input
            v-model="queryParams.deviceId"
            placeholder="请输入设备ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点位ID" prop="pointId">
        <el-input
            v-model="queryParams.pointId"
            placeholder="请输入点位ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="控制结果" prop="controlResult">
        <el-select v-model="queryParams.controlResult" placeholder="请选择结果" style="width: 140px"
                   @keyup.enter="handleQuery">
          <el-option
              v-for="dict in control_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="操作人" prop="operatorName">
        <el-input
            v-model="queryParams.operatorName"
            placeholder="请输入操作人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="controlType">
          <el-select v-model="queryParams.controlType" placeholder="请选择操作类型" clearable style="width: 240px"
                     @keyup.enter="handleQuery">
            <el-option
                v-for="dict in root_control_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
      <el-form-item label="操作时间" style="width: 308px">
        <el-date-picker v-model="queryParams.operateTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['device:control:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="controlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID1" align="center" prop="id" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="点位ID" align="center" prop="pointId" />
      <el-table-column label="控制类型" align="center" prop="controlType">
        <template #default="scope">
          <dict-tag :options="root_control_type" :value="scope.row.controlType"/>
        </template>

      </el-table-column>
      <el-table-column label="原值" align="center" prop="oldValue" />
      <el-table-column label="新值" align="center" prop="newValue" />
      <el-table-column label="控制结果" align="center" prop="controlResult">
        <template #default="scope">
          <dict-tag :options="control_result" :value="scope.row.controlResult"/>
        </template>
      </el-table-column>
      <el-table-column label="操作人ID" align="center" prop="operatorId" />
      <el-table-column label="操作人姓名" align="center" prop="operatorName" />
      <el-table-column label="操作时间" align="center" prop="operateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['device:control:query']">详情</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="800px" append-to-body>
      <el-form ref="controlRef" :model="form" :rules="rules" label-width="100px" disabled>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="点位ID" prop="pointId">
          <el-input v-model="form.pointId" placeholder="请输入点位ID" />
        </el-form-item>
        <el-form-item label="原值" prop="oldValue">
          <el-input v-model="form.oldValue" placeholder="请输入原值" />
        </el-form-item>
        <el-form-item label="新值" prop="newValue">
          <el-input v-model="form.newValue" placeholder="请输入新值" />
        </el-form-item>
        <el-form-item label="控制结果" prop="controlResult">
          <el-input v-model="form.controlResult" placeholder="请输入控制结果" />
        </el-form-item>
        <el-form-item label="操作人ID" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作人ID" />
        </el-form-item>
        <el-form-item label="操作人姓名" prop="operatorName">
          <el-input v-model="form.operatorName" placeholder="请输入操作人姓名" />
        </el-form-item>
        <el-form-item label="操作时间" prop="operateTime">
          <el-date-picker clearable
                          v-model="form.operateTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Control">
import { listControl, getControl, delControl, addControl, updateControl } from "@/api/device/control";

const { proxy } = getCurrentInstance();
const {root_control_type,control_result} = proxy.useDict('root_control_type','control_result');


const controlList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = 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,
    deviceId: null,
    pointId: null,
    controlType: null,
    controlResult: null,
    operatorName: null,
  },
  rules: {
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    pointId: [
      { required: true, message: "点位ID不能为空", trigger: "blur" }
    ],
    controlType: [
      { required: true, message: "控制类型不能为空", trigger: "change" }
    ],
    operatorId: [
      { required: true, message: "操作人ID不能为空", trigger: "blur" }
    ],
    operatorName: [
      { required: true, message: "操作人姓名不能为空", trigger: "blur" }
    ],
    operateTime: [
      { required: true, message: "操作时间不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ]
  }
});

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

/** 查询设备控制记录列表 */
function getList() {
  loading.value = true;
  listControl(queryParams.value).then(response => {
    controlList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceId: null,
    pointId: null,
    controlType: null,
    oldValue: null,
    newValue: null,
    controlResult: null,
    operatorId: null,
    operatorName: null,
    operateTime: null,
    remark: null,
    createTime: null
  };
  proxy.resetForm("controlRef");
}

/** 搜索按钮操作 */
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();
  open.value = true;
  title.value = "添加设备控制记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getControl(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "设备控制记录详情";
  });
}
/** 详细按钮操作 */
function handleView(row) {
  open.value = true;
  form.value = row;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["controlRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateControl(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addControl(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 delControl(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('device/control/export', {
    ...queryParams.value
  }, `control_${new Date().getTime()}.xlsx`)
}

getList();
</script>
