<template>
  <div class="app-container">
    <div v-show="!isShowUpdate">
      <div class="search-layout" style="margin-bottom: 0px; margin-top: -10px">
        <el-input
          v-model="form.sn"
          placeholder="设备sn"
          class="filter-item input"
        />
        <el-select
          v-model="form.tenantId"
          class="filter-item"
          placeholder="服务商平台名称"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in merchantLists"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-model="form.userLibId"
          class="filter-item"
          placeholder="请选择⽤户库"
          clearable
        >
          <el-option
            v-for="(item, index) in userLibList"
            :key="index"
            :label="item.userLibName"
            :value="item.id"
          />
        </el-select>
        <el-button
          class="filter-item"
          style="margin-right: 0;margin-left: 10px"
          type="primary"
          icon="el-icon-search"
          @click="query(1)"
        >
          搜索
        </el-button>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="margin-top: 20px"
        element-loading-text="加载中..."
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{
              (pagination.page - 1) * pagination.pageSize + (scope.$index + 1)
            }}
          </template>
        </el-table-column>
        <el-table-column label="设备sn">
          <template slot-scope="scope">
            {{ scope.row.sn }}
          </template>
        </el-table-column>
        <el-table-column label="服务商平台名称">
          <template slot-scope="scope">
            {{ scope.row.tenantName }}
          </template>
        </el-table-column>
        <el-table-column label="用户库名称">
          <template slot-scope="scope">
            {{ scope.row.userLibName }}
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
          class-name="status-col"
          width="120"
        >
          <template slot-scope="{ row }">
            <el-tag :type="row.status | statusFilter">
              {{ row.status | statusFilterStr }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row, $index }">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="pagination.total"
        :page.sync="pagination.page"
        :limit.sync="pagination.pageSize"
        :page-sizes="pagination.pageSizeOptions"
        :layout="pagination.layout"
        @pagination="handleSizeChange"
      />
    </div>
    <UpdateModule
      v-show="isShowUpdate"
      ref="updateModel"
      :user-lib-list="userLibList"
      :merchant-lists="merchantLists"
      @success="modalSuccess"
      @cancel="isShowUpdate = false"
    />
  </div>
</template>

<script>
import UpdateModule from './UpdateModule.vue'
import tableMixins from '@/mixins/tableMixins'
import Pagination from '@/components/Pagination'
import { getAuthHHFaceUserLibList, getAuthHHFaceTenantList, getList } from '@/api/HHFaceDeviceAuth'
export default {
  components: { Pagination, UpdateModule },
  filters: {
    statusFilter(status) {
      return status ? 'success' : 'info'
    },
    statusFilterStr(status) {
      return status ? '已授权' : '未授权'
    }
  },
  mixins: [tableMixins],
  data() {
    return {
      updateData: {},
      merchantLists: [],
      userLibList: [],
      isShowUpdate: false,
      pagination: {
        page: 1,
        current: 1
      },
      form: {
        id: null
      }
    }
  },
  created() {
    this.getMerchantList()
    this.getUserLibList()
  },
  methods: {
    initData() {
      this.getTableList()
    },
    getMerchantList() {
      getAuthHHFaceTenantList().then((res) => {
        this.merchantLists = res.data
      })
    },
    getUserLibList() {
      getAuthHHFaceUserLibList().then((res) => {
        this.userLibList = res.data
      })
    },
    // 获取表格数据
    async getTableList() {
      this.pagination.current = this.pagination.page
      this.tableLoading = true
      try {
        const result = await getList(this.getFilterParams())
        this.setDataList(result.data)
      } catch (e) {
        console.log(e)
      } finally {
        this.tableLoading = false
      }
    },
    // 获取筛选信息
    query(type) {
      if (type) {
        this.pagination.page = 1
        this.pagination.current = 1
      }
      this.clearPageParams()
      this.getTableList()
    },
    // 增加筛选条件
    transformFilterForm() {
      return {
        userLibId: this.form.userLibId || null,
        tenantId: this.form.tenantId || null,
        sn: this.form.sn || null
      }
    },
    modalSuccess(isEdit) {
      this.isShowUpdate = false
      if (!isEdit) {
        this.clearPageParams()
      }
      this.getTableList()
    },
    handleUpdate(row) {
      this.$nextTick(() => {
        this.isShowUpdate = true
        // this.updateData = JSON.parse(JSON.stringify(row))
        this.$refs.updateModel.open(Object.assign({}, JSON.parse(JSON.stringify(row))))
      })
    }
  }
}
</script>

<style scoped>
.search-layout {
  margin-bottom: 20px;
}
.search-layout .filter-item {
  margin-right: 20px;
  margin-top: 10px;
}
.search-layout .filter-item:last-of-type {
  margin-right: 0;
}
.head {
  margin-left: 16px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
}
.input {
  width: 200px;
  margin-right: 15px;
}
.m-r {
  margin-right: 15px;
}
</style>
