<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.bill_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-input
              v-model="query.material_code"
              size="mini"
              clearable
              placeholder="物料编码"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="生成方式">
            <el-select
              v-model="query.create_mode"
              clearable
              size="mini"
              placeholder="生成方式"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_CREATE_MODE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </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.move_bill_status"
                :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 dict.ST_INV_TYPE_MV"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <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>
          <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"
        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
          v-permission="['admin','movestor:del','movestor:edit']"
          label="操作"
          width="160"
          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" />
        <el-table-column prop="bill_code" label="单据号" :min-width="flexWidth('bill_code',crud.data,'单据号')">
          <template slot-scope="scope">
            <el-link type="warning" @click="crud.toView(scope.row)">{{ scope.row.bill_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column :formatter="stateFormat" prop="bill_status" label="单据状态" :min-width="flexWidth('bill_status',crud.data,'单据状态')"/>
        <el-table-column prop="stor_name" label="仓库" :min-width="flexWidth('stor_name',crud.data,'仓库')" />
        <el-table-column prop="bill_type" :formatter="bill_typeFormat" label="业务类型" :min-width="flexWidth('bill_type',crud.data,'业务类型')"/>
        <el-table-column prop="biz_date" label="业务日期" :min-width="flexWidth('biz_date',crud.data,'业务日期')" />
        <el-table-column :formatter="create_modeFormat" prop="create_mode" label="生成方式" :min-width="flexWidth('create_mode',crud.data,'生成方式')"/>
        <el-table-column label="明细数" prop="detail_count" :min-width="flexWidth('detail_count',crud.data,'明细数')" />
        <el-table-column prop="input_optname" label="创建人" :min-width="flexWidth('input_optname',crud.data,'创建人')" />
        <el-table-column prop="input_time" label="创建日期" :min-width="flexWidth('input_time',crud.data,'创建日期')" />
        <el-table-column prop="update_optname" label="修改人" :min-width="flexWidth('update_optname',crud.data,'修改人')" />
        <el-table-column prop="update_time" label="修改日期" :min-width="flexWidth('update_time',crud.data,'修改日期')" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog @AddChanged="querytable" />
  </div>
</template>

<script>
import movestor from '@/views/wms/st/movebill/movestor'
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 AddDialog from '@/views/wms/st/movebill/AddDialog'
import crudStorattr from '@/views/wms/basedata/bsrealstorattr/bsrealstorattr'

export default {
  name: 'Movestor',
  components: { AddDialog, crudOperation, rrOperation, udOperation, pagination },
  cruds() {
    return CRUD({ title: '移库单', idField: 'moveinv_id', url: 'api/moveStor', crudMethod: { ...movestor },
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: true,
        download: false
      }})
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['move_bill_status', 'ST_CREATE_MODE', 'ST_INV_TYPE_MV'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {
        add: ['admin', 'movestor:add'],
        edit: ['admin', 'movestor:edit'],
        del: ['admin', 'movestor:del']
      },
      work_flag: true,
      confirm_flag: true,
      mstrow: {},
      currentRow: null,
      storlist: []
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudStorattr.getStor({}).then(res => {
      this.storlist = res
    })
    this.initQuery()
    this.crud.toQuery()
  },
  methods: {
    initQuery() {
      const end = new Date()
      const start = new Date()
      const endYear = end.getFullYear()
      var endMonth = end.getMonth() + 1
      if (end.getMonth() + 1 < 10) {
        endMonth = '0' + endMonth.toString()
      }
      var endDay = end.getDate()
      if (end.getDate() < 10) {
        endDay = '0' + endDay.toString()
      }
      const endDate = endYear + '-' + endMonth + '-' + endDay + ' 23:59:59'
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      const startYear = start.getFullYear()
      var startMonth = start.getMonth() + 1
      if (start.getMonth() + 1 < 10) {
        startMonth = '0' + startMonth.toString()
      }
      var startDay = start.getDate()
      if (start.getDate() < 10) {
        startDay = '0' + startDay.toString()
      }
      const startDate = startYear + '-' + startMonth + '-' + startDay + ' 00:00:00'
      this.$set(this.query, 'createTime', [startDate, endDate])
      this.crud.toQuery()
    },
    canUd(row) {
      return row.bill_status !== '10'
    },
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query.buss_type = ''
      this.handleCurrentChange(null)
    },
    stateFormat(row) {
      return this.dict.label.move_bill_status[row.bill_status]
    },
    bill_typeFormat(row) {
      return this.dict.label.ST_INV_TYPE_MV[row.bill_type]
    },
    create_modeFormat(row) {
      return this.dict.label.ST_CREATE_MODE[row.create_mode]
    },
    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 === '10') {
          this.work_flag = false
        } else {
          this.work_flag = true
        }
        if (current.bill_status !== '99') {
          this.confirm_flag = false
        } else {
          this.confirm_flag = true
        }
      }
    },
    handleCurrentChange(current) {
      if (current === null) {
        this.confirm_flag = true
        this.work_flag = true
        this.currentRow = {}
      }
    },
    checkboxT(row) {
      return row.bill_status !== '99'
    },
    confirm() {
      movestor.confirm({ 'moveinv_id': this.currentRow.moveinv_id }).then(res => {
        this.crud.notify('强制确认成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.querytable()
      })
    },
    querytable() {
      this.onSelectAll()
      this.crud.toQuery()
      this.handleCurrentChange(null)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
