<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="topicID">
            <el-input v-model="queryParams.topicId" placeholder="请输入topicID" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="groupID">
            <el-input v-model="queryParams.groupId" placeholder="请输入groupID" 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">增加</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete">批量删除</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="topicId" align="center" label="topicID">
          </el-table-column>
          <el-table-column prop="groupId" align="center" label="groupID">
          </el-table-column>
          <el-table-column prop="remark" 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-edit" @click="handleView(scope.row)">编辑项目</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</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="topicID" prop="topicId">
          <el-input v-model="form.topicId" :disabled="!!form.id" placeholder="请输入topicID" />
        </el-form-item>
        <el-form-item label="groupID" prop="groupId">
          <el-input v-model="form.groupId" :disabled="!!form.id" placeholder="请输入groupID" />
        </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="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 关联项目参数配置对话框 -->
    <el-dialog title="项目管理列表" :visible.sync="projectListOpen" width="800px" append-to-body>
      <el-row>
        <el-col :span="8">
          <span v-if="!projectIds.length">暂无项目</span>
          <el-tag class="mb5 mr5" v-for="tag in projectIds" :key="tag.projectId" closable @close="handleTopicProDel(tag)">
            {{tag.projectName}}
          </el-tag>
        </el-col>
        <el-col :span="16">
          <el-table :data="projectList"
            :row-class-name="({row}) => { if(projectIds.find(v => v.projectId == row.projectId)) return 'topicListDisabled' }"
            style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column prop="projectId" align="center" label="项目ID">
            </el-table-column>
            <el-table-column prop="projectName" align="center" label="项目名称">
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-plus" @click="handleTopicProjectSave(scope.row)">添加</el-button>
              </template>
            </el-table-column>

          </el-table>
          <pagination v-show="proTotal > 0" :total="proTotal" :page.sync="proQueryParams.pageNum" :limit.sync="proQueryParams.pageSize"
            @pagination="getProjectList" />
        </el-col>
      </el-row>
      <!-- <el-checkbox-group v-model="projectIds">
        <el-row>
          <el-col style="margin-bottom: 15px" :span="12" v-for="data in projectList" :key="data.projectId">
            <el-checkbox size="medium" :label="data.projectId">{{data.projectName}}</el-checkbox>
          </el-col>
        </el-row>
      </el-checkbox-group> -->
      <!-- <el-table :data="projectList" @selection-change="handleProjectSelectionChange">
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="projectName" align="center" label="项目名称">
        </el-table-column>
      </el-table> -->
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPro">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
    <!-- 查看项目列表弹框 -->
    <!-- <el-dialog title="项目管理列表" :visible.sync="projectManagementOpen">
      <el-row class="mb8">
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="batchHandleDelete">批量删除</el-button>
      </el-row>
      <el-table :data="proData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="projectId" align="center" label="项目ID">
        </el-table-column>
        <el-table-column prop="projectName" align="center" label="项目名称">
        </el-table-column>
        <el-table-column prop="topicId" align="center" label="topicId">
        </el-table-column>
      </el-table>
    </el-dialog> -->
  </section>
</template>

<script>
import { list, info, add, del, select } from "@/api/topicInfo/info";
import { save, saves, topicProDel, topicProSelect } from "@/api/topicInfo/topicProject";
import { list as proList } from "@/api/hardware/project";
export default {
  name: "TopicList",
  data () {
    return {
      // 选中数组
      ids: [],
      projectIds: [],
      // 所有项目
      projectList: [],
      // 表格数据
      tableData: [],
      proData: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      proTotal: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      projectListOpen: false,
      projectManagementOpen: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      proQueryParams: {
        pageNum: 1,
        pageSize: 5
      },
      snapRow: {},
      select: {},
      row: {},
      rules: {
        topicId: [
          { required: true, message: "topicID不能为空", trigger: "blur" },
        ],
        groupId: [
          { required: true, message: "groupID不能为空", trigger: "blur" },
        ]
      },
    }
  },
  created () {
    this.getProjectList()
    this.getList()
  },
  methods: {
    rowClassRow ({ row, rowIndex }) {
      if (this.projectIds.find(v => v.projectId == row.projectId)) {
        return 'topicListDisabled';
      }
      return '';
    },
    // 获取Topic列表
    getList () {
      list(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    // 获取项目列表
    getProjectList () {
      proList(this.proQueryParams).then(({ code, msg, rows, total }) => {
        if (200 == code) {
          this.projectList = rows;
          this.proTotal = total;
          this.$forceUpdate()
        } else {
          this.msgError(msg);
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams = {};
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加topic";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改topic";
    },
    /** 提交按钮 */
    submit () {
      add(this.form).then(res => {
        this.open = false;
        this.getList()
      })
    },
    handleView (row) {
      this.row = row;
      this.topicProSelect(row);
    },
    topicProSelect (row) {
      topicProSelect(row.topicId).then(response => {
        this.projectIds = response.select;
      });
      this.snapRow = Object.assign({}, row);
      this.projectListOpen = true;
    },
    handleTopicProjectSave (row) {
      const loading = this.$loading({
        lock: true,
        text: '添加中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      });
      save({ topicId: this.snapRow.topicId, projectId: row.projectId }).then(res => {
        if (200 == res.code) {
          this.topicProSelect(this.row);
          this.getProjectList();
        } else {
          this.msgError(res.msg);
        }
        loading.close();
      });
    },
    handleTopicProDel ({ id }) {
      const loading = this.$loading({
        lock: true,
        text: '添加中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      });
      topicProDel(id).then(res => {
        this.topicProSelect(this.row);
        this.getProjectList();
        loading.close();
      })
    },
    /** 删除按钮操作 */
    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.msgError(res.msg);
          }
        })
        .catch((res) => { });
    },

    // 批量删除项目
    batchHandleDelete () { },
    // 表单重置
    reset () {
      this.form = {
        topicId: "",
        groupId: "",
        status: "",
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.projectListOpen = false;
      this.reset();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    handleProjectSelectionChange (selection) {
      this.projectIds = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
  }

}
</script>

<style>
.topicListDisabled {
  pointer-events: none;
  cursor: default;
  opacity: 0.6;
}
</style>