<template>
  <!-- 监测点报告 -->
  <div>
    <el-form :inline="true" :model="formData">
      <el-form-item label="模板类型:">
        <el-select
          v-model="formData.type"
          placeholder="请选择模板类型"
          clearable
          @change="changeFn"
        >
          <el-option
            v-for="item in templateType"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button
          type="primary"
          icon="el-icon-notebook-2"
          @click="$emit('flag')"
          >报告字典管理</el-button
        >
        <el-button type="primary" icon="el-icon-plus" @click="addTemplate"
          >新增模板</el-button
        >
        <!-- <el-button type="primary" icon="el-icon-upload2">导出</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      stripe
      v-loading="loading"
      :data="tableData"
      header-cell-class-name="table_header"
      style="width: 100%"
      highlight-current-row
      border
      :height="vh"
    >
      <el-table-column prop="name" label="模板名称"> </el-table-column>
      <el-table-column prop="code" label="模板描述"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="modify(scope.row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deletes(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      align="right"
      class="mt10"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formData.pageNum"
      :page-sizes="[20, 30, 40, 50, 100]"
      :page-size="formData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- <Template v-if="dialogVisible" @close='close' :title="title" :dialogVisible="dialogVisible" :TemplateTree="TemplateTree"/> -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="closeDialog" 
    >
      <el-row :gutter="20">
        <el-col :span="11"
          ><div class="grid-content">
            <el-tree
              :data="data"
              show-checkbox
              node-key="id"
              :props="defaultProps"
              :default-checked-keys="treeCheckedData"
              :default-expanded-keys="treeExpandData"
              @check-change="handleCheckChange"
              ref="tree"
            >
            </el-tree></div
        ></el-col>
        <el-col :span="13"
          ><el-form ref="form" :rules="rules" :model="form" label-width="60px">
            <el-form-item label="名称:" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="请输入名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="描述:" prop="code" class="top">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入描述"
                v-model.trim="form.code"
              >
              </el-input>
            </el-form-item> </el-form
        ></el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button
          v-if="this.title == '新增模板'"
          type="primary"
          @click="addDetermine"
          >确 定</el-button
        >
        <el-button
          v-else-if="this.title == '修改模板'"
          type="primary"
          @click="updateDetermine"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getList,
  getDictTree,
  add,
  update,
  deleteData,
} from "@/api/BusinessAdministrator/ReportConfiguration/MonitoringPoint";
export default {
  props: {},
  data() {
    return {
      loading: true,
      vh: undefined,
      formData: {
        pageNum: 1,
        pageSize: 20,
        type: 0,
      },
      total: 0,
      templateType: [
        {
          id: 0,
          label: "监测点报告",
        },
        {
          id: 1,
          label: "区域报告",
        },
      ],
      tableData: [],
      dialogVisible: false,
      title: "",
      TemplateTree: [],
      treeCheckedData: [],
      treeExpandData: [],
      form: {
        name: "",
        type: 0,
        code: "",
        ids: [],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "描述不能为空", trigger: "blur" }],
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "reportDescribe",
      },
    };
  },
  created() {
    this.info();
  },
  mounted() {
   
    this.setHeight()
        window.addEventListener('resize', this.setHeight)
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
          this.vh = window.sessionStorage.getItem("appheight") - 155;
          
        },
    closeDialog(){
    this.dialogVisible =false
     this.resetForm()
    },
        // 表单重置
  resetForm() {
      if (this.$refs["form"]) {
        this.$refs["form"].resetFields();
      }
    },
    //查询列表
    info() {
      this.loading = true;
      this.$emit("send", this.formData.type);
      getList(this.formData).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
      this.loading = false;
    },
    changeFn(val) {
      // console.log(val);
      this.formData.type = val;
      this.form.type = val;
      this.info();
    },
    //查询字典树
    searchTree() {
      getDictTree(this.formData).then((res) => {
        this.data = res.data;
        this.data.forEach((item) => {
          this.treeExpandData.push(item.id);
        });
      });
    },
    // 新增
    addTemplate() {
      this.title = "新增模板";
      this.form = {};
      this.treeCheckedData = [];
      this.searchTree();
      this.dialogVisible = true;
    },
    //新增确定
    addDetermine() {
      this.form.type = this.formData.type;
      add(this.form).then((res) => {
        if (res.code == "A0000") {
          this.$message({
            message: res.message,
            type: "success",
          });
        }
        this.dialogVisible = false;
        this.info();
      });
    },
    // 修改
    modify(row) {
     // console.log(row);
      this.title = "修改模板";
      this.searchTree();
      (this.form = {
        name: row.name,
        type: row.type,
        code: row.code,
        id: row.id,
        ids: row.rdIds,
      }),
        (this.treeCheckedData = row.rdIds);
      this.dialogVisible = true;
    },
    //修改确定
    updateDetermine() {
      update(this.form).then((res) => {
        if (res.code == "A0000") {
          this.$message({
            message: res.message,
            type: "success",
          });
        }
        this.dialogVisible = false;
        this.info();
      });
    },
    // 删除
    deletes(row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = [row.id];
          deleteData(data).then((res) => {
            if ((res.code == "A0000")) {
              this.$message({
                type: "success",
                message: res.message,
              });
            }
            this.info();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      this.formData.pageSize = val;
      this.info();
    },
    handleCurrentChange(val) {
      this.formData.pageNum = val;
      this.info();
    },

    // 树多选
    handleCheckChange(data, checked, indeterminate) {
      // 获取当前选中的所有节点ID
      // console.log(this.$refs.tree.getCheckedKeys());
      // console.log(data, checked, indeterminate);
      this.form.ids = this.$refs.tree.getCheckedKeys();
    },
  },

  computed: {},

  watch: {},
};
</script>
<style lang="less" scoped>
@import url("../../../../styles/comStyle.less");
::v-deep .el-table .cell {
  text-align: center;
}
::v-deep .el-tabs--border-card > .el-tabs__content {
  padding: 10px;
}
</style>

<style lang="scss" scoped>
.BOXbutton {
  display: flex;
  justify-content: end;

  .el-button + .el-button,
  .el-checkbox.is-bordered + .el-checkbox.is-bordered {
    margin-left: 15px;
  }
}
::v-deep.el-tree {
  height: 400px;
  overflow-y: auto;
}
::v-deep textarea {
  resize: none;
}
::v-deep .el-form-item--small.el-form-item {
  margin-bottom: 15px;
}
</style>
