<template>
  <div class="app-container">
    <div class="app-container">
      <div class="filter-container">
        <el-select
          v-model="getdataListParm.parammaps.pastureName"
          placeholder="牧场"
          style="width:120px"
          class="filter-item"
        >
          <el-option
            v-for="item in findAllPasture"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
        <el-input v-model="getdataListParm.parammaps.empCode" placeholder="用户名" style="width: 140px;" class="filter-item" />
        <el-input v-model="getdataListParm.parammaps.empname" placeholder="姓名" style="width: 140px;" class="filter-item" />
        <el-input v-model="getdataListParm.parammaps.roleName" placeholder="角色" style="width: 140px;" class="filter-item" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
        <el-button v-if="isUserAdd" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="form_add"> 新增 </el-button>
      </div>
      <el-table
        v-loading="listLoading"
        element-loading-text="给我一点时间"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @sort-change="tableSort"
        :max-height="myHeight"
      >
        <el-table-column label="牧场" prop="pastureName"  header-align="center" width="120px" align="center">
        </el-table-column>
        <el-table-column label="用户名称" header-align="center" width="120px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" min-width="120px" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.empname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" min-width="120px" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.deptname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色" min-width="120px" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.roleName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="顺序" sortable prop="sort" min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.sort }}</span>
          </template>
        </el-table-column>
        <el-table-column label="启用" min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" disabled active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="handleEnableChange(scope.$index, scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" header-align="center" align="center" width="330" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button v-if="isUserRole" type="warning" size="mini" @click="handleRole(row)"> 角色 </el-button>
            <el-button v-if="isUserEdit" type="primary" size="mini" @click="form_edit(row)"> 编辑 </el-button>
            <el-button v-if="isUserResetpassword" type="warning" size="mini" style="padding: 7px 5px;" @click="handleResetPassword(row)"> 重置密码 </el-button>
            <el-button v-if="row.status !== '已删' && isUserDelete" size="mini" type="danger" @click="form_delete(row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="get_table_data" />

      <!-- 编辑 -->
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form ref="dataForm" :rules="rules" :model="dataform" label-position="left" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item label="职工名" prop="empid">
            <el-autocomplete ref="empid" v-model="dataform.empname" value-key="empid" class="inline-input" :fetch-suggestions="formNameSearch" placeholder="请输入内容" style="width:80%" @select="handleformNameSelect">
              <template slot-scope="{ item }">
                <div class="name" style="display: inline;">姓名： {{ item.empname }}</div>
                <span class="addr"> 职位：{{ item.position }}</span>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input ref="username" v-model="dataform.username" />
          </el-form-item>
          <el-form-item label="顺序" prop="sort">
            <el-input ref="sort" v-model="dataform.sort" />
          </el-form-item>
          <el-form-item label="启用" prop="enable">
            <el-switch ref="enable" v-model="dataform.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </el-form-item>
           <el-form-item label="记录仪账号" prop="deviceId">
            <el-select v-model="dataform.deviceId" style="width:500px;" placeholder="请选择" @change="change_deviceId">
              <el-option v-for="item in viedoAccountList" :key="item.deviceId" :label="item.deviceId" :value="item.deviceId" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否保养工" prop="keeper">
            <el-switch ref="keeper" v-model="dataform.keeper" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item label="是否维修工" prop="maintenancePerson">
            <el-switch ref="maintenancePerson" v-model="dataform.maintenancePerson" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="changeMaintenanceWorker" />
          </el-form-item>
          <el-form-item label="维修工类型" prop="maintenanceTypeId">
            <el-select v-model="dataform.maintenanceTypeId" :disabled="disabled" style="width:500px;" multiple placeholder="请选择">
              <el-option v-for="item in repairmanTypeList" :key="item.id" :label="item.typeName" :value="item.id" />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="记录仪账号" prop="deviceId">
            <el-select v-model="dataform.deviceId" style="width:500px;" placeholder="请选择" @change="change_deviceId">
              <el-option v-for="item in viedoAccountList" :key="item.deviceId" :label="item.deviceId" :value="item.deviceId" />
            </el-select>
          </el-form-item> -->

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="edit_dialog_save()"> 确认 </el-button>
          <el-button @click="dialogFormVisible = false"> 关闭 </el-button>
        </div>
      </el-dialog>

      <!-- 新增 -->
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisibleAdd" :close-on-click-modal="false">
        <el-form ref="dataForm" :rules="rules" :model="dataform" label-position="left" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item label="职工名" prop="empid">
            <el-autocomplete ref="empid" v-model="dataform.empname" value-key="empid" class="inline-input" :fetch-suggestions="formNameSearch" placeholder="请输入内容" style="width:100%" @select="handleformNameSelect">
              <template slot-scope="{ item }">
                <div class="name" style="display: inline;">姓名： {{ item.empname }}</div>
                <span class="addr"> 职位：{{ item.position }}</span>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input ref="username" v-model="dataform.username" />
          </el-form-item>
          <el-form-item label="角色" prop="rolename">
            <el-select v-model="dataform.selectedRole" style="width:500px;" multiple placeholder="请选择">
              <el-option v-for="item in rolelist" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="启用" prop="enable">
            <el-switch ref="enable" v-model="dataform.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item label="记录仪账号" prop="deviceId">
            <el-select v-model="dataform.deviceId" style="width:500px;" placeholder="请选择" @change="change_deviceId">
              <el-option v-for="item in viedoAccountList" :key="item.deviceId" :label="item.deviceId" :value="item.deviceId" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否保养工" prop="keeper">
            <el-switch ref="keeper" v-model="dataform.keeper" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item label="是否维修工" prop="maintenancePerson">
            <el-switch ref="maintenancePerson" v-model="dataform.maintenancePerson" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="changeMaintenanceWorker" />
          </el-form-item>
          <el-form-item label="维修工类型" prop="maintenanceTypeId">
            <el-select v-model="dataform.maintenanceTypeId" :disabled="disabled" style="width:500px;" multiple placeholder="请选择">
              <el-option v-for="item in repairmanTypeList" :key="item.id" :label="item.typeName" :value="item.id" />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="记录仪账号" prop="deviceId">
            <el-select v-model="dataform.deviceId" style="width:500px;" placeholder="请选择">
              <el-option v-for="item in viedoAccountList" :key="item.deviceId" :label="item.deviceId" :value="item.deviceId" />
            </el-select>
          </el-form-item> -->

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="add_dialog_save()"> 确认 </el-button>
          <el-button @click="dialogFormVisibleAdd = false"> 关闭 </el-button>
        </div>
      </el-dialog>

      <!-- 角色 -->
      <el-dialog :title="textMap['role']" :visible.sync="dialogRoleVisible" :close-on-click-modal="false">
        <el-form :rules="rules" label-position="left" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item label="角色" prop="rolename">
            <el-select v-model="selectedRole" style="width:500px;" multiple placeholder="请选择">
              <el-option v-for="item in rolelist" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="updateRole()"> 确认 </el-button>
          <el-button @click="dialogRoleVisible = false"> 关闭 </el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { isIntegerZero } from '@/utils/validate'
import { PostDataByName, GetDataByName, GetDataByNames, UpdateDataRelation, ExecDataByConfig,checkButtons } from '@/api/common'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
import { sortChange } from '@/utils/index.js'
export default {
  name: 'User',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      findAllPasture: [],
      myHeight:document.documentElement.clientHeight - 85- 200,
      isUserDelete:[],isUserResetpassword:[],isUserEdit:[],isUserRole:[],isUserAdd:[],
      tableKey: 0,
      list: [{ sqlname: '', id: 0, sqlstr: '' }],
      total: 0,
      listLoading: true,
      requestParam: { name: 'createapisql', offset: 0, pagecount: 0, params: [] },
      requestParam2: {},
      repairmanTypeList: [],
      requestParams: [
        { name: 'getRoleall', offset: 0, pagecount: 0, parammaps: { pastureId: Cookies.get('pastureid') }},
        { name: 'getEmpall', offset: 0, pagecount: 0, params: [] },
        { name: 'getMaintenanceTypeList', offset: 0, pagecount: 0, params: [] },
        { name: 'getMcsAccounts', offset: 0, pagecount: 0, params: [] },
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }}
      ],
      requestFilterParams: { returntype: 'Map', parammaps: {}},
      UpdateDataRelationParam: { name: '', dataname: '', datavalue: '', valuename: '', values: [] },
      dataform: { empname: '', position: '', username: '', fullname: '', empid: '', id: '', sort: '', enable: 1, maintenancePerson: 1, keeper: 1, maintenanceTypeId: '', selectedRole: '' },
      temp1: { empname: '', empid: '', position: '' },
      rolelist: [],
      emplist: [],
      viedoAccountList: [
        // { deviceId: '92301', uId: 'gxlf', pwd: '000000' },
      //   { deviceId: 'apptest2', uId: 'PC', pwd: 'mcs8@666' }
      ],
      getdataListParm: {
        name: 'getuserallL', offset: 1, pagecount: 10, returntype: 'Map',
        parammaps: { pastureId: Cookies.get('pastureid'), empCode: '', empname: '' }
      },
      updateParam: {
        name: 'getMaintenanceTypeByUser', offset: 1, pagecount: 0, returntype: 'list',
        params: []
      },
      rules: {
        empid: [{ required: true, message: '用户名必填', trigger: 'blur' }],
        username: [{ type: 'string', required: true, message: '用户名必填', trigger: 'change' }],
        empname: [{ type: 'string', required: true, message: '职工名必填', trigger: 'change' }],
        sort: [{ validator: isIntegerZero, trigger: 'blur' }]
      },
      dialogFormVisible: false,
      dialogRoleVisible: false,
      selectedRole: '',
      dialogStatus: '',
      textMap: { update: '编辑', create: '新增', role: '选择角色' },
      dialogFormVisibleAdd: false,
      postDataPramas: {},
      disabled: false,
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },
  created() {
    const that = this
    GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'jwt_username': Cookies.get('name') }}).then(response => {
      that.buttons = response.data.list
      that.get_auto_buttons()
    })
    this.get_table_data()
  },
  methods: {
    tableSort(column) {
      sortChange(column, this.list)
    },
    get_auto_buttons() {
      // 新增
      const UserAdd = 'console:user:add'
      const isUserAdd = checkButtons(this.$store.state.user.buttons, UserAdd)
      this.isUserAdd = isUserAdd
      // 角色
      const UserRole = 'console:user:role'
      const isUserRole = checkButtons(this.$store.state.user.buttons, UserRole)
      this.isUserRole = isUserRole
      // 编辑
      const UserEdit = 'console:user:edit'
      const isUserEdit = checkButtons(this.$store.state.user.buttons, UserEdit)
      this.isUserEdit = isUserEdit
      // 重置密码
      const UserResetpassword = 'console:user:resetpassword'
      const isUserResetpassword = checkButtons(this.$store.state.user.buttons, UserResetpassword)
      this.isUserResetpassword = isUserResetpassword
      // 删除
      const UserDelete = 'console:user:delete'
      const isUserDelete = checkButtons(this.$store.state.user.buttons, isUserDelete)
      this.isUserDelete = isUserDelete
    },
    get_table_data() {
      this.listLoading = true
      GetDataByNames(this.requestParams).then(response => {
        this.rolelist = response.data.getRoleall.list
        this.emplist = response.data.getEmpall.list
        this.repairmanTypeList = response.data.getMaintenanceTypeList.list
        this.viedoAccountList = response.data.getMcsAccounts.list
        this.findAllPasture = response.data.findAllPasture.list

        GetDataByName(this.getdataListParm).then(response => {
          this.list = response.data.list
          if (response.data.total) {
            this.total = response.data.total
          }
          setTimeout(() => {
            this.listLoading = false
          }, 1000)
        })
      })
    },
    form_search() {
      this.listLoading = true
      this.getdataListParm.offset = 1
      this.get_table_data()
    },
    resetRequestParam() {
      this.dataform = { username: '', fullname: '', empid: '', id: '', empname: '', position: '', sort: '', enable: 1, selectedRole: '',selectedRole2:'', maintenancePerson: 1, keeper: 1, maintenanceTypeId: '' }
    },
    form_add() {
      this.resetRequestParam()
      this.dialogStatus = 'create'
      this.dialogFormVisibleAdd = true
    },
    changeMaintenanceWorker(item) {
      if (item == 1) {
        this.disabled = false
      } else {
        this.disabled = true
        this.dataform.maintenanceTypeId = []
      }
    },
    add_dialog_save() {
      var role_Array = []
      for (let i = 0; i < this.dataform.selectedRole.length; i++) {
        var role_obj = {}
        this.$set(role_obj, 'role_id', this.dataform.selectedRole[i])
        role_Array.push(role_obj)
      }
      this.dataform.selectedRole2 = role_Array
      var maintenanceTypeId_Array = []
      for (let i = 0; i < this.dataform.maintenanceTypeId.length; i++) {
        var maintenanceTypeId = {}
        this.$set(maintenanceTypeId, 'maintenanceTypeId', this.dataform.maintenanceTypeId[i])
        maintenanceTypeId_Array.push(maintenanceTypeId)
      }
      this.dataform.maintenanceTypeId = maintenanceTypeId_Array
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.dataform.selectedRole2.length <= 0) {
            this.$message({ type: 'warning', message: '请检查角色是否未填写', duration: 2000 })
            return false
          } else {
            this.postDataPramas.common = { 'returnmap': '0' }
            this.postDataPramas.data = []
            this.postDataPramas.data[0] = { 'name': 'createUser', 'type': 'e', 'parammaps': {
              username: this.dataform.username,
              empid: this.dataform.empid,
              sort: '0',
              enable: this.dataform.enable,
              maintenancePerson: this.dataform.maintenancePerson,
              keeper: this.dataform.keeper
            }}
            this.postDataPramas.data[1] = { 'name': 'insertSpotList', 'resultmaps': { 'list': this.dataform.selectedRole2 }}
            this.postDataPramas.data[1].children = []
            this.postDataPramas.data[1].children[0] = { 'name': 'insertUserRole', 'type': 'e', 'parammaps': {
              user_id: '@createUser.LastInsertId',
              role_id: '@insertSpotList.role_id'
            }}
            this.postDataPramas.data[2] = { 'name': 'insertSpotList1', 'resultmaps': { 'list': this.dataform.maintenanceTypeId }}
            this.postDataPramas.data[2].children = []
            this.postDataPramas.data[2].children[0] = { 'name': 'insertUserMaintenance', 'type': 'e', 'parammaps': {
              user_id: '@createUser.LastInsertId',
              maintenance_id: '@insertSpotList1.maintenanceTypeId'
            }}
            console.log('新增保存发送参数', this.postDataPramas)
            ExecDataByConfig(this.postDataPramas).then(response => {
              console.log('新增保存发送参数', this.postDataPramas)
              if (response.msg === 'fail') {
                var username = new RegExp('username')
                if (username.test(response.data)) {
                  this.$notify({ title: '', message: '该用户名称已存在', type: 'warning', duration: 2000 })
                } else {
                  this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
                }
              } else {

                // 没有记录的内容
                this.$notify({ title: '成功', message: '成功', type: 'success', duration: 2000 })
                this.get_table_data()
                this.dialogFormVisibleAdd = false
                // 没有记录的内容




                var pastureId = Cookies.get('pastureid')


                console.log(pastureid)

                var deviceId = this.dataform.deviceId
                var uId = this.dataform.uId
                var pwd = this.dataform.pwd




                console.log('pastureId', pastureId)
                var send_data = {
                  'common': {
                    'returnmap': '0'
                  },
                  'data': [
                    {
                      'name': 'checkDeviceId',
                      'type': 'v',
                      'parammaps': {
                        'deviceId': deviceId

                      }
                    },
                    {
                      'name': 'addDeviceInformation',
                      'type': 'e',
                      'parammaps': {
                        'empId': this.dataform.empid,
                        'uId': uId,
                        'deviceId': deviceId,
                        'pwd': pwd
                      }
                    },
                    {
                      'name': 'updateUserDevice',
                      'type': 'e',
                      'parammaps': {
                        'empId': this.dataform.empid

                      }
                    }

                  ]
                }


                console.log('记录仪保存发送参数===========', send_data)
                ExecDataByConfig(send_data).then(response => {
                  console.log('记录仪保存结果===========', response)
                  if (response.msg === 'fail') {
                    if (response.data == '已被绑定') {
                      var send_data3 = {
                        'name': 'updateDeviceInformation',
                        'type': 'e',
                        'parammaps': {
                          'empId': this.dataform.empid,
                          'deviceId': this.dataform.deviceId
                        }
                      }
                      let str1 = '启用'
                      if(this.dataform.enable == 1){
                        str1 = '启用'
                      }else{
                        str1 = '禁用'
                      }
                      let str = '是否'+ str1 +'该用户?'
                      MessageBox.confirm(str, {
                        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
                      }).then(() => {
                        PostDataByName(send_data3).then(() => {
                          this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
                          this.get_table_data()
                          this.dialogFormVisible = false
                        })
                      })
                    } else {
                      this.dialogFormVisibleAdd = false
                      this.get_table_data()
                      this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
                    }
                  } else {
                    this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
                  }
                })




              }
            })
          }
        }
      })
    },
    handleRole(row) {
      this.requestParam.name = 'getRoleByUser'
      this.requestParam.params = []
      this.requestParam.params[0] = row.id
      this.requestParam.pagecount = 0
      this.requestParam.returntype = 'list'
      this.dataform.id = row.id
      this.dialogRoleVisible = true
      GetDataByName(this.requestParam).then(response => {
        this.selectedRole = response.data.lists.role_id
      })
    },
    updateRole() {
      this.UpdateDataRelationParam.name = 'user_role'
      this.UpdateDataRelationParam.dataname = 'user_id'
      this.UpdateDataRelationParam.datavalue = this.dataform.id
      this.UpdateDataRelationParam.valuename = 'role_id'
      this.UpdateDataRelationParam.values = this.selectedRole

      UpdateDataRelation(this.UpdateDataRelationParam).then(() => {
        this.dialogRoleVisible = false
        this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
        this.get_table_data()
      })
    },
    form_edit(row) {
      console.log(row)
      if (row.maintenancePerson == 1) {
        this.updateParam.params[0] = row.id
        GetDataByName(this.updateParam).then(response => {
          console.log(response.data)
          if (response.data.lists !== null) {
            this.$set(row, 'maintenanceTypeId', response.data.lists.maintenance_id)
          } else {
            this.$set(row, 'maintenanceTypeId', [])
          }
          this.dialogStatus = 'update'
          this.dialogFormVisible = true
          this.disabled = false
        })
      } else {
        this.disabled = true
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$set(row, 'maintenanceTypeId', [])
      }
      this.dataform = Object.assign(row, {})
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit_dialog_save() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.requestParam = {}
          this.requestParam.name = 'updateUser'
          this.requestParam.params = []
          this.requestParam.params[0] = this.dataform.username
          this.requestParam.params[1] = this.dataform.empid
          this.requestParam.params[2] = this.dataform.sort
          this.requestParam.params[3] = this.dataform.enable

        if(this.dataform.maintenancePerson == null || this.dataform.maintenancePerson == undefined){
            this.requestParam.params[4] = 0
        } else {
            this.requestParam.params[4] = this.dataform.maintenancePerson
        }



          this.requestParam.params[5] = this.dataform.keeper
          this.requestParam.params[6] = this.dataform.deviceId
          this.requestParam.params[7] = this.dataform.id
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.requestParam2 = {}
              this.requestParam2.name = 'user_mainthenance'
              this.requestParam2.dataname = 'user_id'
              this.requestParam2.datavalue = this.dataform.id
              this.requestParam2.valuename = 'maintenance_id'
              this.requestParam2.values = this.dataform.maintenanceTypeId
              UpdateDataRelation(this.requestParam2).then(response => {
                if (response.msg !== 'fail') {
                  // this.get_table_data()
                  // this.dialogFormVisible = false

                  var send_data = {
                    'common': {
                      'returnmap': '0'
                    },
                    'data': [
                      {
                        'name': 'checkDeviceId',
                        'type': 'v',
                        'parammaps': {
                          'deviceId': this.dataform.deviceId

                        }
                      },
                      {
                        'name': 'addDeviceInformation',
                        'type': 'e',
                        'parammaps': {
                          'empId': this.dataform.empid,
                          'uId': this.dataform.uId,
                          'deviceId': this.dataform.deviceId,
                          'pwd': this.dataform.pwd
                        }
                      },
                      {
                        'name': 'updateUserDevice',
                        'type': 'e',
                        'parammaps': {
                          'empId': this.dataform.empid

                        }
                      }

                    ]
                  }

                  console.log('记录仪保存发送参数===========', send_data)
                  ExecDataByConfig(send_data).then(response => {
                    console.log('记录仪保存结果===========', response)
                    if (response.msg === 'fail') {
                      if (response.data == '已被绑定') {
                        var send_data3 = {
                          'name': 'updateDeviceInformation',
                          'type': 'e',
                          'parammaps': {
                            'empId': this.dataform.empid,
                            'deviceId': this.dataform.deviceId
                          }
                        }
                        let str1 = '启用'
                        if(this.dataform.enable == 1){
                          str1 = '启用'
                        }else{
                          str1 = '禁用'
                        }
                        let str = '是否'+ str1 +'该用户?'
                        MessageBox.confirm(str, {
                        // MessageBox.confirm('该设备已经绑定，是否重新绑定？', {
                          confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
                        }).then(() => {
                          PostDataByName(send_data3).then(() => {
                            this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
                            this.get_table_data()
                            this.dialogFormVisible = false
                          })
                        })
                      } else {
                        this.get_table_data()
                        this.dialogFormVisible = false
                      }
                    } else {
                      this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
                    }
                  })
                } else {
                  this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
                }
              })
            } else {
              this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
            }
          })
        }
      })
    },
    handleEnableChange(index, row) {
      this.requestParam.name = 'updateUser'
      this.requestParam.params = []
      this.requestParam.params[0] = row.username
      this.requestParam.params[1] = row.empid
      this.requestParam.params[2] = row.sort
      this.requestParam.params[3] = row.enable

      this.requestParam.params[4] = row.id
      PostDataByName(this.requestParam).then(() => {
        this.$notify({ title: '成功', message: '修改成功', type: 'success', duration: 2000 })
      })
    },
    form_delete(row) {
      MessageBox.confirm('确认删除？', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        this.requestParam.name = 'deleteUser'
        this.requestParam.params = []
        this.requestParam.params[0] = row.id
        PostDataByName(this.requestParam).then(() => {
          this.get_table_data()
          this.resetRequestParam()
          this.dialogFormVisible = false
          this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 2000 })
        })
      })
    },

    formNameSearch(queryString, cb) {
      this.requestFilterParams.name = 'userSolr'
      this.requestFilterParams.parammaps['pastureId'] = this.$store.state.user.pastureid
      this.requestFilterParams.parammaps['empname'] = queryString
      GetDataByName(this.requestFilterParams).then(response => {
        cb(response.data.list)
      })
    },
    handleformNameSelect(item) {
      this.dataform.username = item.empCode
      this.dataform.empid = item.id
      this.$set(this.dataform, 'empname', item.empname)
      this.$set(this.dataform, 'position', item.position)
    },
    handleResetPassword(row) {
      MessageBox.confirm('是否确认将此用户密码重置为默认密码xdmy@1234？', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        this.requestParam.name = 'initPassword'
        this.requestParam.parammaps = {}
        this.requestParam.parammaps.id = row.id
        PostDataByName(this.requestParam).then(() => {
          this.get_table_data()
          this.resetRequestParam()
          this.dialogFormVisible = false
          this.$notify({ title: '成功', message: '重置密码成功', type: 'success', duration: 2000 })
        })
      })
    },
    change_deviceId(e) {
      console.log(e)
      var deviceId = e
      var pwd = ''
      var uId = ''
      var viedoAccountList = this.viedoAccountList
      viedoAccountList.forEach(function(io) {
        if (io.deviceId == deviceId) {
          uId = io.uId
          pwd = io.pwd
        }
      })
      this.dataform.uId = uId
      this.dataform.pwd = pwd
      this.dataform.deviceId = deviceId
    }
  }
}
</script>
