<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.model_name"
              clearable
              :placeholder="__('请输入模板名称')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="fetchData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="350"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        :label="__('模板ID')"
        prop="model_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('模板名称')"
        prop="model_name"
        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"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">
        {{ __('确定') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getDriveList } from '@/api/base/product/drive'

  export default {
    name: 'SelectDriverModelTable',
    components: {},
    data() {
      return {
        list: [],
        listLoading: true,
        disabled: false,
        form: {},
        action: '',
        total_id: undefined,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: [],
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        param: {},
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showTables(driverModelId) {
        this.fetchData()
        this.dialogFormVisible = true
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      close() {
        this.queryForm.model_name = ''
        this.param = {}
        this.list = []
        this.dialogFormVisible = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getDriveList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async save() {
        if (this.selectRows.length === 0) {
          this.$baseMessage('请选择模板', 'success')
          return
        }
        if (this.selectRows.length > 1) {
          this.$baseMessage('同一服务只能选择一个模板', 'success')
          return
        }

        let selectModels = this.selectRows.map((item) => item.model_id)
        let selectModelName = this.selectRows.map((item) => item.model_name)
        if (selectModels.length > 0) {
          this.$emit(
            'fetch-data',
            selectModels.toString(),
            selectModelName.toString()
          )
        }

        this.close()
        this.disabled = false
      },
    },
  }
</script>
