<template>
  <div class="system-paramMagager">
    <!-- 头部细节部分 -->
      <div class="title">
        <el-row>
          <el-col :span="24">
            <div class="top">
              <el-input  placeholder="参数名称" class="input-demo" v-model="paramName"></el-input>
              <el-input  placeholder="参数值" class="input-demo" v-model="paramValue"></el-input>
              <el-input  placeholder="备注信息" class="input-demo" v-model="paramNote"></el-input>
               <el-button type="success" style="margin-left: 1%;" @click="queryLook">查询</el-button>
                <el-button type="success" style="margin-left: 1%;" @click="addParamList = true">添加</el-button>
                <el-button type="primary" style="margin-left: 1%;" @click="exportExcel">导出报表</el-button>

            </div>
          </el-col>
        </el-row>
      </div>
    <!-- 头部细节部分结束 -->
    <!-- 表格部分 -->
      <template>
        <el-table
         class="table"
          v-loading="loading"
          ref="multipleTable"
          :data="usertable"
           height="370px"
          border
          tooltip-effect="dark">
          <el-table-column
            label="参数名称"
            prop="paramName"
            show-overflow-tooltip>
          </el-table-column>
           <el-table-column
            prop="paramValue"
            label="参数值"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="paramNote"
            label="备注信息"
            show-overflow-tooltip>
          </el-table-column>
            <el-table-column
            label="操作"
            width="280">
            <template slot-scope="scope" show-overflow-tooltip>
              <el-link style="cursor:pointer;color: blue;" @click="openChange(scope.row)">修改</el-link>
            </template>
          </el-table-column>
        </el-table>
      </template>

    <!-- 分页 -->
      <div class="block">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 8, 10, 20, 50 ,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
      </div>



       <!-- 新增参数 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="addParamList"
        style="font-size: 0px;">
        <div class="tou">参数--添加</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :rules="rules"
          ref="ruleForm"
          :model="formParamList">
          <el-form-item label="参数名称" prop="paramName">
            <el-input v-model="formParamList.paramName" auto-complete="off" placeholder="请输入参数名称"></el-input>
          </el-form-item>

          <el-form-item label="参数值" prop="paramValue">
            <el-input v-model="formParamList.paramValue" auto-complete="off" placeholder="请输入参数值"></el-input>
          </el-form-item>

          <el-form-item label="备注信息" prop="paramNote">
            <el-input v-model="formParamList.paramNote" auto-complete="off" placeholder="请输入备注信息"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addParamList = false" type="info">取 消</el-button>
          <el-button type="primary" @click="addData('ruleForm')">新 增</el-button>
        </div>
      </el-dialog>


         <!-- 修改参数 -->
      <el-dialog
        @close="handleEditDialogClose"
        :visible.sync="changeParam"
        style="font-size: 0px;">
        <div class="tou">参数--修改</div>
        <div class="line"></div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formParamList">

          <el-form-item label="参数名称">
            <el-input v-model="formParamList.paramName" auto-complete="off" placeholder="请输入参数名称"></el-input>
          </el-form-item>
          <el-form-item label="参数值">
            <el-input v-model="formParamList.paramValue" auto-complete="off" placeholder="请输入参数值"></el-input>
          </el-form-item>
          <el-form-item label="备注信息">
            <el-input v-model="formParamList.paramNote" auto-complete="off" placeholder="请输入备注信息"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeParam = false" type="info">取 消</el-button>
          <el-button type="primary" @click="changeData">修 改</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script type="text/javascript">
