<template>
  <div class="videoManage manage-page">
    <div class="flex-view flex-title">
      <div class="table-title">
        <img src="../../assets/manageImg/userInfoControl.png" alt="">用户信息管理
      </div>
      <div class="table-title-btn blue" @click="openModel({}, 0)">
        <img src="../../assets/manageImg/add-btn.png" alt="">添加
      </div>
    </div>
    <div class="flex-view ">
      <div class="manage-input  w-180">
        <span class="input-title">昵称查询</span>
        <input class="search-input" type="text" v-model="searchName" placeholder="">
      </div>
      <div class="table-title-btn blue" @click="dataInit">
        查询
      </div>
    </div>
    <div class="table-view">
      <el-table :data="tableList" :row-class-name="tableRowClassName" height="540" style="width: 100%">
        <el-table-column prop="userName" v-model="nowData.userName" label="用户名">
        </el-table-column>
        <el-table-column prop="nickName" v-model="nowData.nickName" label="昵称">
        </el-table-column>
        <el-table-column prop="phonenumber" v-model="nowData.phonenumber" label="手机号">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-flex">
              <div class="table-operate blue" @click="openModel(scope.row, 1)">
                <img src="../../assets/manageImg/table-edit.png" alt="">编辑
              </div>
              <div class="table-operate red" @click="openPassword(scope.row, 1)">
                <img src="../../assets/manageImg/table-edit1.png" alt="">修改密码
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100, 200, 500, 1000, 1500]" :page-size="pageSize"
        layout="->,total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="提示" top="10vh" :visible.sync="passwordModel" width="460px" class="vrcode-model manage-model"
      :append-to-body="true">
      <div class="vrcode-model-title">
        修改密码
      </div>
      <div class="vrcode-content">
        <div class="manage-left">
          <div class="manage-input  w-400">
            <div class="input-title">原密码</div>
            <input type="text" v-model="pwdData.oldPassword" placeholder="请输入原密码">
          </div>
          <div class="manage-input  w-400">
            <div class="input-title">新密码</div>
            <input type="text" v-model="pwdData.password" placeholder="请输入新密码">
          </div>
          <div class="manage-input  w-400">
            <div class="input-title">确认密码</div>
            <input type="text" v-model="confirmPwd" placeholder="请输入确认密码">
          </div>
        </div>
        <!-- <div class="manage-right">
           
              </div> -->
      </div>
      <span slot="footer" class="dialog-footer">
        <div class="vrcode-btn blue-btn w-100" @click="confirmPwdBtn">确定</div>
        <div class="vrcode-btn cancle-btn w-100" @click="passwordModel = false">取消</div>
      </span>
    </el-dialog>
    <el-dialog title="提示" top="10vh" :visible.sync="addDeviceModel" width="460px" class="vrcode-model manage-model"
      :append-to-body="true" :close-on-click-modal="false">
      <div class="vrcode-model-title">
        添加设备
      </div>
      <div class="vrcode-content">
        <div class="manage-left">
          <div class="manage-input  w-400">
            <div class="input-title">设备code码</div>
            <input type="text" v-model="openData.deviceId" placeholder="请输入设备code码">
          </div>
          <!-- <div class="manage-input  w-400">
            <div class="input-title">设备类型</div>
            <input type="text" v-model="openData.password" placeholder="请输入新密码">
          </div> -->
          <div class="manage-input-sel w-400" style="margin-right: 0;">
            <div class="input-title">设备类型</div>
            <el-dropdown @command="handleCommand1">
              <div class="el-dropdown-link">
                <input v-model="deviceType" type="text" class="input-input" placeholder="请选择设备类型">
                <div class="arrow"><img src="../../assets/image/arrow.png" alt=""></div>
              </div>
              <el-dropdown-menu slot="dropdown" class="manage-dropdown w-400">
                <el-dropdown-item v-for="item, index in deviceSel
                " :key="index" :command="item.label">{{ item.label }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="manage-input  w-400">
            <div class="input-title">排序</div>
            <input type="text" v-model="openData.equipmentStatu" placeholder="请输入排序">
          </div>
        </div>
        <!-- <div class="manage-right">
           
              </div> -->
      </div>

      <span slot="footer" class="dialog-footer">
        <div class="vrcode-btn blue-btn w-100" @click="addDevice">确定</div>
        <div class="vrcode-btn cancle-btn w-100" @click="addDeviceModel = false">取消</div>
      </span>
    </el-dialog>
    <el-dialog title="提示" top="10vh" :visible.sync="addModel" width="760px" class="vrcode-model manage-model"
      :append-to-body="true" :close-on-click-modal="false">
      <div class="vrcode-model-title">
        {{ modelName }}
      </div>
      <div class="vrcode-content">
        <div class="manage-left">
          <div class="manage-input w-350">
            <div class="input-title">用户名</div>
            <input type="text" v-model="nowData.userName" placeholder="请输入用户名">
          </div>
          <div class="manage-input w-350">
            <div class="input-title">手机号</div>
            <input type="text" v-model="nowData.phonenumber" placeholder="请输入手机号">
          </div>
        </div>
        <div class="manage-right">
          <div class="manage-input  w-350">
            <div class="input-title">昵称</div>
            <input type="text" v-model="nowData.nickName" placeholder="请输入昵称">
          </div>
          <div class="manage-input-sel w-350" style="margin: 0;">
            <div class="input-title">角色</div>
            <el-dropdown @command="handleCommand">
              <div class="el-dropdown-link">
                <input v-model="role" type="text" class="input-input" placeholder="请选择角色">
                <div class="arrow"><img src="../../assets/image/arrow.png" alt=""></div>
              </div>
              <el-dropdown-menu slot="dropdown" class="manage-dropdown w-350">
                <el-dropdown-item v-for="item, index in typeSelList
                " :key="index" :command="item.label">{{ item.label }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <!-- <div class="manage-input  w-180">
            <div class="input-title">关联设备ID</div>
            <input type="text" :title="nowData.device" v-model="nowData.device" disabled placeholder="请输入关联设备ID">
          </div> -->
        </div>

      </div>
      <div class="vrcode-content" style="padding-top: 0;" v-if="modelName!='添加'">
        <div class="manage-left">

        </div>
        <div class="manage-right">
          <div class="table-title-btn blue" @click="openaddDeviceModel">
            添加
          </div>
        </div>
      </div>
      <div class="table-view" style="margin-top: 5px;"  v-if="modelName!='添加'">
        <el-table :data="nowData.equipments" :row-class-name="tableRowClassName" height="300" style="width: 100%">
          <el-table-column prop="deviceId" v-model="nowData.userName" label="设备code码">
          </el-table-column>
          <el-table-column prop="equipmentName" v-model="nowData.nickName" label="设备类型">
            <template slot-scope="scope">
              <div>{{ getEquipmentName(scope.row.equipmentName) }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="equipmentStatu" v-model="nowData.phonenumber" label="排序">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="table-flex">
                <el-popconfirm @confirm="delDevice(scope.row, scope.$index)" title="这一段内容确定删除吗？" icon-color="red">
                  <div class="table-operate red" slot="reference">
                    <img src="../../assets/manageImg/table-delete.png" alt="">删除
                  </div>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <div class="vrcode-btn blue-btn w-100" @click="confirmEdit">确定</div>
        <div class="vrcode-btn cancle-btn w-100" @click="addModel = false">取消</div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      tableList: [],
      total: 0,
      modelName: '添加',
      addModel: false,//编辑弹窗
      passwordModel: false,//修改密码弹窗
      addDeviceModel: false,//添加设备弹窗
      nowData: {
      },
      role: '',

      openData: {},
      searchName: '',
      deviceList: [],
      pwdData: {
        "oldPassword": "",
        "password": "",
        "id": ''
      },
      confirmPwd: '',
      typeSelList: [{ label: '管理员', value: 1, }, { label: '用户', value: 2, }],

      deviceType:'',
     deviceSel: [{ label: '控制器', value: 10, },
       { label: '施肥机', value: 1, }, 
       { label: '气象站', value:30, }, 
       { label: '智能灌溉控制器', value:20, }],
    }
  },
  mounted() { this.dataInit() },
  methods: {
    dataInit() {

      this.pageSize = 10
      this.currentPage = 1
      setTimeout(() => {
        this.getData()
      }, 0);
    },
    addDevice() {
      if (!this.openData.deviceId) {
        this.$message.error('设备code码还未填写');
        return
      }
      if (!this.openData.equipmentName) {
        this.$message.error('设备类型还未选择');
        return
      }
      if (!this.openData.equipmentStatu) {
        this.$message.error('排序还未填写');
        return
      }
      // if(this.modelName=='添加'){
      //   this.nowData.equipments.push(this.openData)
       
      // }else{
       
      // }
        this.api.addUserByid({...this.openData,userId:this.nowData.id}).then(res=>{
        
          if (res.data.code == 200) {
            this.nowData.equipments.push({...this.openData,userId:this.nowData.id})
          this.addDeviceModel=false
          this.getData()
          this.$message.success(res.data.msg);
          
        } else {
          this.$message.error(res.data.msg);
        }
        })

    },
    //弹窗删除设备
    delDevice(item, index) {
      var data = { deviceId: item.deviceId }

      this.api.delUserByid(data).then(res => {
        if (res.data.code == 200) {
          this.nowData.equipments.splice(index, 1)
          this.getData()
          this.$message.success(res.data.msg);
          
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    getEquipmentName(e) {
      if (e == 10) {
        return '控制器'
      } else if (e == '1') {
        return '施肥机'
      } else if (e == '30') {
        return '气象站'
      } else if (e == '20') {
        return '智能灌溉控制器'
      } else {
        return ''
      }
    },
    getData() {
      var data = {
        page: this.currentPage,
        pageSize: this.pageSize,
        nickName: this.searchName
      }
      this.loading = this.$loading({
        lock: true,
        text: '加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.api.adminAll(data).then(res => {
        this.loading.close()
        if (res.data.code == 200) {
          this.tableList = res.data.data.user
          this.total = res.data.data.total
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    confirmEdit() {
      if (!this.nowData.userName) {
        this.$message.error('用户名还未填写');
        return
      }
      if (!this.nowData.phonenumber) {
        this.$message.error('手机号还未填写');
        return
      }
      if (!this.nowData.nickName) {
        this.$message.error('昵称还未填写');
        return
      }
      if (!this.nowData.role) {
        this.$message.error('角色还未选择');
        return
      }
    
      if (this.modelName == '添加') {
        this.api.addUser(this.nowData).then(res=>{
          if (res.data.code == 200) {
            this.getData()
            this.$message.success(res.data.msg);
            this.addModel = false
          } else {
            this.$message.error(res.data.msg);
            this.addModel = false
          }
        })
      } else {
        if (!this.nowData.equipments.length) {
        this.$message.error('请添加一个设备');
        return
      }
        this.api.updateUser(this.nowData).then(res => {
          if (res.data.code == 200) {
            this.getData()
            this.$message.success(res.data.msg);
            this.addModel = false
          } else {
            this.$message.error(res.data.msg);
            this.addModel = false
          }
        })
      }

    },
    getRole(e) {
      if (e == '管理员') {
        return 1
      } else {
        return 2
      }
    },
    handleCommand(e) {
      this.nowData.role = this.getRole(e)
      this.role = e
    },
    getDeviceType(e) {
      if (e == '控制器') {
        return 10
      } else if(e == '施肥机'){
        return 1
      } else if(e == '气象站'){
        return 30
      }else if(e == '智能灌溉控制器'){
        return 20
      }
    },
    handleCommand1(e) {
      this.openData.equipmentName = this.getDeviceType(e)
      this.deviceType = e
    },
    confirmPwdBtn() {
      if (this.confirmPwd != this.pwdData.password) {
        this.$message.error('两次密码不一致');
        return
      }
      this.api.updatePwd(this.pwdData).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
          this.passwordModel = false
        } else {
          this.$message.error(res.data.msg);
          this.passwordModel = false
        }
      })
    },
    openaddDeviceModel(){
      this.addDeviceModel = true
      this.openData={}
    },
    openModel(item, type) {
      if (type == 0) {
        this.modelName = '添加'
        this.addModel = true
        this.nowData = {
          equipments: [],
        }
        this.role = ''
      } else {
        this.modelName = '编辑'
        this.addModel = true

        // const deviceIds = this.deviceList.map(device => device.deviceId);

        // // 使用 join 方法将 deviceId 连接成一个字符串，以逗号隔开
        // const deviceIdString = deviceIds.join(',');
        this.nowData = JSON.parse(JSON.stringify(item))
        if (this.nowData.role == 1) {
          this.role = '管理员'
        } else {
          this.role = '用户'
        }
        if(!this.nowData.role){
          this.nowData.role=2
        }
      }
    },
    openPassword(item) {
      this.pwdData = {
        "oldPassword": "",
        "password": "",
        "id": item.id
      }
      this.passwordModel = true
    },
    handleSizeChange(val) {
      this.pageSize = val
      setTimeout(() => {
        this.getData()
      }, 0);
    },
    handleCurrentChange(val) {
      this.currentPage = val
      setTimeout(() => {
        this.getData()
      }, 0);
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row';
      } else if (rowIndex % 2 == 0) {
        return 'success-row';
      }
      return '';
    }
  },

}
</script>
<style lang="scss"></style>