<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型编码" prop="modelCode">
        <el-input
          v-model="queryParams.modelCode"
          placeholder="请输入模型编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型类别Id" prop="modelTypeId">
        <el-select v-model="queryParams.modelTypeId" placeholder="请选择模型类别Id" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="上级id" prop="pid">
        <el-input
          v-model="queryParams.pid"
          placeholder="请输入上级id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否是叶节点" prop="isLeaf">
        <el-select v-model="queryParams.isLeaf" placeholder="请选择是否是叶节点" clearable>
          <el-option
            v-for="dict in dc_is_leaf"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否可用" prop="vailable">
        <el-select v-model="queryParams.vailable" placeholder="请选择是否可用" clearable>
          <el-option
            v-for="dict in available"
            :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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['model:devicemodel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="devicemodelList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="模型编码" align="center" prop="modelCode" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="模型类别Id" align="center" prop="modelTypeId" />
      <el-table-column label="上级id" align="center" prop="pid" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="是否是叶节点" align="center" prop="isLeaf">
        <template #default="scope">
          <dict-tag :options="dc_is_leaf" :value="scope.row.isLeaf"/>
        </template>
      </el-table-column>
      <el-table-column label="是否可用" align="center" prop="vailable">
        <template #default="scope">
          <dict-tag :options="available" :value="scope.row.vailable"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:devicemodel:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['model:devicemodel:add']"
          >新增</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:devicemodel:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设备模型信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="devicemodelRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="模型编码" prop="modelCode">
          <el-input v-model="form.modelCode" placeholder="请输入模型编码" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="模型类别Id" prop="modelTypeId">
          <el-select v-model="form.modelTypeId" placeholder="请选择模型类别Id">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="上级id" prop="pid">
          <el-tree-select
            v-model="form.pid"
            :data="devicemodelOptions"
            :props="{ value: 'id', label: 'modelName', children: 'children' }"
            value-key="id"
            placeholder="请选择上级id"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否是叶节点">
          <el-radio-group v-model="form.isLeaf">
            <el-radio
              v-for="dict in dc_is_leaf"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否可用">
          <el-radio-group v-model="form.vailable">
            <el-radio
              v-for="dict in available"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </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>
  </div>
</template>

<script setup name="Devicemodel">
import { listDevicemodel, getDevicemodel, delDevicemodel, addDevicemodel, updateDevicemodel } from "@/api/model/devicemodel";

const { proxy } = getCurrentInstance();
const { available, dc_is_leaf } = proxy.useDict('available', 'dc_is_leaf');

const devicemodelList = ref([]);
const devicemodelOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);
const router = useRouter();
const data = reactive({
  form: {},
  queryParams: {
    modelName: null,
    modelCode: null,
    sort: null,
    modelTypeId: null,
    pid: null,
    note: null,
    isLeaf: null,
    vailable: null
  },
  rules: {
    modelName: [
      { required: true, message: "模型名称不能为空", trigger: "blur" }
    ],
    modelCode: [
      { required: true, message: "模型编码不能为空", trigger: "blur" }
    ],
    sort: [
      { required: true, message: "排序不能为空", trigger: "blur" }
    ],
    modelTypeId: [
      { required: true, message: "模型类别Id不能为空", trigger: "change" }
    ],
    pid: [
      { required: true, message: "上级id不能为空", trigger: "blur" }
    ],
    isLeaf: [
      { required: true, message: "是否是叶节点不能为空", trigger: "blur" }
    ],
    vailable: [
      { required: true, message: "是否可用不能为空", trigger: "blur" }
    ]
  }
});

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

/** 查询设备模型信息列表 */
function getList() {
  loading.value = true;
  listDevicemodel(queryParams.value).then(response => {
    devicemodelList.value = proxy.handleTree(response.data, "id", "pid");
    loading.value = false;
  });
}

/** 查询设备模型信息下拉树结构 */
function getTreeselect() {
  listDevicemodel().then(response => {
    devicemodelOptions.value = [];
    const data = { id: 0, modelName: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "pid");
    devicemodelOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    tentantId: null,
    version: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    deptId: null,
    userId: null,
    deleteBy: null,
    deleteTime: null,
    id: null,
    modelName: null,
    modelCode: null,
    sort: null,
    modelTypeId: null,
    pid: null,
    note: null,
    isLeaf: "0",

    vailable: "0"

  };
  proxy.resetForm("devicemodelRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

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

/** 新增按钮操作 */
function handleAdd(row) {
  const path = "/newdevicemodel";
  let pid = 0;
  if(row.isLeaf){
    proxy.$message({
      message: '请选择非叶节点行再操作',
      type: 'warn'
    });
    return;
  }
  if (row != null && row.id) {
    pid = row.id;
  }
  router.push({
    name:path,
    query:{
      pid:pid,
      // devicemodelOptions:devicemodelOptions
    }
  });
  
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.pid = row.id;
  }
  getDevicemodel(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备模型信息";
  });
}

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

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

 /** 查询设备模型信息下拉树结构 */

//  getTreeselect();
getList();
</script>
