<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: 'left',
    width: 100,
    scopedSlots: {
      customRender: 'index'
    },
    customHeaderCell: () => {
      return {
        style: {
          'padding-left': '26px !important'
        }
      }
    },
    customCell: () => {
      return {
        style: {
          'padding-left': '26px !important'
        }
      }
    }
  },
  {
    title: 'SITE DET CODE',
    align: 'left',
    dataIndex: 'siteDetCode',
    width: 130
  },
  {
    title: 'SAMPLE ID',
    align: 'left',
    width: 100,
    dataIndex: 'sampleId'
  },
  {
    title: 'STATION',
    align: 'left',
    width: 100,
    dataIndex: 'stationName'
  },
  {
    title: 'DETECTOR',
    align: 'left',
    width: 100,
    dataIndex: 'detectorsName'
  },
  {
    title: 'SAMPLE TYPE',
    align: 'left',
    width: 120,
    dataIndex: 'sampleType'
  },
  {
    title: 'DATA TYPE',
    align: 'left',
    width: 100,
    dataIndex: 'dataType'
  },
  {
    title: 'GEOMETRY',
    align: 'left',
    width: 100,
    dataIndex: 'geometry'
  },
  {
    title: 'QUALIFIE',
    align: 'left',
    width: 100,
    dataIndex: 'spectralQualifie'
  },
  {
    title: 'TRANSMIT DTG',
    align: 'left',
    width: 150,
    dataIndex: 'transmitDtg'
  },
  {
    title: 'COLLECT START',
    align: 'left',
    width: 150,
    dataIndex: 'collectStart'
  },
  {
    title: 'COLLECT STOP',
    align: 'left',
    width: 150,
    dataIndex: 'collectStop'
  },
  {
    title: 'ACQ.START',
    align: 'left',
    width: 150,
    dataIndex: 'acquisitionStart'
  },
  {
    title: 'ACQ.STOP',
    align: 'left',
    width: 150,
    dataIndex: 'acquisitionStop'
  },
  {
    title: 'ACQ.REAL(S)',
    align: 'left',
    width: 120,
    dataIndex: 'acquisitionRealSec'
  },
  {
    title: 'ACQ.LIVE(S)',
    align: 'left',
    width: 100,
    dataIndex: 'acquisitionLiveSec'
  },
  {
    title: 'QUANTITY',
    align: 'left',
    width: 100,
    dataIndex: 'quantity'
  },
  {
    title: 'STATUS',
    align: 'left',
    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',
          props: {
            allowClear: true,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'Station',
          type: 'custom-select',
          name: 'stationId',
          props: {
            options: [
              {
                label: 'ALL',
                value: ''
              },
              ...this.stationList
            ],
            showSearch: true,
            filterOption: this.filterOption,
            allowClear: true,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'Detector',
          type: 'custom-select',
          name: 'detectorId',
          props: {
            options: [
              {
                label: 'ALL',
                value: ''
              },
              ...this.detectorList
            ],
            showSearch: true,
            filterOption: this.filterOption,
            allowClear: true,
            style: {
              width: '261px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'From',
          type: 'a-date-picker',
          name: 'collectStart',
          props: {
            showTime: { format: 'HH:mm' },
            format: 'YYYY-MM-DD HH:mm',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            style: {
              minWidth: 'auto',
              width: '350px'
            }
          },
          style: {
            width: 'auto'
          }
        },
        {
          label: 'To',
          type: 'a-date-picker',
          name: 'collectStop',
          props: {
            showTime: { format: 'HH:mm' },
            format: 'YYYY-MM-DD HH:mm',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            style: {
              minWidth: 'auto',
              width: '350px'
            }
          },
          style: {
            paddingRight: 0,
            width: 'auto'
          }
        },
        {
          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>
