<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="统计日期">
            <date-range-picker v-model="query.createTime" class="date-item"/>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-select
              v-model="query.org_id"
              clearable
              size="mini"
              placeholder="所属组织"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in Depts"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <rrOperation/>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        :max-height="590"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="index" label="序号" width="100" align="center" fixed/>
        <template v-for="(col,index) in cols">
          <el-table-column v-if="col.prop !== '1'" :prop="col.prop" :label="col.label" width="120px" show-overflow-tooltip/>
          <el-table-column v-if="col.prop === '1'" :prop="col.prop" :label="col.label" width="120px" show-overflow-tooltip fixed/>
        </template>
      </el-table>

      <el-table
        ref="dtl_table"
        v-loading="crud.loading"
        :data="dtlList"
        size="mini"
        :max-height="590"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="index" label="序号" width="100" align="center" fixed/>
        <template v-for="(col,index) in cols2">
          <el-table-column v-if="!col.option" :prop="col.prop" :label="col.label" width="120px" />
          <el-table-column v-if="col.option" :prop="col.prop" :label="col.label" width="120px" fixed/>
        </template>
      </el-table>
      <!--分页组件-->
    </div>
  </div>
</template>

<script>
import report from '@/api/wms/statistics/report'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import crudInspectionsheetmst from '@/api/wms/ql/inspectionsheetmst'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'
import Date from '@/utils/datetime'
import workorder from '@/api/wms/pdm/workorder'

const start = new Date()
export default {
  name: 'MaterPlanQuery',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '粉料计划表',
      url: 'api/statistical/materPlanQuery',
      idField: '1',
      sort: '1,desc',
      query: {
        createTime: [new Date(), start.daysLater(30)]
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      cols: [],
      cols2: [],
      dtlList: [],
      Depts: [],
      statusList: [],
      permission: {},
      rules: {}
    }
  },
  beforeCreate() {
  },
  created() {
    workorder.getDepts().then(res => {
      this.Depts = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      debugger
      report.getHeader().then(res => {
        this.cols = res
      })
      report.getHeader2().then(res => {
        this.cols2 = res
      })
      if (this.crud.query.createTime) {
        this.crud.query.begin_time = this.crud.query.createTime[0]
        this.crud.query.end_time = this.crud.query.createTime[1]
      }
      const data = this.crud.query
      report.materPlanDtlQuery(data).then(res => {
        this.dtlList = res.content
      })
      crudInspectionsheetmst.getStatus().then(res => {
        this.statusList = res
      })
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    downdtl() {
      if (this.currentRow !== null) {
        crud.downloadLoading = true
        download('/api/rawUatWcQuery/download', this.crud.query).then(result => {
          downloadFile(result, '原材料碳化钨', 'xlsx')
          crud.downloadLoading = false
        }).catch(() => {
          crud.downloadLoading = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
