<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-input
              v-model="query.mol_code"
              size="mini"
              clearable
              placeholder="单据编码"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="货位编码">
            <el-input
              v-model="query.struct_code"
              size="mini"
              clearable
              placeholder="货位编码"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <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-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>
          <el-form-item label="损溢类型">
            <el-select
              v-model="query.mol_type"
              clearable
              size="mini"
              placeholder="损溢类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_MOL_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单据类型">
            <el-select
              v-model="query.mol_inv_type"
              clearable
              size="mini"
              placeholder="单据类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.MOL_INV_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="单据状态">
            <el-select
              v-model="query.status"
              clearable
              size="mini"
              placeholder="单据状态"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.MOL_BILL_STATUS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <rrOperation />
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          :disabled="confirm_flag"
          icon="el-icon-check"
          size="mini"
          @click="confirm"
        >
          强制确认
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :max-height="590"
        size="mini"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
        @current-change="handleCurrentChange"
        @select="handleSelectionChange"
        @select-all="onSelectAll"
      >
        <el-table-column
          label="操作"
          width="115"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="canUd(scope.row)"
              :disabled-dle="canUd(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column :selectable="checkboxT" type="selection" width="55" />
        <el-table-column  prop="mol_code" label="损溢单号" :min-width="flexWidth('mol_code',crud.data,'损溢单号')">
          <template slot-scope="scope">
            <el-link type="warning" @click="crud.toView(scope.row)">{{ scope.row.mol_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column :formatter="billTypeFormat" prop="mol_inv_type" label="单据类型" :min-width="flexWidth('mol_inv_type',crud.data,'单据类型')" />
        <el-table-column :formatter="typeFormat" prop="mol_type" label="损溢类型" :min-width="flexWidth('mol_type',crud.data,'损溢类型')" />
        <el-table-column label="明细数" align="center" prop="dtl_num" :min-width="flexWidth('dtl_num',crud.data,'明细数')" />
        <el-table-column label="损益重量" align="center" prop="total_qty" :min-width="flexWidth('total_qty',crud.data,'损益重量')" >
          <template slot-scope="scope">
            {{ fun(scope.row.total_qty) }}
          </template>
        </el-table-column>
        <el-table-column :formatter="stateFormat" prop="status" label="单据状态" :min-width="flexWidth('status',crud.data,'单据状态')" />
        <el-table-column prop="create_mode" :formatter="create_modeFormat" label="生成方式" :min-width="flexWidth('create_mode',crud.data,'生成方式')" />
        <el-table-column label="制单人" align="center" prop="input_optname" :min-width="flexWidth('input_optname',crud.data,'制单人')" />
        <el-table-column label="制单时间" align="center" prop="input_time" :min-width="flexWidth('input_time',crud.data,'制单时间')" />
        <el-table-column label="确认人" align="center" prop="confirm_optname" :min-width="flexWidth('confirm_optname',crud.data,'确认人')" />
        <el-table-column label="确认时间" align="center" prop="confirm_time" :min-width="flexWidth('confirm_time',crud.data,'确认时间')" />
        <el-table-column label="备注" align="center" prop="remark" :min-width="flexWidth('remark',crud.data,'备注')" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog @AddChanged="querytable" />
  </div>
</template>

<script>
import crudMoreOrless from '@/views/wms/st/moreorlessbill/moreorless'
import CRUD, { crud, header, presenter } 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/index'
import AddDialog from '@/views/wms/st/moreorlessbill/AddDialog'
import { mapGetters } from 'vuex'
import crudStorattr from '@/views/wms/basedata/bsrealstorattr/bsrealstorattr'

export default {
  name: 'MoreOrLess',
  components: { AddDialog, crudOperation, rrOperation, udOperation, pagination, DateRangePicker },
  cruds() {
    return CRUD({
      title: '',
      optShow: { add: true, reset: true },
      props: {
        // 每页数据条数
        size: 20
      },
      idField: 'mol_id',
      url: '/api/moreorLess',
      crudMethod: { ...crudMoreOrless }
    })
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['MOL_BILL_STATUS', 'ST_CREATE_MODE', 'ST_MOL_TYPE', 'MOL_INV_TYPE'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      },
      confirm_flag: true,
      currentRow: null,
      storlist: [],
      statuslist: []
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudStorattr.getStor({}).then(res => {
      this.storlist = res
    })
  },
  methods: {
    fun(val) {
      return Number(val).toFixed(3)
    },
    canUd(row) {
      return row.status !== '10'
    },
    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
        if (current.bill_status !== '99') {
          this.confirm_flag = false
        } else {
          this.confirm_flag = true
        }
      }
    }
    ,
    querytable() {
      this.onSelectAll()
      this.crud.toQuery()
      this.handleCurrentChange(null)
    },
    handleCurrentChange(currentRow) {
      if (currentRow === null) {
        this.currentRow = {}
        this.confirm_flag = true
      }
    },
    stateFormat(row, column) {
      return this.dict.label.MOL_BILL_STATUS[row.status]
    },
    typeFormat(row, column) {
      return this.dict.label.ST_MOL_TYPE[row.mol_type]
    },
    billTypeFormat(row, column) {
      return this.dict.label.MOL_INV_TYPE[row.mol_inv_type]
    },
    create_modeFormat(row, column) {
      return this.dict.label.ST_CREATE_MODE[row.create_mode]
    },
    checkboxT(row) {
      return row.status !== '99'
    },
    confirm() {
      if (!this.currentRow) {
        this.crud.notify('请选择一条单据', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      crudMoreOrless.confirm(this.currentRow).then(res => {
        this.crud.notify('单据确认成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.refresh()
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
