<template>
  <div>
    <search-form :items="formItems" v-model="queryParam" @search="searchQuery">
      <div style="float: right" class="btn-group" slot="additional">
        <a-button type="primary" @click="onDel">
          <img src="@/assets/images/global/delete.png" alt="" />
          Delete
        </a-button>
      </div>
    </search-form>
    <custom-table
      size="middle"
      rowKey="sampleId"
      :columns="columns"
      :list="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
      :selectedRowKeys.sync="selectedRowKeys"
      :scroll="{ x: true, y: 'calc(100vh - 405px)' }"
    >
      <template slot="index" slot-scope="{ index }">
        {{ index + 1 }}
      </template>
    </custom-table>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '../../api/manage'

const columns = [
  {
    title: 'NO',
    align: 'center',
    width: 100,
    scopedSlots: {
      customRender: 'index'
    }
  },
  {
    title: 'SITE DET CODE',
    align: 'center',
    dataIndex: 'siteDetCode',
    width: 130
  },
  {
    title: 'SAMPLE ID',
    align: 'center',
    width: 100,
    dataIndex: 'sampleId'
  },
  {
    title: 'STATION',
    align: 'center',
    width: 100,
    dataIndex: 'stationName'
  },
  {
    title: 'DETECTOR',
    align: 'center',
    width: 100,
    dataIndex: 'detectorsName'
  },
  {
    title: 'SAMPLE TYPE',
    align: 'center',
    width: 120,
    dataIndex: 'sampleType'
  },
  {
    title: 'DATA TYPE',
    align: 'center',
    width: 100,
    dataIndex: 'dataType'
  },
  {
    title: 'GEOMETRY',
    align: 'center',
    width: 100,
    dataIndex: 'geometry'
  },
  {
    title: 'QUALIFIE',
    align: 'center',
    width: 100,
    dataIndex: 'spectralQualifie'
  },
  {
    title: 'TRANSMIT DTG',
    align: 'center',
    width: 150,
    dataIndex: 'transmitDtg'
  },
  {
    title: 'COLLECT START',
    align: 'center',
    width: 150,
    dataIndex: 'collectStart'
  },
  {
    title: 'COLLECT STOP',
    align: 'center',
    width: 150,
    dataIndex: 'collectStop'
  },
  {
    title: 'ACQ.START',
    align: 'center',
    width: 150,
    dataIndex: 'acquisitionStart'
  },
  {
    title: 'ACQ.STOP',
    align: 'center',
    width: 150,
    dataIndex: 'acquisitionStop'
  },
  {
    title: 'ACQ.REAL(S)',
    align: 'center',
    width: 120,
    dataIndex: 'acquisitionRealSec'
  },
  {
    title: 'ACQ.LIVE(S)',
    align: 'center',
    width: 100,
    dataIndex: 'acquisitionLiveSec'
  },
  {
    title: 'QUANTITY',
    align: 'center',
    width: 100,
    dataIndex: 'quantity'
  },
  {
    title: 'STATUS',
    align: 'center',
    width: 100,
    dataIndex: 'status'
  }
]

export default {
  mixins: [JeecgListMixin],
  data() {
    this.columns = columns
    return {
      queryParam: {
        station: '',
        detector: ''
      },
      url: {
        list: '/gardsSampleData/findPage',
        delete: '/gardsSampleData/deleteById'
      },
      stationList: [],
      detectorList: []
    }
  },
  created() {
    this.getStationList()
    this.getDetectorList()
  },
  methods: {
    async getStationList() {
      try {
        const { success, result, message } = await getAction('/gardsStations/findPage', {
          pageIndex: 1,
          pageSize: 10000
        })
        if (success) {
          this.stationList = result.records.map(record => ({ label: record.stationCode, value: record.stationId }))
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      }
    },

    async getDetectorList() {
      try {
        const { success, result, message } = await getAction('/gardsDetectors/findPage', {
          pageIndex: 1,
          pageSize: 10000
        })
        if (success) {
          this.detectorList = result.records.map(record => ({ label: record.detectorCode, value: record.detectorId }))
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      }
    },

    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    onDel() {
      if (this.selectedRowKeys && this.selectedRowKeys.length) {
        this.$confirm({
          title: 'Do You Want To Delete This Item?',
          okText: 'OK',
          cancelText: 'Cancel',
          onOk: () => {
            this.handleDelete(this.selectedRowKeys[0], 'sampleId')
          }
        })
      } else {
        this.$message.warn('Please Select An Item To Delete')
      }
    }
  },
  computed: {
    formItems() {
      return [
        {
          label: 'SampleID',
          type: 'a-input',
          name: 'sampleID',
          style: {
            width: '19%'
          }
        },
        {
          label: 'Station',
          type: 'custom-select',
          name: 'stationId',
          props: {
            options: [
              {
                label: 'ALL',
                value: ''
              },
              ...this.stationList
            ],
            showSearch: true,
            filterOption: this.filterOption
          },
          style: {
            width: '19%'
          }
        },
        {
          label: 'Detector',
          type: 'custom-select',
          name: 'detectorId',
          props: {
            options: [
              {
                label: 'ALL',
                value: ''
              },
              ...this.detectorList
            ],
            showSearch: true,
            filterOption: this.filterOption
          },
          style: {
            width: '19%'
          }
        },
        {
          label: 'From',
          type: 'a-date-picker',
          name: 'from',
          props: {
            showTime: { format: 'HH:mm' },
            format: 'YYYY-MM-DD HH:mm',
            style: {
              minWidth: 'auto'
            }
          },
          style: {
            width: '22%'
          }
        },
        {
          label: 'To',
          type: 'a-date-picker',
          name: 'to',
          props: {
            showTime: { format: 'HH:mm' },
            format: 'YYYY-MM-DD HH:mm',
            style: {
              minWidth: 'auto'
            }
          },
          style: {
            paddingRight: 0,
            width: '21%'
          }
        },
        {
          label: '',
          type: 'a-checkbox-group',
          props: {
            options: [
              { label: 'Collect Stop', value: 'collectStop' },
              { label: 'Acq.Start', value: 'acqDotStart' },
              { label: 'SampleData', value: 'sampleData' },
              { label: 'AutoResults', value: 'autoResults' },
              { label: 'Reviewed Results', value: 'reviewedResults' }
            ]
          },
          style: {
            width: '610px'
          }
        }
      ]
    }
  }
}
</script>

<style lang="less" scoped>
.btn-group {
  img {
    margin-right: 12px;
    height: 18px;
  }
}
</style>
