<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-button type="primary" plain icon="Plus" @click="handleAddCap"
            >新增吨位</el-button
          >
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="{ label: 'label', children: 'children' }"
            :expand-on-click-node="false"
            ref="deptTreeRef"
            node-key="id"
            highlight-current
            default-expand-all
            style="height: 85vh; overflow-y: scroll"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="设备名称" prop="userName">
            <el-input
              v-model="queryParams.equipmentName"
              placeholder="请输入设备名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item label="类型" prop="status">
            <el-select
              v-model="queryParams.equipmentType"
              placeholder="设备类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in equipment_type"
                :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="handDeviceAdd"
              >新增</el-button
            >
          </el-col>

          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          style="margin-top: 20px"
          border
          :data="deviceList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="序号"
            type="index"
            width="55"
            align="center"
          />

          <el-table-column
            label="设备名称"
            align="center"
            prop="equipmentName"
          />

          <el-table-column
            label="设备尺寸"
            align="center"
            prop="equipmentSize"
          />
          <el-table-column label="设备类型" align="center" prop="equipmentType">
            <template #default="scope">
              {{
                equipment_type.filter(
                  (item) => item.value == scope.row.equipmentType
                )[0].label
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="250px"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleExit(scope.row)"
                >编辑</el-button
              >
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleSet(scope.row)"
                >配置</el-button
              >
              <el-button
                link
                type="danger"
                icon="Delete"
                @click="handleDeviceDelete(scope.row)"
                >删除</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-col>
    </el-row>

    <el-dialog :title="title" v-model="open" width="50%" append-to-body>
      <el-button type="primary" plain icon="Plus" @click="handleAdd"
        >新增</el-button
      >
      <el-table
        v-loading="loading"
        style="margin-top: 20px"
        border
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="主要设备" align="center" prop="componentName" />

        <el-table-column
          label="参数"
          show-overflow-tooltip
          align="center"
          prop="param"
        />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="单位" align="center" prop="quantity" >
          <template #default="scope">

            {{equipment_component_unit.filter(item=>item.value==scope.row.unit)[0].label}}
          </template>

        </el-table-column>
        <el-table-column label="品牌" align="center" prop="componentBrand" >
          <template #default="scope">

          {{equipment_component_brand.filter(item=>item.value==scope.row.componentBrand)[0].label}}
        </template>

      </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleComSet(scope.row)"
              >编辑</el-button
            >
            <el-button
              link
              type="danger"
              icon="Delete"
              @click="handleDeleteCom(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="openDist" width="30%" append-to-body>
      <el-form :model="form" ref="deviceComRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="主要设备">
              <el-input
                v-model="form.componentName"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="参数">
              <el-input
                v-model="form.param"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="组件类型">
              <el-select
              v-model="form.componentType"
              placeholder="组件类型"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="dict in equipment_component_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
            </el-form-item>
            <el-form-item label="单位">
              <el-select
                v-model="form.unit"
                placeholder="品牌"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in equipment_component_unit"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="数量">
              <el-input
                v-model="form.quantity"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="品牌">
              <el-select
                v-model="form.componentBrand"
                placeholder="品牌"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in equipment_component_brand"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitComponent">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="openExit" width="30%" append-to-body>
      <el-form :model="form" ref="deviceRef" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备名称">
              <el-input
                v-model="form.equipmentName"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="设备尺寸">
              <el-input
                v-model="form.equipmentSize"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="处理量">
              <el-select
                v-model="form.capacityIds"
                multiple
                placeholder="设备类型"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in deptOptions[0].children"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="设备类型">
              <el-select
                v-model="form.equipmentType"
                placeholder="设备类型"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in equipment_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </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-dialog :title="title" v-model="openCapacity" width="30%" append-to-body>
      <el-form :model="form" ref="deviceCap" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="吨位">
              <el-input
                v-model="form.capacityTonnage"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitCapacity">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="porfiles">
import {
  list as equipmentList,
  postData as equipmentpos,
  putData as equipmentput,
  deleteData as equipmentdelete,
} from "@/api/renovation/scheme/equipment";
import {
  list as equipmentComponentList,
  postData as equipmentComponentpos,
  putData as equipmentComponentput,
  deleteData as equipmentComponentdelete,
} from "@/api/renovation/scheme/equipmentComponent";
import {
  list,
  postData,
  putData,
  deleteData,
} from "@/api/renovation/scheme/capacity";

const { proxy } = getCurrentInstance();
const { equipment_component_brand, equipment_type,equipment_component_type,equipment_component_unit } = proxy.useDict(
  "equipment_component_brand",
  "equipment_type",
  "equipment_component_type",
  "equipment_component_unit"
);

const showDetail = ref(false);
const backCall = () => {
  if (!document.startViewTransition) {
    showDetail.value = false;
    return;
  }
  document.startViewTransition(() => (showDetail.value = false));
  getList();
  // showDetail.value = false
};
const data = reactive({
  // 按钮loading
  buttonLoading: false,
  openCapacity: false,
  // 遮罩层
  loading: false,
  // 选中数组
  ids: [],
  openExit: false,
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  deptOptions: [
    {
      label: "全部吨位",
      value: "0",
      children: [],
    },
  ],
  // 总条数
  total: 0,
  dataList: [],
  // 图层管理表格数据
  deviceList: [],

  title: "",
  // 是否显示弹出层
  open: false,
  openDist: false,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createBy: undefined,
    layersId: undefined,
  },
  // 表单参数
  form: {},
  // 表单校验
});
const input = ref("");
const {
  open,
  dataList,
  showSearch,
  schemetype,
  queryParams,
  form,
  deptOptions,
  ids,
  openExit,
  title,
  openDist,
  deviceList,
  openCapacity,
  total,
  multiple,
  single,
  loading,

  buttonLoading,
} = toRefs(data);
function submitCapacity() {
  proxy.$refs["deviceCap"].validate((valid) => {
    if (valid) {
      if (form.value.capacityId != undefined) {
        putData(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          openCapacity.value = false;
          getcapacityList();
        });
      } else {
        postData(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          openCapacity.value = false;
          getcapacityList();
        });
      }
    }
  });
}
function submitComponent() {
  proxy.$refs["deviceComRef"].validate((valid) => {
    if (valid) {
      if (form.value.componentId != undefined) {
        equipmentComponentput(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          openDist.value = false;
          handleSet();
        });
      } else {
        equipmentComponentpos(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          openDist.value = false;
          handleSet();
        });
      }
    }
  });
}
function cancel() {
  openDist.value = false;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceRef"].validate((valid) => {
    if (valid) {
      if (form.value.equipmentId != undefined) {
        equipmentput(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          openExit.value = false;
          getList();
        });
      } else {
        equipmentpos(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          openExit.value = false;
          getList();
        });
      }
    }
  });
}

