<template>
  <div>
    <search-form :items="formItems" v-model="queryParam" @search="searchQuery">
      <div class="btn-group" slot="additional">
        <div>
          <a-checkbox v-model="delParams.sampleData"> SampleData </a-checkbox>
          <a-checkbox v-model="delParams.rnAuto" @change="handleCheckboxChange('rnAuto')"> AutoResults </a-checkbox>
          <a-checkbox v-model="delParams.rnMan" @change="handleCheckboxChange('rnMan')"> Reviewed Results </a-checkbox>
        </div>
        <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 - 415px)' }"
    >
      <template slot="index" slot-scope="{ index }">
        {{ index + 1 }}
      </template>
    </custom-table>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction } from '../../api/manage'
import dateFormat from '@/components/jeecg/JEasyCron/format-date'

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: 150,
  },
  {
    title: 'SAMPLE ID',
    align: 'left',
    width: 120,
    dataIndex: 'sampleId',
  },
  {
    title: 'STATION',
    align: 'left',
    width: 100,
    dataIndex: 'stationName',
  },
  {
    title: 'DETECTOR',
    align: 'left',
    width: 120,
    dataIndex: 'detectorsName',
  },
  {
    title: 'SAMPLE TYPE',
    align: 'left',
    width: 140,
    dataIndex: 'sampleType',
  },
  {
    title: 'DATA TYPE',
    align: 'left',
    width: 120,
    dataIndex: 'dataType',
  },
  {
    title: 'GEOMETRY',
    align: 'left',
    width: 160,
    dataIndex: 'geometry',
    ellipsis: true,
  },
  {
    title: 'QUALIFIE',
    align: 'left',
    width: 100,
    dataIndex: 'spectralQualifie',
  },
  {
    title: 'TRANSMIT DTG',
    align: 'left',
    width: 180,
    dataIndex: 'transmitDtg',
  },
  {
    title: 'COLLECT START',
    align: 'left',
    width: 180,
    dataIndex: 'collectStart',
  },
  {
    title: 'COLLECT STOP',
    align: 'left',
    width: 180,
    dataIndex: 'collectStop',
  },
  {
    title: 'ACQ.START',
    align: 'left',
    width: 180,
    dataIndex: 'acquisitionStart',
  },
  {
    title: 'ACQ.STOP',
    align: 'left',
    width: 180,
    dataIndex: 'acquisitionStop',
  },
  {
    title: 'ACQ.REAL(S)',
    align: 'left',
    width: 120,
    dataIndex: 'acquisitionRealSec',
  },
  {
    title: 'ACQ.LIVE(S)',
    align: 'left',
    width: 140,
    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: {
        collectStart: this.getBeforeDate(6),
        collectStop: dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
      },
      delParams: {
        sampleData: true,
        rnAuto: true,
        rnMan: true,
      },
      url: {
        list: '/gardsSampleData/findPage',
      },
      stationList: [],
      detectorList: [],
    }
  },
  created() {
    // this.queryParam.collectStart = this.getBeforeDate(6)
    this.getStationList()
    this.getDetectorList()
  },
  methods: {
    // 获取n天前的日期
    getBeforeDate(n) {
      var n = n
      var d = new Date()
      var year = d.getFullYear()
      var mon = d.getMonth() + 1
      var day = d.getDate()
      if (day <= n) {
        if (mon > 1) {
          mon = mon - 1
        } else {
          year = year - 1
          mon = 12
        }
      }
      d.setDate(d.getDate() - n)
      year = d.getFullYear()
      mon = d.getMonth() + 1
      day = d.getDate()
      var s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day) + ' 00:00:00'
      return s
    },
    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
    },

    handleCheckboxChange(name) {
      if (!this.delParams[name]) {
        this.delParams.sampleData = false
      }
    },

    onDel() {
      if (this.selectedRowKeys && this.selectedRowKeys.length) {
        this.$confirm({
          title: 'Do You Want To Delete This Item?',
          okText: 'OK',
          cancelText: 'Cancel',
          onOk: async () => {
            try {
              const { success, message } = await deleteAction('/gardsSampleData/deleteById', {
                sampleId: this.selectedRowKeys[0],
                ...this.delParams,
              })
              if (success) {
                this.$message.success('Delete Success!')
              } else {
                this.$message.error(message)
              }
            } catch (error) {
              console.error(error)
            }
          },
        })
      } 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: '187px',
            },
          },
          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: '187px',
            },
          },
          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: '187px',
            },
          },
          style: {
            width: 'auto',
          },
        },
        {
          label: 'From',
          type: 'custom-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: '279px',
            },
          },
          style: {
            width: 'auto',
          },
        },
        {
          label: 'To',
          type: 'custom-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: '279px',
            },
          },
          style: {
            width: 'auto',
          },
        },
        {
          type: 'a-checkbox',
          name: 'collectStopCheck',
          innerLabel: 'Collect Stop',
          style: {
            width: '132px',
          },
        },
        {
          type: 'a-checkbox',
          name: 'acqDotStartCheck',
          innerLabel: 'Acq.Start',
          style: {
            width: '113px',
          },
        },
      ]
    },
  },
  watch: {
    'delParams.sampleData': {
      handler(newVal) {
        if (newVal) {
          this.delParams.rnAuto = true
          this.delParams.rnMan = true
        }
      },
    },
  },
}
</script>

<style lang="less" scoped>
.btn-group {
  display: flex;
  align-items: center;
  margin-left: 10px;
  margin-bottom: 8px;

  img {
    margin-right: 12px;
    height: 18px;
  }
}
</style>
