<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <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-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"

              >新增</el-button
            >
          </el-col>


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

        <el-table
          v-loading="loading"
          :data="standardList"
          @selection-change="handleSelectionChange"
        >

          <el-table-column label="检测指标" align="center" prop="level" />
          <el-table-column
            label="COD（mg/L）"
            align="center"
            prop="cod"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="BOD（mg/L）"
            align="center"
            prop="bod"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="SS（mg/L）"
            align="center"
            prop="ss"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="氨氮（mg/L）"
            align="center"
            prop="nh3"
            width="120"
          />
          <el-table-column
            label="总氮（mg/L）"
            align="center"
            prop="tn"
            width="120"
          />

          <el-table-column
            label="总磷（mg/L）"
            align="center"
            prop="tp"
            width="120"
          />
          <el-table-column
          label="PH"
          align="center"
          prop="ph"
          width="120"
        />
          <el-table-column
            label="动植物油（mg/L）"
            align="center"
            prop="animalVegetableOil"
            width="120"
          />
          <el-table-column
            label="操作"
            align="center"
            width="150"
            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)"

                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"

                ></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-col>
    </el-row>

    <!-- 添加或修改配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="waterRef" label-width="100px">
        <el-form-item label="出水标准">
          <el-input v-model="form.region" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="依据文件名">
          <el-input v-model="form.basisDoc" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="检测指标">
          <el-input v-model="form.level" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="COD">
          <el-input v-model="form.cod" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="BOD">
          <el-input v-model="form.bod" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="SS">
          <el-input v-model="form.ss" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="氨氮NH3-N">
          <el-input v-model="form.nh3" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="总氮">
          <el-input v-model="form.tn" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="总磷">
          <el-input v-model="form.tp" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="PH">
          <el-input v-model="form.ph" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="动植物油">
          <el-input v-model="form.animalVegetableOil" 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>
  </div>
</template>

<script setup name="Discharge">
import city from "./city.json";
import {
  list,
  postData,
  putData,
  deleteData,
} from "@/api/renovation/scheme/discharge";

const { proxy } = getCurrentInstance();
const loading = ref();

const data = reactive({
  form: {},
  showSearch: false,
  total: "",
  title: "",
  standardList: [],
  deptOptions: [],
  multiple: true,
  single: true,
  open: false,
  ids:[],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    region: city[0].children[0].label,
  },
  rules: {},
});

const {
  queryParams,
  form,
  ids,
  rules,
  standardList,
  total,
  single,
  showSearch,
  deptOptions,
  title,
  open,
  multiple,
} = toRefs(data);



function getDeptTree() {
  deptOptions.value = city;
}
/** 查询列表 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value)).then((res) => {
    loading.value = false;
    standardList.value = res.rows;
    total.value = res.total;
  });
}
/** 节点单击事件 */
function handleNodeClick(data) {
  console.log(data);

  queryParams.value.region =data.id==101?data.children[0].label: data.label;
  handleQuery();
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const level = row.level ;
  proxy.$modal
    .confirm('是否确认删除指标为"' + level + '"的数据项？')
    .then(function () {
      return deleteData(row.standardId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/user/export",
    {
      ...queryParams.value,
    },
    `user_${new Date().getTime()}.xlsx`
  );
}


/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 导入按钮操作 */
function handleImport() {
  upload.title = "用户导入";
  upload.open = true;
}



/** 重置操作表单 */
function reset() {
  form.value = {
    standardId: "",
    region: "",
    basisDoc: "",
    level: "",
    cod: "",
    bod: "",
    ss: "",
    nh3: "",
    tn: "",
    tp: "",
    ph: "",
    animalVegetableOil: "",
  };
  proxy.resetForm("waterRef");
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  title.value = "添加数据";
  form.value.region = queryParams.value.region;
  open.value = true;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  open.value = true;
  form.region = queryParams.value.region;
  form.value = JSON.parse(JSON.stringify(row));
  title.value = "修改用户";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["waterRef"].validate((valid) => {
    if (valid) {
      if (form.value.standardId != '') {
        putData(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        postData(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

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