<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <label class="el-form-item-label">物料编码</label>
      <el-select v-model="query.itemCode" placeholder="物料编码" filterable style="width: 170px;" class="filter-item">
        <el-option
          @keyup.enter.native="crud.toQuery"
          v-for="item in this.items"
          :key="item.code"
          :label="item.code"
          :value="item.code"
        >
          <span style="float: left">{{ item.code }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
        </el-option>
      </el-select>

      <label class="el-form-item-label">物料名称</label>
      <el-input v-model="query.itemName" clearable placeholder="物料名称" style="width: 170px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />

      <label class="el-form-item-label">MO票</label>
      <el-input v-model="query.po" clearable placeholder="请输入MO票" style="width: 170px;" class="filter-item"
                @keyup.enter.native="crud.toQuery"
      />

      <label class="el-form-item-label">日期</label>
      <date-range-picker v-model="query.createTime" class="date-item" style="width: 170px"/>

      <rrOperation :crud="crud"/>
      <el-button
        class="filter-item"
        size="mini"
        type="warning"
        icon="el-icon-refresh-left"
        @click="resetQuery"
      >
        重置
      </el-button>


      <div class="statusButton" style="border-bottom: solid lightgray 1px;">
        <el-radio-group v-model="radio3" @change="clickChange" size="small">
          <el-radio-button label="全部"> 全部</el-radio-button>
          <el-radio-button label="打开"> 打开</el-radio-button>
          <el-radio-button label="收货完成"> 收货完成</el-radio-button>
        </el-radio-group>


      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-upload
          class="upload-demo"
          slot="right"
          style="float: right;padding-left: 5px"
          :headers="headers"
          action="/api/asnDetail/import_mo"
          :file-list="fileList"
          :on-error="handleErr"
          :on-success=" (response, file, fileList) => { return handleSuccess(response, file, fileList,crud);}"
          :show-file-list="true"
        >
          <el-button size="mini" v-if="false" type="success" icon="el-icon-upload2">导入</el-button>
        </el-upload>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          size="mini"
          @click="materialInDialog = true"
        >
          原材料入库
        </el-button>
      </crudOperation>
    </div>

    <!--表单组件-->
    <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
               :title="crud.status.title" width="400px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="物料" prop="item">
          <el-select v-model="form.item" @focus="getItem" value-key="id" filterable placeholder="请选择物料"
                     style="width: 200px;"
          >
            <el-option
              v-for="item in items"
              :key="item.id"
              :label="item.code"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="容器" prop="stock">
          <el-select v-model="form.stock" @focus="getStock" value-key="id" filterable placeholder="请选择容器"
                     style="width: 200px;"
          >
            <el-option
              v-for="item in stocks"
              :key="item.id"
              :label="item.code"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单数量">
          <el-input v-model="form.orderQty" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="批次号">
          <el-input v-model="form.propC1" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--原材料入库组件-->
    <el-dialog :visible.sync="materialInDialog" title="原材料入库" width="400px" :before-close="materialInCancel">
      <el-form ref="materialInform" :model="BindStock" :rules="materialInRules" size="small" label-width="80px">
        <el-form-item label="MO" prop="boxNumbers">
          <el-input type="textarea" v-model="BindStock.boxNumbers" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="容器" prop="stockCode">
          <el-select v-model="BindStock.stockCode" @focus="getStock" value-key="id" filterable placeholder="请选择容器"
                     style="width: 200px;"
          >
            <el-option
              v-for="item in stocks"
              :key="item.id"
              :label="item.code"
              :value="item.code"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="点位" prop="pointCode">
          <el-select v-model="BindStock.pointCode" @focus="getPoint" value-key="id" filterable style="width:200px"
                     placeholder="请选择点位"
          >
            <el-option
              v-for="point in points"
              :key="point.id"
              :label="point.name"
              :value="point.code"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="materialInCancel">取消</el-button>
        <el-button type="primary" @click="submitMaterialIn">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" height="58vh" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
              @selection-change="this.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="xh" type="index" :index="indexMethod" label="序号"/>
      <el-table-column prop="itemCode" label="物料编码" width="120">
        <template slot-scope="scope">
          <div>{{ scope.row.item.code }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="itemName" label="物料名称">
        <template slot-scope="scope">
          <div>{{ scope.row.item.name }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="stock" label="容器">
        <template slot-scope="scope">
          <div>{{ scope.row.stock == null ? '' : scope.row.stock.code }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ dict.label.asn_status[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column prop="orderQty" label="订单数量"/>
      <el-table-column prop="receivedQty" label="收货数量"/>
      <el-table-column prop="propC1" label="批次号" width="120"/>
      <el-table-column prop="po" label="MO票" width="200"/>
      <el-table-column prop="orderNumber" label="箱号" width="200"/>
      <el-table-column prop="remark" label="备注"/>
      <el-table-column prop="createBy" label="创建人"/>
      <el-table-column prop="createTime" label="创建时间" width="135" sortable/>
    </el-table>
    <!--分页组件-->

    <div style="float: right;">
      <pagination/>
    </div>

  </div>
</template>

<script>
import crudAsnDetail from '@/api/asnDetail'
import crudItem from '@/api/item'
import crudStock from '@/api/stock'
import crudPoint from '@/api/point'
import crudApp from '@/api/app'
import CRUD, { presenter, header, form, crud } 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 Treeselect from '@riophae/vue-treeselect'
import Search from '@/views/monitor/log/search.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'
import { getToken } from '@/utils/auth'

const defaultForm = {
  id: null,
  asnId: null,
  item: null,
  stock: null,
  lineNo: null,
  po: null,
  status: 'OPEN',
  orderQty: 0,
  receivedQty: 0,
  moveQty: 0,
  putQty: 0,
  weight: 0,
  volume: 0,
  remark: null,
  propC1: null,
  propC2: null,
  propC3: null,
  propC4: null,
  propC5: null,
  propC6: null,
  propD1: null,
  propD2: null,
  deptId: null,
  sourceName: null,
  sourceId: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'AsnDetail',
  components: { DateRangePicker, Search, Treeselect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['asn_status'],
  cruds() {
    return CRUD({
      title: '收货数据', url: 'api/asnDetail', idField: 'id', sort: 'id,desc', crudMethod: { ...crudAsnDetail },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: false,
        download: true
      }
    })
  },
  data() {
    return {
      points: [],
      BindStock: { stockCode: null, boxNumbers: null, pointCode: null },
      KuQu: null,
      materialInDialog: false,
      headers: { 'Authorization': getToken() },
      radio3: '全部',
      fileList: [],
      items: [],
      stocks: [],
      permission: {
        add: ['admin', 'asnDetail:add'],
        edit: ['admin', 'asnDetail:edit'],
        del: ['admin', 'asnDetail:del']
      },
      rules: {
        item: [
          { required: true, message: '物料不能为空', trigger: 'blur' }
        ],
        stock: [
          { required: true, message: '容器不能为空', trigger: 'blur' }
        ],
        po: [
          { required: true, message: 'MO票不能为空', trigger: 'blur' }
        ]
      },
      materialInRules: {
        boxNumbers: [
          { required: true, message: 'Mo不能为空', trigger: 'blur' }
        ],
        stockCode: [
          { required: true, message: '容器不能为空', trigger: 'blur' }
        ],
        pointCode: [
          { required: true, message: '点位不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'itemCode', display_name: '物料代码' },
        { key: 'itemName', display_name: '物料名称' },
        { key: 'po', display_name: 'MO票' },
        { key: 'status', display_name: '状态' }
      ]

    }
  },
  mounted() {
    //初始化数据
    this.getItem()
  },
  methods: {
    handleSuccess(response, file, fileList, crud) {
      let myError = response.toString()//转字符串
      this.crud.notify(response['message'], CRUD.NOTIFICATION_TYPE.SUCCESS)
      crud.toQuery()
    },
    handleErr(err, file, fileList) {

      let myError = err.toString()//转字符串
      myError = myError.replace('Error: ', '') // 去掉前面的" Error: "
      myError = JSON.parse(myError)//转对象
      this.crud.notify(myError['message'], CRUD.NOTIFICATION_TYPE.ERROR)
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItem() {
      crudItem.queryItemAll({ enabled: true }).then(res => {
        this.items = res
      })
    },
    getStock() {
      crudStock.queryStockAll({ status: 'FREE', enabled: true, stockType: '托盘' }).then(res => {
        this.stocks = res
      })
    },
    indexMethod(index) {
      return index * 1 + 1
    },
    selectionChangeHandler(val) {
      this.crud.selections = val
    },
    clickChange(lab) {
      if (lab === '全部') {
        this.crud.resetQuery()
        this.crud.toQuery()
      } else if (lab === '打开') {
        this.query.status = 'OPEN'
        this.crud.toQuery()
      } else if (lab === '收货完成') {
        this.query.status = 'RECEIVED'
        this.crud.toQuery()
      }
    },
    resetQuery() {
      this.radio3 = '全部'
      this.crud.resetQuery()
    },
    materialInCancel() {
      this.materialInDialog = false
      this.$refs['materialInform'].resetFields()
    },

    getPoint() {
      crudPoint.queryPointAll({ areaName: '大小件入库区' }).then(res => {
        this.points = res
      })
    },

    submitMaterialIn() {
      this.$refs['materialInform'].validate(valid => {
        if (!valid) {
          return
        }
        crudApp.materialIn(this.BindStock).then(res => {
          if (res.status = 200) {
            this.materialInDialog = false//关闭窗口
            this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
            this.crud.toQuery()
          } else {
            this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.ERROR)
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
