<!--计量函数综合管理-->
<template>
  <div class="systemBase common-container-deriction">
    <div class="common-title-div">计量函数综合管理
      <div class="common-operation-button-div">
        <el-button icon="el-icon-circle-plus-outline" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
           @click="addData" v-privilege="activeMenu + 'ADD'">新增</el-button>
          <el-button icon="el-icon-edit" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
          @click="modifyData" v-privilege="activeMenu + 'UPDATE'">修改</el-button>
          <el-button size="mini" type="danger" plain icon="el-icon-remove-outline" v-loading.fullscreen.lock="operateLoading"
          @click="abolishData" v-privilege="activeMenu + 'ABOLISH'">废除</el-button>
        <el-button size="mini" type="primary" icon="el-icon-check" v-loading.fullscreen.lock="operateLoading"
          @click="recoveryData" v-privilege="activeMenu + 'RECOVERY'">启用</el-button>
      </div>
    </div>
    <div class="common-table-div">
      <el-table v-loading="dataLoading" @row-click="rowChange" ref="dataTable" :row-class-name="tableRowClassName" highlight-current-row
        border :height="tableHeight" @selection-change="handleSelectionChange" :data="table.list"
        :exchangeFilterMap="{'valueFlag':{'1':'无效','0':'有效'}}"
        :icore-filter-flag="icoreFilterFlag">
        <el-table-column type="index" label="序号" fixed="left" align="center"></el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionNo" label="函数编号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionName" label="函数名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionParam" label="函数参数" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionOutput" label="函数输出" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionBelong" label="函数归属" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="functionEffect" label="函数作用" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" prop="valueFlag" min-width="80px" label="状态" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.valueFlag == 1">无效</span>
              <span v-else-if="scope.row.valueFlag == 0">有效</span>
          </template>
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="createManName" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="updateManName" label="修改人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[100, 500, 1000]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage">
        </el-pagination>
    </div>
    <!-- <div ref="editTitle" class="common-title-div" style="border-top: 1px solid #d5d5d5;">编辑区
    </div> -->
    <div ref="editDiv" class="commom-edit-div" style="border-top: 1px solid #d5d5d5; margin-top: 10px">
      <el-form ref="baseData" label-width="80px" :rules="ruleValidate" :inline="true" :model="baseData">
         <!-- <el-form-item prop="functionNo" label="函数编号">
           <el-input v-model="baseData.functionNo" clearable readonly disabled size="small" style="width:120px"></el-input>
         </el-form-item> -->
         <el-form-item prop="functionName" label="函数名称">
           <el-input v-model="baseData.functionName" clearable size="small" placeholder="请输入" style="width:120px"></el-input>
         </el-form-item>
         <el-form-item prop="functionParam" label="函数参数">
           <el-input v-model="baseData.functionParam" clearable size="small" placeholder="请输入" style="width:120px"></el-input>
         </el-form-item>
         <el-form-item prop="functionOutput" label="函数输出">
           <el-input v-model="baseData.functionOutput" clearable size="small" placeholder="请输入" style="width:120px"></el-input>
         </el-form-item>
         <el-form-item prop="functionBelong" label="函数归属">
           <el-input v-model="baseData.functionBelong" clearable size="small" placeholder="请输入" style="width:120px"></el-input>
         </el-form-item>
         <el-form-item prop="functionEffect" label="函数作用">
           <el-input v-model="baseData.functionEffect" clearable size="small" placeholder="请输入" style="width:120px"></el-input>
         </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
   import {
    meterbasefunctions
  } from '../../../utils/api.js';
  import {
    Delete,
    Recovery,
    add,
    modify,
    discard
  } from '../../../utils/httpRequest.js';
  import pinyinUtils from '../../../utils/pinyin/pinyinUtils.js'
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        baseData: {
          functionNo: '',
          functionName: '',
          functionParam: '',
          functionOutput: '',
          functionBelong: '',
          functionEffect: ''
        },
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        multipleSelection: [],
        tableHeight: 0,
        ruleValidate: {
          // functionNo: [{
          //   required: true,
          //   message: '函数编号不能为空',
          //   trigger: 'change'
          // }],
          functionName: [{
            required: true,
            message: '函数名称不能为空',
            trigger: 'change'
          }],
          functionParam: [{
            required: true,
            message: '函数参数不能为空',
            trigger: 'change'
          }],
          functionOutput: [{
            required: true,
            message: '函数输出不能为空',
            trigger: 'change'
          }],
          functionBelong: [{
            required: true,
            message: '函数归属不能为空',
            trigger: 'change'
          }],
          functionEffect: [{
            required: true,
            message: '函数作用不能为空',
            trigger: 'change'
          }]
        },
        activeMenu: ''
      }
    },
    created () {
      this.tableHeight = (window.innerHeight - 220) > 400 ? window.innerHeight - 220 : 400
      this.activeMenu = window.top.localStorage.getItem('activeMenu')
    },
    mounted () {
      // this.tableHeight = this.getTableHeight(this.$refs['dataTable'].$el) - 80;
      this.getDataList();
    },
    methods: {
      rowChange (row, column, event) {
        this.baseData =  Object.assign ({}, row);
      },
      addData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            _this.operateLoading = true;
            let baseDataAdd = {};
            // baseDataAdd.functionNo = _this.baseData.functionNo
            baseDataAdd.functionName = _this.baseData.functionName
            baseDataAdd.functionParam = _this.baseData.functionParam
            baseDataAdd.functionOutput = _this.baseData.functionOutput
            baseDataAdd.functionBelong = _this.baseData.functionBelong
            baseDataAdd.functionEffect = _this.baseData.functionEffect
            add(meterbasefunctions, baseDataAdd, (e) => {
              _this.operateLoading = false;
              _this.getDataList();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            _this.operateLoading = true;
            modify(meterbasefunctions + _this.baseData.functionNo + '/', _this.baseData, (e) => {
              _this.operateLoading = false;
              _this.getDataList();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      recoveryData () {
        let _this = this;
        _this.operateLoading = true;
        Recovery(meterbasefunctions + 'Recovery', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
      // deleteData () {
      //   this.$confirm('确定删除选中的计量函数信息？', {
      //       confirmButtonText: '确定',
      //       cancelButtonText: '取消'
      //     })
      //     .then(() => {
      //       this.operateLoading = true;
      //       Delete(meterbasefunctions, this.multipleSelection, (e) => {
      //         this.operateLoading = false;
      //         if (!e) {
      //           this.getDataList();
      //         }
      //       })
      //     })
      //     .catch(action => {
      //       this.$message({
      //         type: 'info',
      //         message: action === 'cancel' ? '放弃保存并离开页面' : '停留在当前页面'
      //       })
      //     });
      // },
       abolishData () {
        let _this = this;
        _this.operateLoading = true;
        discard(meterbasefunctions + 'abolish', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
      handleSelectionChange (val) {
        this.multipleSelection = val;
      },
      getTableHeight (dom) {
        return (window.innerHeight - dom.offsetTop);
      },
      pageSizeChange (size) {
        this.page.pageSize = size;
        this.getDataList();
      },

      pageCurrentChange (index) {
        this.page.pageNum = index;
        this.getDataList();
      },
      getDataList () {
        this.dataLoading = true;
        this.axios.get(meterbasefunctions + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize)
          .then(res => {
            this.dataLoading = false;
            this.table.list = res.data.list;
            this.page.totalPage = res.data.total;
          }).catch(e => {
            this.dataLoading = false;
          });
      },
      tableRowClassName ({
        row,
        rowIndex
      }) {
        if (row.valueFlag !== '0') {
          return 'error-row2';
        }
        return '';
      }
      // myBlur() {
      //   if (this.baseData.customerSupplierName) {
      //     this.baseData.mnemonicCode = pinyinUtils.initial(this.baseData.customerSupplierName);
      //   }
      // }
    }
  }
</script>

<style scoped>
  .el-select {
    width: 100%;
  }
</style>
