<template>
  <div class="chain-tn-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.chain_tn_nickname"
              clearable
              :placeholder="__('请输入技师名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('技师编号')"
        prop="chain_tn_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('技师头像')"
        prop="chain_tn_avatar"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.chain_tn_avatar"
            :preview-src-list="[scope.row.chain_tn_avatar]"
            :src="scope.row.chain_tn_avatar"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('技师名称')"
        prop="chain_tn_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('职位')"
        prop="chain_tn_position"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('服务数量')"
        prop="chain_service_num"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('好评率')"
        prop="chain_praise_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('手机号')"
        prop="chain_tn_phone"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('激活状态')"
        prop="enable"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="chain_tn_sort"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('注册时间')"
        prop="create_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="100"
      >
        <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>
    <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 { getList, doDelete } from '@/api/chain/tn/base'
  import Edit from './components/ChainTnEdit'

  export default {
    name: 'ChainTn',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          chain_tn_nickname: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.chain_tn_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      formatState(row) {
        return row.enable === 1 ? '已激活' : '未激活'
      },
      handleDelete(row) {
        if (row.chain_tn_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              chain_tn_id: row.chain_tn_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const chain_tn_id = this.selectRows
              .map((item) => item.chain_tn_id)
              .join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ chain_tn_id })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }

                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        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>
