<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="$t('common.inputCodeOrName')">
            <el-input
              v-model="query.bill_code"
              clearable
              size="mini"
              :placeholder="$t('common.in_code')"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('common.stor_name')">
            <el-select
              v-model="query.stor_id"
              clearable
              size="mini"
              :placeholder="$t('common.Please_select')"
              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="$t('common.create_time')" prop="createTime">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="$t('common.startDate')"
              :end-placeholder="$t('common.endDate')"
              :default-time="['00:00:00', '23:59:59']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('common.create_mode')">
            <el-select
              v-model="query.create_mode"
              clearable
              size="mini"
              :placeholder="$t('common.create_mode')"
              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="$t('common.bill_type')">
            <el-select
              v-model="query.bill_type"
              clearable
              filterable
              size="mini"
              :placeholder="$t('common.bill_type')"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_INV_IN_TYPE"
                :key="item.value"
                :disabled="item.value !== '0002'"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('common.container_name')">
            <el-input
              v-model="query.pcsn"
              clearable
              size="mini"
              :placeholder="$t('common.container_name')"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('common.pcsn')">
            <el-input
              v-model="query.sap_pcsn"
              clearable
              size="mini"
              :placeholder="$t('common.pcsn')"
              @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="success"
          :disabled="crud.selections.length !== 1"
          icon="el-icon-check"
          size="mini"
          @click="charge"
        >
          冲销
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column show-overflow-tooltip prop="bill_code" width="130" :label="$t('common.bill_code')">
          <template slot-scope="scope">
            <el-link type="warning" @click="toView(scope.$index, scope.row)">{{ scope.row.bill_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip :formatter="formatStatus" width="100" prop="bill_status" :label="$t('common.bill_status')" />
        <el-table-column prop="stor_name" :label="$t('common.stor_name')" width="100" />
        <el-table-column show-overflow-tooltip prop="bill_type" :formatter="bill_typeFormat" :label="$t('common.bill_type')" />
        <el-table-column show-overflow-tooltip width="135" prop="biz_date" :label="$t('common.biz_date')" />
        <el-table-column show-overflow-tooltip :formatter="create_modeFormat" prop="create_mode" :label="$t('common.create_mode')" width="100" />
        <el-table-column :label="$t('common.detail_count')" align="center" prop="detail_count" width="100" />
        <el-table-column :label="$t('common.total_qty')" align="center" prop="total_qty" width="100" />
        <el-table-column show-overflow-tooltip :label="$t('common.remark')" align="center" prop="remark" width="100" />
        <el-table-column show-overflow-tooltip :label="$t('common.input_optname')" align="center" prop="input_optname" />
        <el-table-column show-overflow-tooltip :label="$t('common.input_time')" align="center" prop="input_time" width="140" />
        <el-table-column show-overflow-tooltip :label="$t('common.update_optname')" align="center" prop="update_optname" />
        <el-table-column show-overflow-tooltip :label="$t('common.update_time')" align="center" prop="update_time" width="140" />
        <el-table-column show-overflow-tooltip :label="$t('common.dis_optname')" align="center" prop="dis_optname" />
        <el-table-column show-overflow-tooltip :label="$t('common.dis_time')" align="center" prop="dis_time" width="140" />
        <el-table-column show-overflow-tooltip :label="$t('common.confirm_optname')" align="center" prop="confirm_optname" />
        <el-table-column show-overflow-tooltip :label="$t('common.confirm_time')" align="center" prop="confirm_time" width="140" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <ViewDialog :dialog-show.sync="viewShow" :rowmst="mstrow" @AddChanged="querytable" />
  </div>
</template>

<script>
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 crudInchargefrom from '@/views/wms/st/incharge/incharge'
import crudStorattr from '@/views/wms/basedata/st/stor/storattr'
import ViewDialog from '@/views/wms/st/outbill/ViewDialog'
import crudUserStor from '@/views/wms/basedata/st/userStor/userStor'

export default {
  name: 'Incharge',
  components: { crudOperation, rrOperation, udOperation, pagination, DateRangePicker, ViewDialog },
  cruds() {
    return CRUD({
      title: '入库冲销',
      url: 'api/incharge',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudInchargefrom },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: false
      },
      queryOnPresenterCreated: false
    })
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['ST_CREATE_MODE', 'ST_INV_IN_TYPE', 'io_bill_status'],
  data() {
    return {
      mstrow: {},
      viewShow: false,
      permission: {
      },
      storlist: []
    }
  },
  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()
    },
    // [CRUD.HOOK.beforeRefresh]() {
    //   return false
    // },
    toView(index, row) {
      this.mstrow = row
      this.viewShow = true
    },
    charge() {
      const _selectData = this.$refs.table.selection
      crudInchargefrom.charge(_selectData[0]).then(res => {
        this.crud.notify(this.$t('common.Operation_success'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    formatStatus(row) {
      return this.dict.label.io_bill_status[row.bill_status]
    },
    bill_typeFormat(row) {
      return this.dict.label.ST_INV_IN_TYPE[row.bill_type]
    },
    create_modeFormat(row) {
      return this.dict.label.ST_CREATE_MODE[row.create_mode]
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
