<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.contract_type_name"
              clearable
              :placeholder="__('请输入保障名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" 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="500">
      <el-table-column
        align="center"
        :label="__('服务类别名称')"
        prop="contract_type_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商铺名称')"
        prop="store_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('申请状态')"
        prop="contract_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatUserState"
        :label="__('加入状态')"
        prop="contract_use_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('保证金余额')"
        prop="contract_deposit"
        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"
    />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/store/contract'
  export default {
    name: 'StoreContract',

    data() {
      return {
        title: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        dialogFormVisible: false,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          contract_type_name: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatState(row) {
        return row.contract_state === 1
          ? this.__('申请加入')
          : row.contract_state === 2
          ? this.__('申请推出')
          : this.__('通过')
      },
      formatUserState(row) {
        return row.contract_use_state === 1
          ? this.__('已加入')
          : this.__('已退出')
      },

      showTable() {
        this.fetchData()
        this.dialogFormVisible = true
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
