<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" @submit.native.prevent size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入数据组名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="info">
        <el-input v-model="queryParams.info" placeholder="请输入数据提示" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="configName">
        <el-input v-model="queryParams.configName" placeholder="请输入关键字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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 type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:group:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:group:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:group:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:group:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <Page uri="/mapi/system/group/list" :request-params="queryParams" ref="pagination" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed="left" />
        <el-table-column label="组合数据ID" align="center" prop="id" />
        <el-table-column label="数据组名称" align="center" prop="name" />
        <el-table-column label="数据提示" align="center" prop="info" />
        <el-table-column label="关键字" align="center" prop="configName" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleList(scope.row)" v-hasPermi="['system:group:edit']">数据列表</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:group:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:group:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </Page>

      <!-- 添加或修改组合数据对话框 -->
      <el-drawer :title="title" :visible.sync="open" size="30%"  :before-close="checkClose" append-to-body>
        <el-row :gutter="15" style="margin-bottom: 80px">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-col :span="24">
              <el-form-item label="数据组名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入数据组名称" maxlength="50" show-word-limit />

              </el-form-item>
              <el-form-item label="数据提示" prop="info">
                <el-input v-model="form.info" placeholder="请输入数据提示" maxlength="256" show-word-limit />

              </el-form-item>
              <el-form-item label="关键字" prop="configName">
                <el-input v-model="form.configName" placeholder="请输入关键字" maxlength="50" show-word-limit />

              </el-form-item>
              <el-form-item label="数据组字段以及类型" prop="fields">
                <el-input v-model="form.fields" type="textarea" placeholder="请输入数据组字段以及类型" maxlength="${length}" show-word-limit />

              </el-form-item>
            </el-col>
          </el-form>
        </el-row>

        <div class="horizontal-view box-shadow drawer-bottom-buttons">
          <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-drawer>
      <group-from ref="groupfroms" :titleFrom="titleFrom" :groupId="groupId" :addId="addId" @getList="getList"></group-from>
  </div>
</template>

<script>
import request from '@/utils/request'
import { listGroup, getGroup, delGroup, addGroup, updateGroup , updateGroupStatus } from "@/api/system/group";
import groupFrom from './components/groupFrom';
import editFrom from '@/components/from/from';
export default {
  name: "Group",
  components: { groupFrom, editFrom },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组合数据表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
        info: null,
        configName: null,
        fields: null
      },
      groupId: 0,
      titleFrom: '',
      addId: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "数据组名称不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "数据提示不能为空", trigger: "blur" }
        ],
        configName: [
          { required: true, message: "关键字不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询组合数据列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        info: null,
        configName: null,
        fields: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
    //   this.open = true;
    this.$refs.groupfroms.modals = true;
      this.title = "添加组合数据";
      this.$refs.groupfroms.modals = true;
      this.titleFrom = "添加数据组";
      this.addId = 'addId';
      this.groupId = 0;
    },
    handleList(row){
        this.$router.push({
        path: '/tool/groupDataList?groupId=' + row.id,
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.titleFrom = "修改数据组";
      this.groupId = row.id;
      this.$refs.groupfroms.fromData(row.id);
      this.$refs.groupfroms.modals = true;
      this.addId = '';
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除组合数据编号为"' + ids + '"的数据项？').then(function () {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mapi/system/group/export', {
        ...this.queryParams
      }, `group_${new Date().getTime()}.xlsx`)
    },
    checkClose(done) {
      this.$confirm('是否关闭表单，关闭后数据将丢失？').then(function () {
        done()
      }).then(() => { }).catch(() => { });
    },

  }
};
</script>
