<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item width="100" prop="userName" :label="$t('staff.yhm')">
            <el-input
              v-model="queryForm.userName"
              size="small"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="nickName" label="昵称">
            <el-input
              v-model="queryForm.nickName"
              size="small"
              placeholder="请输入昵称"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="phone" label="手机号">
            <el-input
              v-model="queryForm.phone"
              size="small"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="email" :label="$t('staff.yx')">
            <el-input
              v-model="queryForm.email"
              size="small"
              placeholder="请输入邮箱"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="deptId" label="部门">
            <el-select
              v-model="queryForm.deptId"
              size="small"
              placeholder="请选择部门"
            >
              <el-option value="" :label="$t('dataEnquiry.qb')"></el-option>
              <el-option
                v-for="item in deptData"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="handleAdd">
        添加用户
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        {{ $t('defalult.plsc') }}
      </el-button>
    </div>
    <el-table
      :data="roleData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="userName"
        :label="$t('staff.yhm')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dept.deptName"
        label="部门"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="nickName"
        label="名字"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="手机"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="email"
        :label="$t('staff.yx')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="wechat"
        label="微信"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        :label="$t('dataEnquiry.zt')"
        align="center"
        width="60"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @click="soreStatus(scope.row.status, scope.row.id)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('grouping.cjsj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        :label="$t('grouping.gxsj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        :label="$t('defalult.cz')"
        width="350"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
          <el-button type="text" @click="assignRole(row)">平台角色</el-button>
          <el-button type="text" @click="assignMiniRole(row)">
            小程序角色
          </el-button>
          <el-button type="text" @click="assignProduct(row)">
            分配项目
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <manage-edit ref="edit"></manage-edit>
    <manage-role ref="index"></manage-role>
    <manage-product ref="product"></manage-product>
    <manage-mini-role ref="mini"></manage-mini-role>
  </div>
</template>

<script>
import { doDelete, doEdit, getList, userBindWechat } from '@/api/manage';
import manageEdit from './components/user/manageEdit';
import manageRole from './components/user/manageRole';
import manageProduct from './components/user/manageProduct';
import manageMiniRole from './components/user/manageMiniRole';
import { getDeptList } from '@/api/dept';
export default {
  name: 'ManageIndex',
  components: {
    manageEdit,
    manageRole,
    manageProduct,
    manageMiniRole,
  },
  data() {
    return {
      roleData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      input: '',
      textarea: '',
      list: [],
      ids: [],
      deptData: [],
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        userName: '',
        nickName: '',
        phone: '',
        email: '',
        deptId: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
    this.getDeptData();
  },
  methods: {
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    // 部门列表
    async getDeptData() {
      const { data } = await getDeptList();
      this.deptData = data;
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ status: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    manageIndex() {
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.roleData = data.items;
      this.total = data.total;
      console.log(this.roleData);
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    handleAdd() {
      this.$refs['edit'].manageEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].manageEdit(row);
    },
    assignRole(row) {
      this.$refs['index'].manageRole(row);
    },
    assignMiniRole(row) {
      this.$refs['mini'].manageMiniRole(row);
    },
    assignProduct(row) {
      this.$refs['product'].manageProduct(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    deleteData(data) {
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(data);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          const that = this;
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      if (row.id) {
        this.deleteData([row.id]);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-button {
  margin-bottom: 30px;
}
</style>
