<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="90px"
          label-suffix=":"
        >
          <el-form-item label="所属仓库">
            <el-select
              v-model="query.stor_id"
              clearable
              size="mini"
              placeholder="全部"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in storlist"
                :key="item.stor_id"
                :label="item.stor_name"
                :value="item.stor_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出入类型">
            <el-select
              v-model="query.io_type"
              clearable
              size="mini"
              placeholder="出入类型"
              class="filter-item"
              @change="ioTypeChange"
            >
              <el-option
                v-for="item in dict.io_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="业务类型">
            <el-select
              v-model="query.bill_type"
              clearable
              filterable
              size="mini"
              placeholder="业务类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in billtypelist"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="单据状态">
            <el-select
              v-model="query.bill_status"
              clearable
              size="mini"
              placeholder="单据状态"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.io_bill_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出入库单号">
            <el-input
              v-model="query.bill_code"
              size="mini"
              clearable
              placeholder="出入库单号"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="是否回传">
            <el-select
              v-model="query.is_upload"
              clearable
              size="mini"
              placeholder="是否回传"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.is_upload"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料">
            <el-input
              v-model="query.material_code"
              size="mini"
              clearable
              placeholder="物料编码或名称模糊查询"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="批次">
            <el-input
              v-model="query.pcsn"
              size="mini"
              clearable
              placeholder="批次模糊查询"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="品质类型">
            <el-select
              v-model="query.quality_scode"
              clearable
              size="mini"
              placeholder="全部"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_QUALITY_SCODE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <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']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="dis_flag"
          @click="divOpen"
        >
          单据明细
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          :disabled="work_flag"
          icon="el-icon-position"
          size="mini"
          @click="taskOpen"
        >
          作业明细
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        size="mini"
        :data="crud.data"
        style="width: 100%;"
        :highlight-current-row="true"
        @selection-change="crud.selectionChangeHandler"
        @current-change="handleCurrentChange"
        @select="handleSelectionChange"
        @select-all="onSelectAll"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column show-overflow-tooltip prop="bill_code" width="130" label="订单编码" />
        <el-table-column show-overflow-tooltip :formatter="stateFormat" width="100" prop="bill_status" label="单据状态" />
        <el-table-column prop="stor_name" label="仓库" width="100" />
        <el-table-column prop="io_type" label="出入库类型" :formatter="ioFormat" min-width="100" show-overflow-tooltip/>
        <el-table-column show-overflow-tooltip prop="bill_type" :formatter="bill_typeFormat" min-width="120" label="业务类型" />
        <el-table-column show-overflow-tooltip width="135" prop="biz_date" label="业务日期" />
        <el-table-column show-overflow-tooltip :formatter="create_modeFormat" prop="create_mode" label="生成方式" width="100" />
        <el-table-column label="明细数" align="center" prop="detail_count" width="100" />
        <el-table-column label="总重量" align="center" prop="total_qty">
          <template slot-scope="scope">
            {{ fun(scope.row.total_qty) }}
          </template>
        </el-table-column>
        <el-table-column label="制单人" align="center" prop="input_optname" />
        <el-table-column label="制单时间" align="center" prop="input_time" width="150" />
        <el-table-column label="修改人" align="center" prop="update_optname" />
        <el-table-column label="修改时间" align="center" prop="update_time" width="150" />
        <el-table-column label="确认人" align="center" prop="confirm_optname" width="150" />
        <el-table-column label="确认时间" align="center" prop="confirm_time" width="150" />
        <el-table-column label="回传人" align="center" prop="upload_optname" width="150" />
        <el-table-column label="回传时间" align="center" prop="upload_time" width="150" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <DivDialog :dialog-show.sync="divShow" :open-array="openParam" :rowmst="mstrow" @DivChanged="querytable" />
    <TaskDialog :dialog-show.sync="taskShow" :open-array="openParam" :rowmst="mstrow" @TaskChanged="querytable" />
  </div>
</template>

<script>
import checkoutbill from '@/api/wms/st/core/outbill/checkoutbill'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import DivDialog from '@/views/wms/statistics/ioStorQuery/DivDialog'
import TaskDialog from '@/views/wms/statistics/ioStorQuery/TaskDialog'
import crudStorattr from '@/api/wms/basedata/st/storattr'
import crudRawAssist from '@/api/wms/st/core/inbill/rawassist'

export default {
  name: 'ioStorQuery',
  components: { crudOperation, rrOperation, pagination, TaskDialog, DivDialog },
  cruds() {
    return CRUD({ title: '出入库单报表', idField: 'iostorinv_id', url: 'api/checkoutbill', crudMethod: { ...checkoutbill },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      }})
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['ST_QUALITY_SCODE','io_bill_status', 'ST_CREATE_MODE', 'io_type', 'is_upload'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {
      },
      divShow: false,
      taskShow: false,
      dis_flag: true,
      work_flag: true,
      openParam: [],
      mstrow: {},
      currentRow: null,
      checkrows: [],
      storlist: [],
      billtypelist: []
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudStorattr.getStor({ 'is_materialstore': '1' }).then(res => {
      this.storlist = res.content
    })
    checkoutbill.getType().then(res => {
      this.billtypelist = res
    })
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.handleCurrentChange(null)
    },
    fun(val) {
      return Number(val).toFixed(3)
    },
    ioFormat(row, column) {
      return this.dict.label.io_type[row.io_type]
    },
    handleSelectionChange(val, row) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val.pop())
        this.buttonChange(row)
      } else if (val.length === 1) {
        this.buttonChange(row)
      } else {
        this.handleCurrentChange(null)
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
      this.handleCurrentChange(null)
    },
    buttonChange(current) {
      if (current !== null) {
        this.currentRow = current
        this.dis_flag = false
        this.work_flag = false
      }
    },
    ioTypeChange(value) {
      if (value === '1') {
        crudRawAssist.getType({ 'io_code': '', 'io_flag': '01' }).then(res => {
          this.billtypelist = res
        })
      } else if (value === '0') {
        crudRawAssist.getType({ 'io_code': '', 'io_flag': '00' }).then(res => {
          this.billtypelist = res
        })
      } else {
        checkoutbill.getType().then(res => {
          this.billtypelist = res
        })
      }
      this.crud.toQuery()
    },
    stateFormat(row) {
      return this.dict.label.io_bill_status[row.bill_status]
    },
    bill_typeFormat(row) {
      for (const item of this.billtypelist) {
        if (item.code === row.bill_type) {
          return item.name
        }
      }
    },
    create_modeFormat(row) {
      return this.dict.label.ST_CREATE_MODE[row.create_mode]
    },
    handleCurrentChange(current) {
      if (current === null) {
        this.dis_flag = true
        this.work_flag = true
        this.currentRow = {}
      }
    },
    divOpen() {
      checkoutbill.getOutBillDtl({ 'iostorinv_id': this.currentRow.iostorinv_id }).then(res => {
        this.openParam = res
        this.divShow = true
        this.mstrow = this.currentRow
      })
    },
    taskOpen() {
      checkoutbill.getOutBillDis({ 'iostorinv_id': this.currentRow.iostorinv_id }).then(res => {
        this.openParam = res
        this.taskShow = true
        this.mstrow = this.currentRow
      })
    },
    querytable() {
      this.crud.toQuery()
      this.handleCurrentChange(null)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
