<template>
  <div id="main" class="h-all">
    <!-- 页面基本布局 -->
    <el-container class="h-all">
      <!-- 页面顶部，存放按钮 -->
      <el-header height="auto">
        <div class="header-button-box">
          <el-button size='mini' icon="el-icon-circle-plus-outline" type="primary" @click="createServiceTypeInit"
                     v-if="createAndCopyCommand.visible">
            {{$t("common.createBtn")}}
          </el-button>
          <el-button size='mini' icon="el-icon-edit" type="primary" @click="updateServiceTypeInit"
                     v-if="updateServiceTypeCommand.visible">
            {{$t("common.updateBtn")}}
          </el-button>
          <el-button size='mini' icon="el-icon-delete" type="primary" @click="deleteServiceTypeInit"
                     v-if="deleteServiceTypeCommand.visible">
            {{$t("common.deleteBtn")}}
          </el-button>
        </div>
        <!-- 条件查询 -->
        <div class="header-search">
          <div class="search-input-box">
            <label>{{ $t('ServiceTypeConfigurationManagement.ServiceTypeID') }}：</label>
            <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="queryServiceTypeIDInput" clearable></el-input>
          </div>
          <div class="search-input-box">
            <label>{{ $t('ServiceTypeConfigurationManagement.ServiceTypeName')}}：</label>
            <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="queryServiceTypeNameInput" clearable></el-input>
          </div>
          <div class="search-input-box">
            <el-button icon="el-icon-search" type="primary" size='mini' @click="queryServiceTypeByCondition"
                       v-loading.fullscreen.lock="fullscreenLoading">{{ $t("common.queryBtn")}}
            </el-button>
          </div>
        </div>
      </el-header>
      <!-- Table数据表格显示 -->
      <el-main class="adapt-height-box">
        <el-table
          :empty-text="$t('common.noData')"
          header-row-class-name="table-header"
          cell-class-name="table-cell"
          class="adapt-height"
          height="auto"
          border
          tooltip-effect="dark"
          ref="singleTable"
          :data="serviceTypeList"
          stripe
          @current-change="handleCurrentChange"
          @selection-change="handleSelectionChange">
          <!-- 多选按钮 -->
          <el-table-column
            type="selection"
            width="28">
          </el-table-column>

          <el-table-column
            prop="serviceTypeID"
            :label="$t('ServiceTypeConfigurationManagement.ServiceTypeID')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="serviceTypeName"
            :label="$t('ServiceTypeConfigurationManagement.ServiceTypeName')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <!--<el-table-column-->
          <!--prop="delFlag"-->
          <!--:label="$t('ServiceTypeConfigurationManagement.delFlag')"-->
          <!--:show-overflow-tooltip="true">-->
          <!--</el-table-column>-->
          <!--<el-table-column-->
          <!--prop="modifyTime"-->
          <!--:label="$t('ServiceTypeConfigurationManagement.modifyTime')"-->
          <!--:show-overflow-tooltip="true">-->
          <!--</el-table-column>-->
        </el-table>
      </el-main>
    </el-container>

    <CommandExecuteContext v-bind:commands="commandContext"></CommandExecuteContext>


  </div>
</template>

