<!--计量场景登记管理-->
<template>
  <div class="systemBase">
    <div>
        <div style="margin-top: 10px;  border-bottom: 1px solid #d5d5d5;">
             <el-form ref="searchData" :inline="true" :model="search">
                  <el-form-item>
                      <div style="font-size: 16px; padding: 0px 20px;">计量场景登记管理
                      </div>
                  </el-form-item>
                  <el-form-item prop="valueFlag" label="状态">
                    <el-select v-model="search.valueFlag" filterable collapse-tags size="small" style="width:120px">
                        <el-option v-for="(item,index) in valueFlagItem" :key="index" :label="item.valueFlag"
                         :value="item.valueFlag">
                        </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label-width="20px">
                  <el-button icon="el-icon-search" type="primary" size="mini" @click="searchDatas" v-loading.fullscreen.lock="operateLoading" v-privilege="activeMenu + 'SEARCH'">查询</el-button>
                  </el-form-item>
                  <el-form-item label-width="20px">
                  <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-form-item>
                  <el-form-item label-width="20px">
                  <el-button icon="el-icon-edit" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
                  @click="modifyData" v-privilege="activeMenu + 'UPDATE'">修改</el-button>
                  </el-form-item>
                   <el-form-item label-width="20px">
                  <el-button size="mini" type="primary" icon="el-icon-check" v-loading.fullscreen.lock="operateLoading"
                   @click="recoveryData" v-privilege="activeMenu + 'RECOVERY'">启用</el-button>
                  </el-form-item>
                   <el-form-item label-width="20px">
                   <el-button size="mini" type="danger" plain icon="el-icon-remove-outline" v-loading.fullscreen.lock="operateLoading"
                   @click="discardData" v-privilege="activeMenu + 'ABOLISH'">废除</el-button>
                  </el-form-item>
              </el-form>
       </div>
      <div class="common-table-div">
        <el-table ref="dataTable"  
          @row-click="rowChange" 
          border 
          :height="tableHeight" 
          highlight-current-row
          @selection-change="handleSelectionChange"
          :row-class-name="tableRowClassName" 
          :data="table.list" 
          :exchangeFilterMap="{
          'valueFlag':{'0':'有效','1':'无效'}}"
          :icore-filter-flag="icoreFilterFlag">
          <el-table-column type="index" label="序号" fixed="left" align="center"></el-table-column>
          <el-table-column type="selection" width="55" fixed="left" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sceneCode" label="场景编码" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="sceneName" label="场景名称" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="sceneNote" label="场景描述" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="sceneFunction" label="包含函数" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" prop="sceneUrl" min-width="200px" label="调用路径" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="memo" label="备注" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" prop="valueFlag" min-width="100px" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.valueFlag == 0">有效</span>
              <span v-else-if="scope.row.valueFlag == 1">无效</span>
            </template>
          </el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" 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="100px" 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="editDiv" class="commom-edit-div" style="border-top: 1px solid #d5d5d5; margin-top: 10px">
        <el-form ref="baseData" :inline="true" :rules="ruleValidateTable" :model="baseData">
           <el-form-item prop="sceneName" label="场景名称">
            <el-input v-model="baseData.sceneName" clearable size="small" style="width:120px" placeholder="请输入"></el-input>
           </el-form-item>
            <el-form-item prop="sceneNote" label="场景描述">
            <el-input v-model="baseData.sceneNote" clearable size="small" style="width:150px" placeholder="请输入"></el-input>
           </el-form-item>
           <el-form-item prop="sceneFunction" label="包含函数">
            <el-input v-model="baseData.sceneFunction" clearable size="small" style="width:150px" placeholder="请输入"></el-input>
           </el-form-item>
            <el-form-item prop="sceneUrl" label="调用路径">
            <el-input v-model="baseData.sceneUrl" clearable size="small" style="width:150px" placeholder="请输入"></el-input>
           </el-form-item>
           <el-form-item prop="memo" label="备注">
            <el-input v-model="baseData.memo"  clearable size="small" style="width:150px" placeholder="请输入"></el-input>
           </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import {formatDate} from '@/utils/util.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        valueFlagItem: [
            {valueFlag:'所有'},
            {valueFlag:'有效'},
            {valueFlag:'无效'}
        ],
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        baseData: {
            sceneName: '',
            sceneNote: '',
            sceneFunction: '',
            sceneUrl: '',
            memo: '',
        },
        search: {
            valueFlag: '所有'
        },
        multipleSelection: [],
        tableHeight: 0,
        ruleValidateTable: {
           sceneName: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
           sceneNote: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
           sceneUrl: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
        },
        activeMenu: ''
      }
    },
    created(){
      this.tableHeight = (window.innerHeight - 200) > 400 ? window.innerHeight - 200 : 400;
      this.activeMenu = window.top.localStorage.getItem('activeMenu')
    },
    mounted () {
      this.searchData();
    },
    methods: {
     getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      return targetRow ? targetRow[nameKey] : '';
    },
    refresh () {
      this.baseData = {
            sceneName: '',
            sceneNote: '',
            sceneFunction: '',
            sceneUrl: '',
            memo: '',
      }
      this.$refs['baseData'].resetFields()
    },
    addData () {
       this.$refs['baseData'].validate((valid) => {
          if (valid) {
             let baseDataAdd = {}
             baseDataAdd.sceneName = this.baseData.sceneName
             baseDataAdd.sceneNote = this.baseData.sceneNote
             baseDataAdd.sceneFunction = this.baseData.sceneFunction
             baseDataAdd.sceneUrl = this.baseData.sceneUrl
             baseDataAdd.memo = this.baseData.memo
             this.axios.post('pass/systemBase/v1/meterworksceneregisters/add', baseDataAdd, {individualType: 'json'}).then(res=>{
               if (res.succeed) {
                  this.$message.success('新增成功');
                  this.searchData ();
                  this.refresh();
               }  else {
                this.$message.error(res.message)
               }
               }).catch(e => {
                this.$message.error(e.message)
              })
          }
       })
    },
    modifyData () {
       this.$refs['baseData'].validate((valid) => {
          if (valid) {
             this.axios.post('pass/systemBase/v1/meterworksceneregisters/update', this.baseData, {individualType: 'json'}).then(res=>{
               if (res.succeed) {
                  this.$message.success('修改成功');
                  this.searchData ();
                  this.refresh();
               }  else {
                this.$message.error(res.message)
               }
               }).catch(e => {
                this.$message.error(e.message)
              })
          }
       })
    },
    discardData () {
    if (this.multipleSelection.length < 1) {
        this.$message.warning('请选择您要废除的数据！');
        return;
      }
      this.axios.post('pass/systemBase/v1/meterworksceneregisters/abolish' , this.multipleSelection, {individualType: 'json'}).then(res => {
         if (res.succeed) {
           this.$message.success('废除成功');
           this.searchData ();
        }  else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
    },
    recoveryData () {
    if (this.multipleSelection.length < 1) {
        this.$message.warning('请选择您要启用的数据！');
        return;
      }
      this.axios.post('pass/systemBase/v1/meterworksceneregisters/recover', this.multipleSelection, {individualType: 'json'}).then(res => {
         if (res.succeed) {
           this.$message.success('启用成功');
           this.searchData ();
        }  else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
    },
    rowChange (row, column, event) {
       this.baseData = Object.assign ({}, row);
    },
      handleSelectionChange (val) {
        this.multipleSelection = val;
      },
      pageSizeChange (size) {
        this.page.pageSize = size;
        this.searchData();
      },
      pageCurrentChange (index) {
        this.page.pageNum = index;
        this.searchData();
      },
      searchDatas () {
          this.page.pageNum = 1;
          this.searchData()
      },
      searchData () {
        this.search.valueFlag1 = this.search.valueFlag==='无效' ? '1':this.search.valueFlag==='有效'?'0':''
        this.dataLoading = true;
        this.axios.get('pass/systemBase/v1/meterworksceneregisters/page' + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize + '&valueFlag=' + this.search.valueFlag1)
          .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 === '1') {
          return 'error-row2';
        }
        return '';
      },
    }
  }
</script>
<style scoped>
</style>