<template>
  <section class="app-container">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="名称" prop="productName">
            <el-input v-model="queryParams.productName" placeholder="请输入名称" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:product:add']">增加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['monitor:product:remove']">批量删除</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleClean" v-hasPermi="['monitor:job:remove']">清空
            </el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['monitor:job:export']">导出
            </el-button>
          </el-col> -->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="productId" align="center" label="产品ID">
          </el-table-column>
          <el-table-column prop="productName" align="center" label="产品名称">
          </el-table-column>
          <el-table-column prop="remark" align="center" label="产品备注">
          </el-table-column>
          <!-- <el-table-column prop="productType" align="center" label="产品编码">
          </el-table-column> -->
          <!-- <el-table-column prop="productType" align="center" label="类型">
          </el-table-column> -->
          <!-- <el-table-column prop="status" align="center" label="状态">
          </el-table-column> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAddnode(scope.row)"
                v-hasPermi="['system:product:addNode']">新增节点
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:product:edit']">
                修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleview(scope.row)" v-hasPermi="['system:node:nodeList']">
                查看节点
              </el-button>
              <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:product:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <!-- <el-form-item label="类型" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入产品类型" />
        </el-form-item> -->
        <!-- <el-form-item label="状态">
          <el-input v-model="form.status" />
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加节点参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="nodeOpen" width="500px" append-to-body>
      <el-form ref="nodeForm" :model="nodeForm" :rules="nodeRules" label-width="100px">
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="nodeForm.productId" disabled placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="nodeForm.nodeName" placeholder="请输入节点名称" />
        </el-form-item>
        <!-- <el-form-item label="状态">
          <el-input v-model="nodeForm.status" />
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input v-model="nodeForm.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveNode">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 节点列表弹框 -->
    <el-dialog title="节点列表" :visible.sync="nodeDialog">
      <el-row :gutter="10" class="mb8">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="nodeMultiple" @click="batchHandleDeletenode"
          v-hasPermi="['monitor:node:remove']">批量删除</el-button>
      </el-row>

      <el-table :data="nodeData" @selection-change="nodeHandleSelectionChange">
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="nodeName" label="节点名称"></el-table-column>
        <el-table-column prop="remark" label="节点备注"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdatenode(scope.row)" v-hasPermi="['system:node:edit']">
              修改
            </el-button>
            <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDeletenode(scope.row)"
              v-hasPermi="['system:node:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </section>
</template>

<script>
import { list, Info, add, update, del } from "@/api/hardware/product";
import { productNodeList, productNodeInfo, productNodeSelect, productNodeAdd, productNodeDel } from "@/api/hardware/productNode";
export default {
  name: "ProductList",
  data () {
    return {
      nodeData: null,
      nodeDialog: false,
      // 选中数组
      ids: [],
      nodeIds: [],
      // 非多个禁用
      multiple: true,
      nodeMultiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      nodeOpen: false,
      labelPosition: "right",
      options: [
        {
          value: "选项1",
        },
        {
          value: "选项2",
        },
      ],
      // 表单参数
      form: {},
      nodeForm: {},
      // 总条数
      total: 0,
      value: "",
      // 表格数据
      tableData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: ''
      },
      rules: {
        productId: [
          { required: true, message: "产品ID不能为空", trigger: "blur" },
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
        ],
        productType: [
          { required: true, message: "产品编码不能为空", trigger: "blur" },
        ],

      },
      nodeRules: {
        productId: [
          { required: true, message: "产品ID不能为空", trigger: "blur" },
        ],
        nodeId: [
          { required: true, message: "节点ID不能为空", trigger: "blur" },
        ],
        nodeName: [
          { required: true, message: "节点名称不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取产品数据列表
    getList () {
      list(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },

    // 获取节点数据列表
    getListnode ({ productId }) {
      this.nodeData = [];
      productNodeList({ productId }).then((response) => {
        this.nodeData = response.rows;
      });
    },

    /** 查看按钮操作 */
    handleview (row) {
      this.nodeDialog = true;
      this.getListnode(row)
    },


    // 取消按钮
    cancel () {
      this.open = false;
      this.nodeOpen = false;
      this.reset();
    },

    // 表单重置
    reset () {
      this.form = {
        productId: "",
        productType: "",
        productName: "",
        status: "",
      };
      this.nodeForm = {
        productId: "",
        nodeId: "",
        nodeName: "",
        status: "",
      };
      this.resetForm("form");
      this.resetForm("nodeForm");
    },

    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    nodeHandleSelectionChange (selection) {
      this.nodeIds = selection.map((item) => item.id);
      this.nodeMultiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    handleAddnode (row) {
      this.reset();
      this.nodeOpen = true;
      this.title = "添加产品节点";
      this.nodeForm.productId = row.productId;
    },

    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改产品";
    },

    /** 修改节点操作 */
    handleUpdatenode (row) {
      this.reset();
      this.nodeOpen = true;
      this.nodeForm = row;
      this.title = "修改产品节点";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then((res) => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }
            });
          } else {
            add(this.form).then((res) => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }
            });
          }
        }
      });
    },
    /** 节点提交操作 */
    saveNode: function () {
      productNodeAdd(this.nodeForm).then((res) => {
        if (200 == res.code) {
          this.msgSuccess(this.title + "成功");
          this.nodeOpen = false;
          this.getList();
        } else {
          this.msgSuccess(res.msg);
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(ids);
        })
        .then((res) => {
          if (200 == res.code) {
            this.getList();
            this.msgSuccess("删除成功");
          } else {
            this.msgSuccess(res.msg);
          }
        })
        .catch((res) => { });
    },

    /** 删除节点按钮操作 */
    handleDeletenode (row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return productNodeDel(ids);
        })
        .then((res) => {
          if (200 == res.code) {
            this.getListnode(row);
            this.msgSuccess("删除成功");
          } else {
            this.msgSuccess(res.msg);
          }
        })
        .catch((res) => { });
    },

    // 批量删除节点
    batchHandleDeletenode () {
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return productNodeDel(this.nodeIds);
        })
        .then((res) => {
          if (200 == res.code) {
            this.getListnode(...this.nodeData);
            this.msgSuccess("删除成功");
          } else {
            this.msgSuccess(res.msg);
          }
        })
        .catch((res) => { });
    },

    // 页码尺寸改变
    handleSizeChange (val) {
      this.queryForm.pagesize = val;
      this.getList();
    },
    // 页码改变
    handleCurrentChange (val) {
      this.queryForm.page = val;
      this.getList();
    },
    // handleSelectionChange(val) {
    //   this.multipleSelection = val;
    // },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
    },
    // 对话框的关闭
    handleClose (done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => { });
    },
  },
};
</script>


<style></style>
