<template>
  <div>
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row style="margin-top: 10px;">
          <!--查询条件-->
          <Col span="24">
          <div class="fl">
            <Form id="search-form" :label-width="70" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item label="委托日期:">
                <el-date-picker
                  v-model="queryDate"
                  type="monthrange"
                  range-separator="-"
                  start-placeholder="开始月份"
                  end-placeholder="结束月份"
                  size="small"
                  value-format="yyyy-MM"
                  @change="_dateChange"
                >
                </el-date-picker>
              </Form-item>
              <Form-item class="search-btn">
                <Button type="primary" class="width-80" @click="_getData">
                  搜索
                </Button>
              </Form-item>
            </Form>
          </div>
          <div class="clear"></div>
          </Col>
          <!--内容-->
          <Col span="24">
          <Row :gutter="16">
            <Col span="13">
            <element-table
              id="table-year"
              ref="pageTable"
              :table-height="tableHeight"
              :get-page="getPage"
              hide-checkbox
              @on-result-change="_tableResultChange"
            >
              <el-table-column fixed type="index" label="序号" width="100">
              </el-table-column>
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <div v-if="item.detail">
                    <a @click.stop="_detailModal(scope.row)">{{ scope.row[item.key] }}</a>
                  </div>
                  <span v-else>{{ scope.row[item.key] }}</span>
                </template>
              </el-table-column>
            </element-table>
            </Col>
            <Col span="11">
            <!--pie-->
            <UserTestPie ref="testPieModal"></UserTestPie>
            </Col>
          </Row>
          </Col>
        </Row>
      </div>
    </div>
    <!--检测项目详情-->
    <!--    <Detail ref="detailModal"></Detail>-->
  </div>
</template>

<script>
// eslint-disable-next-line import/named
import { soilStatistics } from '../../../api'
import UserTestPie from './UserTestPie'

export default {
  components: {
    UserTestPie
  },
  data() {
    const date = this.$dateformat(new Date(), 'yyyy-mm')
    const nextMonth = this.$dateformat(
      new Date().setMonth(new Date().getMonth() + 1),
      'yyyy-mm'
    )
    return {
      beginDate: new Date(),
      endDate: new Date(),
      groupId: '',
      groupName: '',
      tester: '',
      testerId: '',
      pageColumns: [
        { title: '检测人员', key: 'tester', detail: true, width: 140 },
        { title: '项目总量', key: 'total', width: 100 },
        { title: '未完成总量', key: 'doingTotal', width: 110 },
        { title: '已完成总量', key: 'endTotal', width: 110 },
        { title: '样品占比(%)', key: 'ratio', width: 120 }
      ],
      groupData: [],
      getPage: {},
      tempData: {
        groupId: '',
        groupName: '',
        beginDate: date,
        endDate: nextMonth
      },
      formObj: {
        edateBegin: undefined,
        edateEnd: undefined
      },
      testerData: [],
      queryDate: [date, nextMonth],
      serviceType: '',
      serviceTypeList: []
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('search')
    }
  },
  mounted() {
    this.$refs.pageTable._hideLoading()
    const str = this.tempData.endDate
    const arr = str.split('-')
    const lastday = this.mGetDate(arr[0], arr[1]).getTime() - 1
    this.formObj.edateBegin = this.tempData.beginDate
    this.formObj.edateEnd = this.$dateformat(lastday, 'yyyy-mm-dd HH:MM:ss')
    this._getData()
  },
  methods: {
    mGetDate(year, month) {
      const d = new Date(year, month, 1)
      return d
    },
    // 服务类型
    _getData: async function() {
      const result = await soilStatistics.pagePersonalTaskStatistics(
        this.formObj
      )
      if (result) {
        this.getPage = result
        this.$refs.testPieModal._openPie(this.getPage.records)
      }
    },
    // 检测项目详情
    _detailModal(data) {
      const obj = {
        testerIds: [data.testerId],
        groupIds: this.groupIds,
        beginDate: this.tempData.beginDate,
        endDate: this.tempData.endDate
      }
      console.log(obj)
      // this.$refs.detailModal._open(obj)
    },
    _dateChange(data) {
      if (data) {
        this.tempData.beginDate = data[0]
        this.tempData.endDate = data[1]
        this.formObj.edateBegin = data[0]
        const str = data[1]
        const arr = str.split('-')
        const lastday = this.mGetDate(arr[0], arr[1]).getTime() - 1
        this.formObj.edateEnd = this.$dateformat(lastday, 'yyyy-mm-dd HH:MM:ss')
      } else {
        this.tempData.beginDate = ''
        this.tempData.endDate = ''
        this.formObj.edateBegin = undefined
        this.formObj.edateEnd = undefined
      }
    },
    _contHide() {
      this.tempData.beginDate = this.$dateformat(new Date(), 'yyyy-mm')
      this.tempData.endDate = this.$dateformat(new Date(), 'yyyy-mm')
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _search() {
      this.tempData.groupId = this.groupId
      this.tempData.groupName = this.groupId ? this.groupName : ''
      this.tempData.serviceType = this.serviceType ? this.serviceType : ''
      this.$refs.testPieModal._openPie(this.tempData)
      this._formSearch()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'changeSize':
          break
      }
    },
    // 参数
    _searchParams() {
      const data = {}
      if (this.groupId) {
        data.groupId = this.groupId
      }
      if (this.tester && this.testerId) {
        data.testerId = this.testerId
      } else {
        this.testerId = ''
      }
      if (this.tempData.beginDate) {
        data.beginDate = this.tempData.beginDate
      }
      if (this.tempData.endDate) {
        data.endDate = this.tempData.endDate
      }
      return this.$extend(data, { page: 1, rows: 5000 })
    }
  }
}
</script>
