<template>
  <d2-container>
    <template #header>
      <div class="search-bar">
        <el-form :inline="true" size="mini">
          <el-form-item :label="$t(key('batch_no'))">
            <el-input
              v-model="search.batch_no"
              :placeholder="$t(key('please_enter'))"
              clearable
              style="width:200px"
              @keyup.enter.native="onSearch"
            />
          </el-form-item>
          <el-form-item :label="$t(key('product_no'))">
            <el-input
              v-model="search.product_no"
              :placeholder="$t(key('please_enter'))"
              clearable
              style="width:200px"
              @keyup.enter.native="onSearch"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">
              {{ $t(key('search')) }}
            </el-button>
            <el-button icon="el-icon-refresh" @click="onReset">
              {{ $t(key('reset')) }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>

    <page-table
      ref="pageTable"
      :columns="columns"
      :data="tableData"
      :loading="loading"
      :toolbar-buttons="toolbarButtons"
      :row-buttons="rowButtons"
      :pagination="pagination"
      help-url="/help/quality-management/first-article-inspection-reports"
      :help-text="$t(ckey('help'))"
      auto-height
      @page-change="onPageChange"
      @selection-change="onSelect"
    />

  </d2-container>
</template>

<script>
import { useTableColumns } from '@/composables/useTableColumns'
import { useTableButtons } from '@/composables/useTableButtons'
import { i18nMixin } from '@/composables/useI18n'
import { confirmMixin } from '@/composables/useConfirmHandle'
import {
  fetchFirstArticleInspectionReportsList
} from '@/api/quality-management/first-article-inspection-reports'
import PageTable from '@/components/page-table'

function readList (res) {
  if (Array.isArray(res)) return { list: res, total: res.length }
  const data = res && res.data ? res.data : res
  if (Array.isArray(data)) return { list: data, total: data.length }
  const list = (data && (data.list || data.rows || data.records || data.data)) || []
  const total = data && (data.total || data.count || list.length)
  return { list: Array.isArray(list) ? list : [], total: Number(total) || 0 }
}

export default {
  name: 'quality-management-first-article-inspection-reports',
  components: { PageTable },
  mixins: [i18nMixin('page.quality_management.first_article_inspection_reports'), confirmMixin],
  data () {
    return {
      loading: false,
      submitting: false,
      tableData: [],
      selectedRows: [],
      dialogVisible: false,
      dialogTitle: '',
      editId: '',
      handleType: 'create',
      search: {"batch_no":"","product_no":""},
      pagination: { current: 1, size: 10, total: 0 },
      formData: {},
      rules: {

      },
      columns: [],
      toolbarButtons: [],
      rowButtons: [],
      formCols: [

      ]
    }
  },
  created () {
    this.columns = useTableColumns([
      { prop: 'batch_no', label: this.key('batch_no'), minWidth: 130 },
      { prop: 'product_no', label: this.key('product_no'), minWidth: 130 },
      { prop: 'inspection_item', label: this.key('inspection_item'), minWidth: 170 },
      { prop: 'inspection_result', label: this.key('inspection_result'), minWidth: 170 },
      { prop: 'inspector', label: this.key('inspector'), minWidth: 130 },
      { prop: 'create_date', label: this.key('create_date'), minWidth: 130 }
    ])
    const btns = useTableButtons({
      toolbar: [],
      row: []
    }, this.$permission)
    this.toolbarButtons = btns.toolbarButtons
    this.rowButtons = btns.rowButtons
    this.fetchData()
  },
  methods: {
    async fetchData () {
      this.loading = true
      try {
        const params = {
          ...this.search,
          page_no: this.pagination.current,
          page_size: this.pagination.size,
          page: this.pagination.current,
          size: this.pagination.size
        }
        const res = await fetchFirstArticleInspectionReportsList(params)
        const { list, total } = readList(res)
        this.tableData = list
        this.pagination.total = total
      } finally {
        this.loading = false
      }
    },
    onSearch () {
      this.pagination.current = 1
      this.fetchData()
    },
    onReset () {
      this.search = {"batch_no":"","product_no":""}
      this.pagination.current = 1
      this.fetchData()
    },
    onPageChange (page) {
      this.pagination.current = page.current
      this.pagination.size = page.size
      this.fetchData()
    },
    onSelect (rows) {
      this.selectedRows = rows
    }
  }
}
</script>

<style scoped>
.search-bar {
  padding: 10px 0;
}
.spc-chart-panel {
  height: 320px;
  margin-bottom: 12px;
  border: 1px solid #ebeef5;
}
/deep/ .el-form-item--mini.el-form-item {
  margin-bottom: 4px;
}
</style>