function handDeviceAdd() {
  title.value = "添加";
  form.value = {};
  openExit.value = true;
}
function handleAdd() {
  title.value = "添加";
  form.value = {
    equipmentId: equipmentId.value,
  };
  openDist.value = true;
}
function handleAddCap() {
  title.value = "添加";
  form.value = {};
  openCapacity.value = true;
}
/** 表单重置 */
function reset() {
  form.value = {
    paramId: undefined,
  };
  proxy.resetForm("applyRef");
}
/** 新增按钮操作 */
function handleDeviceAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

function handleDeleteCom(row) {
  const componentName = row.componentName;
  proxy.$modal
    .confirm('是否确认删除设备为"' + componentName + '"的数据项？')
    .then(function () {
      return equipmentComponentdelete(row.componentId);
    })
    .then(() => {
      handleSet();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 删除按钮操作 */
function handleDeviceDelete(row) {
  const equipmentName = row.equipmentName;
  proxy.$modal
    .confirm('是否确认删除名称为"' + equipmentName + '"的数据项？')
    .then(function () {
      return equipmentdelete(row.equipmentId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 删除按钮操作 */
function handleDelete(row) {
  const level = row.processingCapacity;
  proxy.$modal
    .confirm('是否确认删除处理量为"' + level + '"的数据项？')
    .then(function () {
      return deleteData(row.paramId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
//详情
const arrSite = ref([]);
const planData = ref({});

function handleComSet(row) {
  form.value = JSON.parse(JSON.stringify(row));
  openDist.value = true;
}

const equipmentId = ref(null);
function handleSet(row) {
  row && (equipmentId.value = row.equipmentId);
  equipmentComponentList({ equipmentId: equipmentId.value }).then((res) => {
    title.value = "编辑";
    dataList.value = res.rows;
    form.value = {};
    form.value.equipmentId = equipmentId.value;
    open.value = true;
  });
}

async function handleExit(row) {
  title.value = "编辑";
  form.value = JSON.parse(JSON.stringify(row));
  openExit.value = true;
}
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleNodeClick(e) {
  queryParams.value.capacityId = e.value==0?null:e.value;
  getList();
}
/** 查询理列表 */
function getList() {
  //  loading.value = false;
  loading.value = true;
  equipmentList(queryParams.value).then((response) => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getcapacityList() {
  list().then((response) => {
    let data = response.data.map((item) => {
      return {
        label: item.capacityTonnage + " 吨",
        value: item.capacityId,
      };
    });
    deptOptions.value[0].children = data;
    deviceList.value = response.rows;

    getList();
  });
}
function resetQuery() {
  proxy.resetForm("queryForm");
}
function handleQuery() {
  getList();
}
getcapacityList();
</script>
<style lang="scss" scoped>
.fade-enter-active {
  transition: all 0.3s ease-out;
}
.fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.fade-enter-from,
.fade-leave-to {
  transform: translateX(-120px);
  opacity: 0;
}

.detail-enter-active,
.detail-leave-active {
  transition: opacity 0.5s ease;
}
.detail-enter-from,
.detail-leave-to {
  opacity: 0;
}
.borderBt {
  border-bottom: 1px solid #ebeef5;
  padding: 0 0 4px;
  margin: 5px;
}
</style>
