<template>
  <el-dialog
    :title="dialogTitle"
    v-model="showDialog"
    width="1100px"
    @open="open()"
    @close="closeDialog(0)"
  >
    <!-- 表格start -->
    <el-table
      :data="tableData"
      border
      stripe
      :header-cell-style="headClass"
      :cell-style="cellStyle"
    >
      <el-table-column type="index" label="序号" width="50px"></el-table-column>
      <el-table-column prop="nickName" label="管理员姓名"></el-table-column>
      <el-table-column
        prop="phonenumber"
        label="管理员手机号"
      ></el-table-column>
      <el-table-column prop="email" label="管理员邮箱"></el-table-column>
      <el-table-column prop="userName" label="管理员账号"></el-table-column>
      <!-- <el-table-column prop="password" label="管理员密码"></el-table-column> -->
      <el-table-column prop="status" label="状态"></el-table-column>

      <el-table-column fixed="right" label="操作" width="240">
        <template #default="scope">
          <el-button
            @click="adminConfig(scope.row)"
            type="text"
            size="small"
            style="color: #409eff"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            style="color: #409eff"
             @click="resetPass(scope.row)"
          >
            密码重置
          </el-button>
          <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除?"
              @confirm="handleDelete(scope.row)"
              @cancel="cancelEvent"
            >
              <template #reference>
                <el-button type="text" size="small" class="delete-text" >
                  
                  删除
                </el-button>
              </template>
            </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格end -->

    <!-- 分页start -->
    <div class="paginationBlock1" style="margin-top:30px">
      <el-pagination
        v-model:currentPage="page"
        :page-sizes="[5,10,15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
      ></el-pagination>
    </div>

    <!-- 分页end -->

    <!-- 管理员配置组件开始 -->
    <admin-Dialog
      :dialog-title="dialogTitle2"
      :item-info="tableItem"
      @closeNo="closeNo"
      :show_dialog2="showDialog2"
      :DialogArray="DialogArray"
    ></admin-Dialog>
    <!-- 管理员配置组件end -->


     <!-- 重置密码组件开始 -->
    <reset-pass
      :dialog-title="dialogTitle3"
      :item-info="tableItem"
      @closeNo3="closeNo3"
      :show_dialog3="showDialog3"
      :DialogArray="DialogArray"
    ></reset-pass>
    <!-- 重置密码组件end -->


  </el-dialog>
</template>
<script>
import { useStore } from 'vuex'
import { defineComponent, ref, watchEffect } from 'vue'
import * as api from '@/api/tenantManage/adminConfig.js'
import { ElMessage } from 'element-plus'
import adminDialog from './adminDialog.vue'
import resetPass from './resetPass.vue'

export default defineComponent({
  components: { adminDialog,resetPass },
  name: 'DialogComponent',
  emits: ['closeDialog'],
  props: {
    show_Dialog: Boolean,
    dialogTitle: String,
    itemInfo: {
      type: Object,
      default: function () {
        return {}
      },
    },
  },
  setup(props, { emit }) {
    const store = useStore()
    const beginTime = ref('')
    const form = ref({})
    const formInfo = ref(null)

    const tableData = ref()
    const currentPage = ref(1)
    const pageSize = ref(5)
    const total = ref(0)

    const tenantId = ref('')
    const userId = ref()

    const tableItem = ref()
    const dialogTitle2 = ref('')
    const dialogTitle3 = ref('')
    const showDialog = ref(false)

    const DialogArray = ref([])
    const showDialog2 = ref(false)
     const showDialog3 = ref(false)

    const handleSizeChange = (val) => {
      pageSize.value = val
      Select()
    }
    const handleCurrentChange = (val) => {
      currentPage.value = val
      Select()
    }

    const roleValid = (rule, value, callback) => {
      rule
      if (value.length === 0) {
        callback(new Error('角色不能为空'))
      } else {
        callback()
      }
    }

    //查询设备列表
    function Select() {
      api
        .getUserList({
          size: pageSize.value,
          page: currentPage.value,
          tenantId: tenantId.value,
        })
        .then((requset) => {
          if (requset.status === 'SUCCESS') {
            tableData.value = requset.data.records.map((val) => {
              store.commit('getTimestampAll', val.startTime)
              val.startTime = store.state.timeProcessing
              store.commit('getTimestampAll', val.endTime)
              val.endTime = store.state.timeProcessing
              store.commit('getTimestampAll', val.createTime)
              val.createTime = store.state.timeProcessing
            })
            tableData.value = requset.data.records
            total.value = requset.data.total
          } else {
            ElMessage.error(requset.msg)
          }
        })
    }

    //管理员配置  （修改操作）
    const adminConfig = (row) => {
      // alert('编辑操作')
       console.log('row')
      console.log(row)

      tableItem.value = {
        nickName: row.nickName,
        userName: row.userName,
        email: row.email,
        phonenumber: row.phonenumber,
        password: row.password,
        status: row.status,
        tenantId: row.tenantId,
        userId:row.userId
      }
      DialogArray.value = row
      dialogTitle2.value = '修改'
      showDialog2.value = true
    }

      //重置密码  （修改操作）
    const resetPass = (row) => {
       console.log('row')
      console.log(row)

      tableItem.value = {
        userId: row.userId,
      }

      DialogArray.value = row
      dialogTitle3.value = '重置密码'
      showDialog3.value = true
    }



    const open = () => {
      form.value = props.itemInfo
      tenantId.value = props.itemInfo.tenantId
      Select()
    }

    // 关闭弹框
    const closeDialog = (flag) => {
      showDialog.value = false
      emit('closeDialog', flag)
    }

   

    watchEffect((fn, options) => {
      fn, options
      showDialog.value = props.show_Dialog
    })
    

   

     //删除 是否删除  ---- 是
    const handleDelete = (row) => {
      api.delUsers({ userId: row.userId }).then((requset) => {
        if (requset.status === 'SUCCESS') {
          ElMessage.success({
            message: '删除成功',
            type: 'success',
          })
          Select()
        } else {
          ElMessage.error(requset.msg)
        }
      })
    }

    const closeNo = (value) => {
      showDialog2.value = value
      Select()
    }

    const closeNo3 = (value) => {
      showDialog3.value = value
    }

    //是否删除  ---- 否
    const cancelEvent = () => {
      console.log('cancel!')
    }



    return {
      roleValid,
      closeDialog,
      open,

      tableData,
      Select,

      store,
      beginTime,

      checked: true,
      form,
      formInfo,

      handleSizeChange,
      handleCurrentChange,

      tenantId,
      userId,

      adminConfig,
      tableItem,
       dialogTitle2,
       dialogTitle3,

      showDialog,

      DialogArray,
      showDialog2,
      showDialog3,

      total,
      pageSize,
      currentPage,
      closeNo,
      closeNo3,


      handleDelete,
      cancelEvent,

      resetPass,

    }
  },
})
</script>
 
<style scoped lang="scss">
.el-input,
.el-select,
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 240px;
}

.el-form-item {
  margin: 0 0 20px !important;
}
// label样式
.el-form-item__label {
  width: 120px !important;
}
.el-form-item__content {
  margin-left: 100px;
}
.demo-form-inline .el-form-item:not(.user-layout .el-form-item) {
  // margin: 0 auto 20px 55px;
}
</style>
<style>
</style>