<template>
  <div class="bbsl-common-page"
       @click="commonPageClick">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam"
               :inline="true">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="巡检对象名称">
              <el-input size="mini"
                        style="width: 100%"
                        placeholder="请输入巡检结构名称"
                        v-model="queryParam.params.position"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary"
                         icon="el-icon-search"
                         size="mini"
                         @click="onClickSearch">搜索</el-button>
              <el-button icon="el-icon-refresh"
                         size="mini"
                         @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="bbsl-table-content"
         ref="contentPanel">
      <div class="bbsl-btn-content"
           ref="btnPanel">
        <el-button size="mini"
                   icon="el-icon-plus"
                   type="primary"
                   @click="openFormDiao">新增</el-button>
        <el-button :disabled="!multipleSelection.length"
                   size="mini"
                   icon="el-icon-delete"
                   type="danger"
                   @click="deleteSelect">删除</el-button>
        <el-button size="mini"
                   icon="el-icon-sort"
                   type="success"
                   @click="savePropertyf">保存排序号</el-button>
      </div>
      <el-table stripe
                size="mini"
                :height="tableMaxHeight"
                @row-dblclick="handleDbClick"
                :style="{width: '100%'}"
                ref="multipleSelection"
                v-loading="loading"
                border
                :data="tableData"
                :row-key="getRowKey"
                @selection-change="handleSelectionChange"
                :header-cell-style="{'text-align': 'center'}"
                :cell-style="{'text-align': 'center'}">
        <el-table-column type="selection"
                         width="55"
                         :reserve-selection="true"></el-table-column>
        <el-table-column fixed
                         sortable
                         prop="structure"
                         label="巡检结构名称"
                         show-overflow-tooltip></el-table-column>
        <el-table-column fixed
                         sortable
                         prop="position"
                         label="巡检对象名称"
                         show-overflow-tooltip></el-table-column>
        <el-table-column fixed
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)"
                       type="text"
                       size="small">常用语配置</el-button>
          </template>
        </el-table-column>

        <el-table-column width="120"
                         fixed
                         sortable
                         prop="sn"
                         label="排序号"
                         show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input type="number" size="small"
                      min="1"
                      @input="insort(scope.row)"
                      v-model="scope.row.sn"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination ref="paginationPanel"
                     align="right"
                     class="bbsl-pagination"
                     background
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryParam.pageBean.page"
                     :page-sizes="[20, 30, 40, 50]"
                     :page-size="queryParam.pageBean.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"></el-pagination>
    </div>
    <form-dialog v-if="showFormDia"
                 @closeFormDiao="closeFormDiao"
                 @refList="getDateList"
                 :showFormDia="showFormDia"
                 :title="title"
                 :pId="pId"></form-dialog>
    <!--常用配置弹框-->
    <commonDialog v-if="commonShow"
                  @closeFormDiao="closeCommonFormDiao"
                  :CommonFormDia="commonShow"
                  :pId="pId"></commonDialog>
  </div>