import axios from 'axios';
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default{
  data() {
    return {
      loading: true,
      rules: {
          paramName: [
            { required: true, message: '请输入参数名称', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          paramValue: [
            { required: true, message: '请输入参数值', trigger: 'blur' },
            { min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur' }
          ],
          paramNote: [
            { required: true, message: '请输入备注信息', trigger: 'blur' },
            { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
          ]
        },
      paramName: '',
      paramValue: '',
      paramNote: '',
      current: 1,
      pagesize: 8,
      formParamList: {
        "paramName":"",
        "paramValue": "",
        "id":"",
        "paramNote": ""
      },
      // 总共有多少条数据
      total: 0,
      addParamList: false,
      changeParam: false,
    }
  },
  created() {
    this.loadData();
  },
  methods: {

    // 列表展示
    async loadData() {
      const formData = new FormData();
      formData.append('current', this.current);
      formData.append('size', this.pagesize);
      const response = await this.$http.post(`param/page`, formData);
      if (response.data.code === 0) {
        this.loading = false;
        this.usertable = response.data.data.records;
        this.total = response.data.data.total;
      }
    },
    //查询
    async queryLook() {
      const formData = new FormData();
      formData.append('current', this.current);
      formData.append('size', this.pagesize);
      formData.append('paramName', this.paramName);
      formData.append('paramValue', this.paramValue);
      formData.append('paramNote', this.paramNote);
      const response = await this.$http.post(`param/page`, formData);
      if (response.data.code === 0) {
        this.loading = false;
        this.usertable = response.data.data.records;
        this.total = response.data.data.total;
      }
    },
    // 新增参数
    addData(formName) {
      this.$refs[formName].validate(async (valid) => {
        if(valid) {
          const response = await this.$http.post(`param`, this.formParamList);
          if(response.data.code === 0) {
            this.loadData();
            this.addParamList = false;
            this.$message({
              type: 'success',
              message: '添加成功'
            });
          }else {
            this.$message({
              type: 'error',
              message: '添加失败'
            });
          }
        }else {
          this.$message.error('请查看是否有选项未填写或填错项');
          return false;
        }
      })
    },
    // 打开修改并赋予信息
    openChange(param) {
      this.changeParam = true;
      this.formParamList.paramName = param.paramName;
      this.formParamList.id = param.id;
      this.formParamList.paramValue = param.paramValue;
      this.formParamList.paramNote = param.paramNote;
    },
    // 修改参数
  async  changeData() {
      const response = await this.$http.post(`param/updateParam`, this.formParamList);
      if(response.data.code === 0) {
        this.loadData();
        this.changeParam = false;
        this.$message({
          type: 'success',
          message: '修改成功'
        });
      }else {
        this.$message({
          type: 'error',
          message: '修改失败'
        });
        }
    },

    // 清空表单数据
    handleEditDialogClose() {
      for (var key in this.formParamList) {
        this.formParamList[key] = '';
      };
      this.current = 1;
      this.pagesize = 8;
    },
    // 分页方法
    handleSizeChange(val) {
      this.pagesize = val;
      if(this.userName !== '' || this.company !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.current = val;
      if(this.userName !== '' || this.company !== '') {
        this.queryLook();
      }else{
        this.loadData();
      };
      console.log(`当前页: ${val}`);
    },
        // 导出表格所用
    exportExcel() {
      // 设置当前日期
      let time = new Date();
      //console.log(time);
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      let name = "用户列表_"+year + "" + month + "" + day;
      // console.log(name)
      /* generate workbook object from table */
      //  .table要导出的是哪一个表格
      var wb = XLSX.utils.table_to_book(document.querySelector(".table"));
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          name + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

  }
};
</script>

<style>
.system-paramMagager {
  border: 1px solid #d9d9d9;
  border-radius: 10px;
}
.system-paramMagager .title {
  font-size: 5px;
  margin-bottom: 20px;
}
.system-paramMagager .top {
  padding-top: 20px;
  padding-left: 20px;
}
.system-paramMagager .text {
  display: inline-block;
  color: #000;
  font-size: 16px ;
  margin-left: 1%;
}
.system-paramMagager .input-demo {
  display: inline-block;
  width: 20%;
  margin-left: 1%;
}
.system-paramMagager .block {
  font-size: 5px;
  text-align: center;
  margin-top: 25px;
  margin-bottom: 25px;
}
.system-paramMagager .el-dialog {
  width: 60%;
}
.system-paramMagager .el-dialog__header, .el-dialog__body {
  padding: 0 20px;
}
.system-paramMagager .tou {
  font-size: 20px;
  height: 30px;
  line-height: 30px;
  padding-top: 15px;
}
.system-paramMagager .line {
  margin-top: 15px;
  margin-bottom: 15px;
  width: 100%;
  height: 2px;
  background-color: #d9d9d9;
}
.system-paramMagager .xinxi {
  text-align: center;
  margin: 15px auto;
  font-size: 18px;
}
</style>