<script>
  import ServiceTypeService from '@/domain/services/Business/ServiceTypeService.js'
  import CommandExecuteContext from '@/commands/CommandExecuteContext'
  import ServiceTypeCommands from "@/commands/ServiceTypeCommands"
  import HelperUtil from "../../utils/HelperUtil";


  export default {
    name: 'ServiceTypeConfigurationManagement',
    data: function () {   //存放 数据
      return {

        //命令
        commandContext: {},

        //为了创建复制初始化传参
        createAndCopyInitList: [],
        createAndCopyTitle: '',
        //修改初始化传参
        updateInitList: [],

        //
        updateTitle: '',
        //删除相关
        deleteList: [],
        //条件查询输入框
//        queryServiceTypeID: null,
//        queryServiceTypeName: null,
        queryServiceTypeNameInput: '',
        queryServiceTypeIDInput: '',

        id: 1,

        formLabelWidth: '120px',

        //双向绑定弹出窗口input域
        form: {
          serviceTypeID: null,
          serviceTypeName: null,
          delFlag: null,
          modifyTime: null,
        },
        //业务类型列表
        serviceTypeList: [
//          {
//          "serviceTypeID": "3",
//          "serviceTypeName": "3",
//          "delFlag": 0,
//          "modifyTime": "20190416.185141.409",
//
//        },
//          {
//            "serviceTypeID": "1",
//            "serviceTypeName": "222",
//            "delFlag": 0,
//            "modifyTime": "20190416.185141.409"
//          }
//

        ],
//      {serviceTypeID:1}
        //保存当前行数据
        currentRow: null,
        //保存多选框数据
        multipleSelection: [],
        // 数据加载过程中，覆盖界面
        fullscreenLoading: false,
      }
    },

    components: {
      CommandExecuteContext,
    },

    computed: {
      openTab() {
        return this.$store.state.openTab;
      },
      activeIndex: {
        get() {
          return this.$store.state.activeIndex;
        },
        set(val) {
          this.$store.commit('set_active_index', val);
        }
      },
      //命令
      createAndCopyCommand() {
        return ServiceTypeCommands.createAndCopyServiceTypeCommand(this.createAndCopyTitle, this.createAndCopyInitList, this.commandContext)
      },
      deleteServiceTypeCommand() {
        return ServiceTypeCommands.deleteServiceTypeCommand(this.deleteList, this.commandContext)
      },
      updateServiceTypeCommand() {
        return ServiceTypeCommands.updateServiceTypeCommand(this.commandContext,this.updateInitList)
      },
    },

    methods: {

      /**
       * @Description  ：单选一行，将所选进行保存
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/13
       */
      handleCurrentChange(val) {
        this.currentRow = val;
      },

      /**
       * @Description  ：多选框改变时，将所选进行保存
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/13
       */
      handleSelectionChange(val) {
        this.multipleSelection = val
      },

      /**
       * @Description  ：获取全部业务类型初始数据
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/13
       */
      getAllServiceTypeInit: function () {
        console.log('获取全业务类型信息：')
        let _this = this
        ServiceTypeService.getAllServiceTypeCollection().then(result => {
          //成功
          _this.serviceTypeList = Object.values(result)
        }).catch(err => {
          //失败
        })
      },

      /**
       * @Description  ：增删改后，更新表格
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/13
       */
      getAllServiceType: function (vue) {
        console.log('获取全业务类型信息：')

        ServiceTypeService.getAllServiceTypeCollection().then(result => {
          //成功
          vue.serviceTypeList = Object.values(result)
        }).catch(err => {
          //失败
        })
      },

      /**
       * @Description  ：创建业务类型初始化
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/12
       */
      createServiceTypeInit: function () {

        var idList = new Array();
        for (var i = 0; i < this.serviceTypeList.length; i++) {
          idList[i] = parseInt(this.serviceTypeList[i]['serviceTypeID']);
        }

        var tempid = 0
        for (var j = 0; j < idList.length; j++) {

          console.log(idList[j])
          if (idList[j] > tempid) {
            tempid = idList[j]
          }
        }

        this.createAndCopyTitle = this.$t('ServiceTypeConfigurationManagement.creationOfServiceTypeInformation');
        this.createAndCopyInitList = {
          "serviceTypeID": tempid + 1,
          "serviceTypeName": "",
          "delFlag": null,
          "modifyTime": ""

        };
        this.createAndCopyCommand.execute();

      },


      /**
       * @Description  ：修改业务类型初始化
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/12
       */
      updateServiceTypeInit: function () {
        const _this = this;
        if (_this.multipleSelection.length === 0) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONE_RECORD_TO_MODIFY))

        } else if (_this.multipleSelection.length > 1) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_RECORD_TO_MODIFY))
        } else {
          _this.currentRow = _this.multipleSelection[0];
          _this.updateTitle = '业务类型信息的修改';
          _this.updateInitList = {
            "serviceTypeKey": _this.currentRow.serviceTypeKey,
            "serviceTypeID": _this.currentRow.serviceTypeID,
            "serviceTypeName": _this.currentRow.serviceTypeName,
            "delFlag": "",
            "modifyTime": ""
          };
//          console.log("父组传的")
//          console.log(_this.updateInitList )
          _this.updateServiceTypeCommand.execute();

        }
      },


      /**
       * @Description  ：删除业务类型初始化（支持批量？）
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/12
       */
      deleteServiceTypeInit: function () {
        const _this = this;
        if (_this.multipleSelection.length === 0) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONE_RECORD_TO_DELETE))

        } else if (_this.multipleSelection.length > 1) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_RECORD_TO_DELETE))
        } else {
          this.deleteList = []
          for (var i = 0; i < this.multipleSelection.length; i++) {
            this.deleteList.push(this.multipleSelection[i]['serviceTypeKey']);
          }
          this.deleteServiceTypeCommand.execute()
        }
      },


      /**
       * @Description  ：条件查询（只根据业务类型名称？）
       * @author       : yzj
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/3/12
       */
      queryServiceTypeByCondition: function () {
        const _this = this;

        console.log('业务类型按条件查询：')
        if ((_this.queryServiceTypeNameInput == null || _this.queryServiceTypeNameInput == '') && (_this.queryServiceTypeIDInput == null || _this.queryServiceTypeIDInput == '')) {
          //所有查询条件为空点查询--》重新刷新表格
          _this.$options.methods.getAllServiceType(_this);

          return;
        }
        var selectList = new Array();
        if (_this.queryServiceTypeNameInput != null && _this.queryServiceTypeNameInput != '') {
          selectList.push({
            quaryAttribute: 'serviceTypeName',
            compareSymbol: "like",
            compareValue: _this.queryServiceTypeNameInput,
          })
        }
        if (_this.queryServiceTypeIDInput != null && _this.queryServiceTypeIDInput != '') {
          selectList.push({
            quaryAttribute: 'serviceTypeID',
            compareSymbol: "like",
            compareValue: _this.queryServiceTypeIDInput,
          })
        }
//        按条件查询
        ServiceTypeService.conditionQueryServiceType(selectList).then(result => {

          //成功
          this.serviceTypeList = result
        }).catch(err => {
          //失败
        })
      },


    }
    ,
    watch: {
      commandContext(newVal, oldVal) {
        if (JSON.stringify(newVal) === '{}') {
          this.queryServiceTypeNameInput = ''
          this.queryServiceTypeIDInput = ''
          this.getAllServiceType(this)
          this.$refs.singleTable.clearSelection();
        }
        deep: true
      }
    },


    //初始化函数，加载数据
    mounted: function () {
      console.log("初始化加载数据")
      this.getAllServiceTypeInit()
    },

  }
</script>

<style scoped>
</style>