</template>
<script>
import FormDialog from './components/dsmlnsForm-dialog.vue'
import commonDialog from './components/commonForm-dialog.vue'
import dsmInsPosition from '@/api/resIns/dsmInsPosition.js'
import req from '@/request.js'
import { TableMixin } from '@/mixins/tableMixin'
export default {
  name: 'disPlacement',
  components: {
    commonDialog,
    FormDialog
  },
  mixins: [TableMixin],
  data () {
    return {
      loading: false,
      multipleSelection: [], // 复选框
      stcds: [],
      gradeCodes: [],
      queryParam: {
        pageBean: {
          pageSize: 20,
          page: 1
        },
        params: {
          structure: null, //巡检结构名称
          position: null, //巡检对象名称
          description: null //巡检对象要求描述,
        }
      },
      sortList: {},
      total: 0,
      title: '',
      showFormDia: false,
      commonShow: false, //常用配置弹框
      tableData: [],
      pId: ''
    }
  },
  created () {
    this.getDateList()
    this.getList()
    this.getSysDict()
  },
  methods: {
    getList () {
      dsmInsPosition.getgqList({}, res => {
        this.stcds = res.value
        this.getDateList()
      })
    },
    //保存排序
    savePropertyf () {
      dsmInsPosition.saveProperty(this.sortList, res => {

        this.getDateList()
      })
    },
    changeCode (val) {
      if (val) {
        this.queryParam.params.gradeCode = val
      } else {
        this.queryParam.params.gradeCode = ''
      }
    },
    getDateList () {
      this.loading = true
      this.tableData = []
      dsmInsPosition.ListGet(this.queryParam, res => {
        console.log(res.value.rows);
        res.value.rows.sort((a, b) => {
          return a.sn - b.sn
        })
        this.total = res.value.total
        this.tableData = res.value.rows
        this.loading = false
      })
    },
    insort (val) {
      let id = val.id
      let value = val.sn
      this.sortList[id] = value
      console.log(this.sortList);

    },
    openFormDiao () {
      this.title = '新增'
      this.showFormDia = true
    },
    openCommonFormDiao () {
      this.title = '新增'
      this.showFormDia = true
    },
    closeFormDiao () {
      this.showFormDia = false
      this.getDateList()
    },
    closeCommonFormDiao () {
      this.commonShow = false
      // this.getDateList()
    },
    handleSizeChange (val) {
      this.queryParam.pageBean.pageSize = val
      this.getDateList()
    },
    handleCurrentChange (val) {
      this.queryParam.pageBean.page = val
      this.getDateList()
    },
    handleUpdate (row) {
      this.title = '修改'
      this.pId = row.id
      this.showFormDia = true
    },
    handleSelectionChange (val) {
      this.multipleSelection = val.map(item => {
        return item.id
      })
    },
    // // 指定一个key标识这一行的数据
    getRowKey (row) {
      return row.id
    },
    // // 批量删除
    deleteSelect () {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      })
        .then(() => {
          dsmInsPosition
            .remove(this.multipleSelection, res => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000
                })
                this.$refs.multipleSelection.clearSelection()
                this.queryParam.pageBean.page = 1
                this.getDateList()
              }
            })
            .catch(res => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80
              })
              this.getDateList()
            })
        })
        .catch(() => { })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      })
        .then(() => {
          dsmInsPosition
            .remove(row.id, res => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000
                  // offset: 80
                })
                this.queryParam.pageBean.page = 1
                this.getDateList()
              }
            })
            .catch(res => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80
              })
              this.getDateList()
            })
        })
        .catch(() => { })
    },
    //常用语配置
    handleClick (val) {
      this.commonShow = true
      this.pId = val.id
    },
    // // 点击查询
    onClickSearch () {
      this.queryParam.pageBean.page = 1
      this.getDateList()
    },
    // 重置
    resetQuery () {
      this.queryParam.params = {}
      this.onClickSearch()
    },
    //获取字典数据
    getSysDict () {
      let portal = window.context.portal
      return new Promise((resolve, reject) => {
        req.post(portal + '/sys/sysDictionary/v1/queryAllDictItems').then(
          response => { },
          error => {
            reject(error)
          }
        )
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.btn-content {
  display: flex;
}
.el-select-dropdown__item span {
  color: rgb(51, 51, 51) !important;
}
.el-select-dropdown__item {
  color: #9a9a9a !important;
}
.el-select-dropdown__item.hover,
.el-select-dropdown__item:hover {
  background: #f5f7fa;
}
.btn-content :first-child {
  margin: 0 0 0 0 !important;
}
.el-switch__label--left {
  position: relative;
  left: 45px;
  color: #fff;
  z-index: -1111;
}
.el-switch__core {
  width: 50px !important;
}
.el-switch__label--right {
  position: relative;
  right: 46px;
  color: #fff;
  z-index: -1111;
}
.el-switch__label--right.is-active {
  z-index: 1111;
  color: #fff !important;
}
.el-switch__label--left.is-active {
  z-index: 1111;
  color: #9c9c9c !important;
}
.sampling {
  ::v-deep .el-dialog__body {
    padding: 30px 20px 0 20px;
  }
}
::-webkit-scrollbar {
  background: #fff !important;
}
</style>
