<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="统计日期">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              @input="onInput()"
              @change="mytoQuery"
            />
          </el-form-item>
          <el-form-item label="所属组织">
            <el-select
              v-model="query.org_id"
              clearable
              size="mini"
              placeholder="所属组织"
              multiple
              style="width: 400px"
              class="filter-item"
              @change="handTaskStatus"
            >
              <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" />
      <!--表格渲染-->
      <el-table
        ref="table"
        highlight-current-row
        v-loading="crud.loading"
        :row-class-name="tableRowClassName"
        :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 !== 'first'" :prop="col.prop" :label="col.label" width="120px" />
          <el-table-column v-if="col.prop === 'first'" :prop="col.prop" :label="col.label" width="140px" fixed>
            <template slot-scope="scope">
              <span v-show="!isshow(scope.row)">{{ scope.row.first }}</span>
              <el-link v-show="isshow(scope.row)" type="warning" @click="open2(scope.$index, scope.row)">{{ scope.row.first }}</el-link>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <div ref="topScroll" class="top-scroll" @scroll="handleScrollTop">
        <div class="top-scroll-content" :key="key" :style="{ width: topScrollWidth }" />
      </div>
      <el-table
        ref="dtl_table"
        :key="key"
        highlight-current-row
        v-loading="crud.loading"
        :row-class-name="tableRowClassName"
        :data="dtlList"
        size="mini"
        :max-height="400"
        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>
      <!--分页组件-->
      <StructIvt4 ref="child" :dialog-show.sync="structshow" :rowmst="form" />
      <StructIvt3 ref="child3" :dialog-show.sync="structshow3" :rowmst="form" />
      <StructIvt2 ref="child2" :dialog-show.sync="structshow2" :rowmst="form" />
    </div>
  </div>
</template>
<style>
/*顶部滚动条*/
.top-scroll {
  overflow-x: auto;
  overflow-y: hidden;
}
.top-scroll .top-scroll-content {
  background-color: #fff;
  height: 15px;
}
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
.el-table__body-wrapper {
  display: block;
}
::v-deep .el-table__body tr.current-row>td {
  color: #fff;
  background: #409EFF!important;
}
</style>
<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 StructIvt4 from '@/views/wms/statistics/materPlan/StructIvt4'
import StructIvt3 from '@/views/wms/statistics/materPlan/StructIvt3'
import StructIvt2 from '@/views/wms/statistics/materPlan/StructIvt2'
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'

export default {
  name: 'MaterPlanQuery',
  components: { crudOperation, rrOperation, StructIvt4, StructIvt2, StructIvt3 },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '粉料计划表',
      url: 'api/statistical/materPlanQuery',
      idField: '1',
      sort: '1,desc',
      query: {
        task_status: ''
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: true
      }
    })
  },
  data() {
    return {
      topScrollWidth: 10,
      cols: [],
      cols2: [],
      dtlList: [],
      form: {},
      structshow: false,
      structshow2: false,
      structshow3: false,
      Depts: [],
      query_flag: true,
      statusList: [],
      permission: {},
      rules: {},
      key: null
    }
  },
  beforeCreate() {
  },
  created() {
    workorder.getDepts().then(res => {
      this.Depts = res
    })
    this.crud.query.createTime = [new Date(), new Date().daysLater(30)]
  },
  methods: {
    handleScrollTop() {
      if (this.$refs.topScroll) {
        var scrollLeft = this.$refs.topScroll.scrollLeft
        var scrollTop = this.$refs.dtl_table.bodyWrapper.scrollTop
        this.$refs.dtl_table.bodyWrapper.scrollTo(scrollLeft, scrollTop)
        // this.refreshTable()
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      report.getHeader().then(res => {
        this.cols = res
      })
      report.getHeader2().then(res => {
        this.cols2 = res
      })
      if (this.query_flag) {
        this.crud.query.begin_time = (new Date()).strftime('%F', 'zh')
        this.crud.query.end_time = (new Date().daysLater(30)).strftime('%F', 'zh')
        this.query_flag = false
      }
      const data = this.crud.query
      report.materPlanDtlQuery(data).then(res => {
        this.dtlList = res.content
        this.topScrollWidth = this.$refs.dtl_table.bodyWrapper.scrollWidth + 'px'
        this.tableDom = this.$refs.dtl_table.bodyWrapper
        this.tableDom.addEventListener('scroll', () => {
          var scrollLeft = this.tableDom.scrollLeft
          this.$refs.topScroll.scrollTo(scrollLeft, 0)
        })
      })
      crudInspectionsheetmst.getStatus().then(res => {
        this.statusList = res
      })
      return true
    },
    handTaskStatus(value) {
      var str1 = ''
      if (value) {
        for (const item of value) {
          if (str1 === '') {
            str1 = item
          } else {
            str1 = str1 + ',' + item
          }
        }
        this.query.task_status = str1
      } else {
        this.query.task_status = ''
      }
    },
    open2(index, row) {
      if (row.first === '库存现存量') {
        this.structshow = true
      }
      if (row.first === '待检入库量') {
        this.structshow2 = true
      }
      if (row.first === '合同在途') {
        this.structshow3 = true
      }
    },
    isshow(row) {
      if (row.first === '库存现存量') {
        return true
      } else if (row.first === '待检入库量') {
        return true
      } else if (row.first === '合同在途') {
        return true
      } else {
        return false
      }
    },
    hand(value) {
      this.crud.toQuery()
    },
    onInput() {
      this.$forceUpdate()
    },
    mytoQuery(array1) {
      if (array1 === null) {
        this.crud.query.begin_time = ''
        this.crud.query.end_time = ''
      } else {
        this.crud.query.begin_time = array1[0]
        this.crud.query.end_time = array1[1]
      }
      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
        })
      }
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'success-row'
      }
      return ''
    },

    refreshTable() {
      this.key = Math.random()
    }
  }
}
</script>

<style scoped>

::v-deep .el-table__body tr.current-row>td {
  color: #fff;
  background: #409EFF!important;
}

</style>
