<template>
  <div class="classes-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model="value"
              :placeholder="__('请选择')"
              @change="queryData"
            >
              <el-option
                v-for="item in searchList"
                :key="item.lable"
                :label="item.lable"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="6">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" :label="__('操作')">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('微信号')"
        prop="wechat_code"
        show-overflow-tooltip
      />
      <el-table-column align="center" :label="__('审核状态')" width="100">
        <template slot-scope="scope">
          <div class="tag-group">
            <el-tag v-if="scope.row.verify_state == 1">
              {{ __('已提交') }}
            </el-tag>
            <el-tag v-else-if="scope.row.verify_state == 2" effect="success">
              {{ __('已审核通过') }}
            </el-tag>
            <el-tag v-else effect="danger">{{ __('审核拒绝') }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatterApplyTime"
        :label="__('申请时间')"
        prop="apply_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatterVerifyTime"
        :label="__('审核时间')"
        prop="verify_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('审核备注')"
        prop="verify_remark"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { timestampToTime } from '@/utils/date'
  import { getList, doDelete } from '@/api/user/anchor'
  import Edit from './components/AnchorEdit'
  import { isNum } from '@/utils/validate'

  export default {
    name: 'Anchor',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        searchList: [
          { value: 0, lable: '全部' },
          { value: 1, lable: '待审核' },
          { value: 2, lable: '审核通过' },
          { value: 3, lable: '审核驳回' },
          { value: 4, lable: '重新提交' },
        ],
        value: undefined,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
        lecturerList: [],
        cycleList: [],
        classes_lecturer_id: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      formatterVerifyTime(row) {
        return timestampToTime(row.verify_time)
      },
      formatterApplyTime(row) {
        return timestampToTime(row.apply_time)
      },
      handleDelete(row) {
        if (row.user_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              user_id: row.user_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ ids })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      handleEdit(row) {
        if (row) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData(val) {
        if (isNum(val)) {
          this.queryForm.verify_state = val
        }
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
