<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      设备号管理
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        ref="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="姓名、编号、手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deviceNum">
          <el-input
            v-model.trim="searchForm.deviceNum"
            @keyup.enter.native="deviceNum"
            clearable
            placeholder="设备号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="timer">
          <el-date-picker
            v-model.trim="searchForm.timer"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="绑定开始日期"
            end-placeholder="绑定结束日期"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column align="center" prop="studentId" label="用户编号">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.userId }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="用户姓名">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.user.realName }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="手机号码">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.user.phone }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="设备号" width="280px">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.deviceNum }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="绑定时间">
            <template slot-scope="scope">
              <div>
                {{ scope.row.bindTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <auth auths="userDevice/unbind">
                  <el-button type="text" @click="unbind(scope.row)"
                    >解除限制</el-button
                  >
                </auth>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import pagination from "@/components/Pagination/index";
import { getUserDevice, unbindUserDevice } from "./api";
import { getTimes } from "@/utils";
export default {
  components: { pagination },
  data() {
    return {
      searchForm: {
        search: null,
        deviceNum: null,
        timer: [],
      },

      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      const { timer, ...rest } = this.searchForm;
      let obj = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        ...getTimes(timer, ["bindStartTime", "bindEndTime"], "YYYY-MM-DD"),
      };
      try {
        const res = await getUserDevice(obj);
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs["searchForm"].resetFields();
      this.search();
    },
    unbind(row) {
      this.$confirm(
        "请确认是否解除绑定,解除后该设备号可重新绑定账号",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          try {
            const res = await unbindUserDevice({ id: row.id });
            this.$message.success("解除绑定成功");
            this.getList();
          } catch (e) {
            console.log(e);
          }
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang='scss' scoped>
</style>
