<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.control_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="创建时间" prop="createTime">
            <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.bill_type"
              clearable
              size="mini"
              placeholder="管控类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.CONTORL_TYPE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="木箱号">
            <el-input
              v-model="query.box_no"
              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>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addDialog('0')"
        >
          新增管控
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="untieDialog('1')"
        >
          库存解绑
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="outDialog('2')"
        >
          管控出库
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        size="mini"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column prop="control_code" label="单据号" :min-width="flexWidth('control_code',crud.data,'单据号')" >
          <template slot-scope="scope">
            <el-link type="warning" @click="toView(scope.$index, scope.row)">{{ scope.row.control_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="stor_name" label="仓库" :min-width="flexWidth('stor_name',crud.data,'仓库')" />
        <el-table-column prop="bill_type" label="管控类型" :min-width="flexWidth('bill_type',crud.data,'管控类型')">
          <template slot-scope="scope">
            {{ dict.label.CONTORL_TYPE[scope.row.bill_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="biz_date" label="业务日期" :min-width="flexWidth('biz_date',crud.data,'业务日期')" />
        <el-table-column prop="detail_count" label="明细数" :min-width="flexWidth('detail_count',crud.data,'明细数')" />
        <el-table-column prop="total_qty" label="总重量" :formatter="crud.formatNum3" :min-width="flexWidth('total_qty',crud.data,'总重量')" />
        <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',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>
      <!--分页组件-->
      <pagination />
    </div>
    <AddDialog :dialog-show.sync="addDialogShow" :open-type="open_type" :lock-type="lock_type" :bill-type="bill_type" />
    <ViewDialog :dialog-show.sync="viewDialogShow" :mst-row="mstRow" :table-data="tableDataDtl" />
  </div>
</template>

<script>
import crudControl from '@/views/wms/control/ivtcontrol/control'
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 crudUserStor from '@/views/wms/basedata/st/userStor/userStor'
import AddDialog from '@/views/wms/control/ivtcontrol/AddDialog'
import ViewDialog from '@/views/wms/control/ivtcontrol/ViewDialog'

export default {
  name: 'ControlMst',
  components: { crudOperation, rrOperation, udOperation, pagination, crudUserStor, AddDialog, ViewDialog },
  cruds() {
    return CRUD({
      title: '库存管控',
      optShow: { add: false, reset: true },
      idField: 'control_id',
      url: '/api/control',
      crudMethod: { ...crudControl }
    })
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['CONTORL_TYPE'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {
        add: ['admin', 'inbill:add'],
        edit: ['admin', 'inbill:edit'],
        del: ['admin', 'inbill:del']
      },
      storlist: [],
      tableDataDtl: [],
      open_type: null,
      lock_type: null,
      bill_type: null,
      mstRow: null,
      addDialogShow: false,
      viewDialogShow: false
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudUserStor.getUserStor().then(res => {
      this.storlist = res
    })
    this.initQuery()
  },
  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()
    },
    addDialog(type) {
      this.open_type = type
      this.addDialogShow = true
      this.lock_type = '1'
      this.bill_type = '0'
    },
    untieDialog(type) {
      this.open_type = type
      this.addDialogShow = true
      this.lock_type = '88'
      this.bill_type = '1'
    },
    outDialog(type) {
      this.open_type = type
      this.addDialogShow = true
      this.lock_type = '88'
      this.bill_type = '2'
    },
    toView(index, row) {
      this.mstRow = row
      this.viewDialogShow = true
      crudControl.queryDtl({ 'control_id': row.control_id }).then(res => {
        this.tableDataDtl = res
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
