<template>
  <el-dialog
    :visible.sync="dialogVisible"
    append-to-body
    destroy-on-close
    title="订单选择"
    width="1250px"
    @close="close"
    @open="open"
  >
    <el-form
      :inline="true"
      class="demo-form-inline"
      label-position="right"
      label-suffix=":"
      label-width="80px"
    >
      <el-form-item label="模糊搜索">
        <el-input
          v-model="query.search"
          clearable
          placeholder="销售单号、物料编码或规格"
          style="width: 200px;"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <rrOperation/>
    </el-form>

    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      border
      size="mini"
      style="width: 100%;"
      @select="handleSelectionChange"
      @select-all="onSelectAll"
      @current-change="clickChange"
    >
      <el-table-column v-if="!isSingle" type="selection" width="55"/>
      <el-table-column v-if="isSingle" label="选择" width="55">
        <template slot-scope="scope">
          <el-radio v-model="tableRadio" :label="scope.row"><i/></el-radio>
        </template>
      </el-table-column>
      <el-table-column
        label="销售单号"
        min-width="100"
        prop="sale_code"
        show-overflow-tooltip
      />
      <el-table-column
        label="行号"
        min-width="60"
        prop="seq_no"
        show-overflow-tooltip
      />
      <!--      <el-table-column-->
      <!--        :min-width="flexWidth('contract_code', crud.data, '合同号')" label="合同号"-->
      <!--        prop="contract_code"-->
      <!--      />-->
      <el-table-column
        :min-width="flexWidth('material_code', crud.data, '物料编码')" label="物料编码"
        prop="material_code"
      />

      <el-table-column
        :min-width="flexWidth('material_spec', crud.data, '物料规格')"
        label="物料规格"
        prop="material_spec"
      />
      <!--      <el-table-column label="工段" prop="product_area">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{ dict.label.product_area[scope.row.product_area] }}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="订单数量" min-width="80" prop="sale_qty"/>
      <el-table-column label="需生产数量" min-width="88" prop="need_product_qty"/>
      <el-table-column label="已生产数量" min-width="88" prop="product_qty"/>
      <el-table-column label="刻字数量" prop="lettering_qty"/>
      <el-table-column label="预入库数量" min-width="88" prop="instor_qty"/>
      <el-table-column label="入库数量" prop="in_storage_qty"/>
      <el-table-column label="发货数量" prop="sendout_qty"/>

      <el-table-column
        label="预入库交期"
        min-width="100"
        prop="warehousing_date"
        show-overflow-tooltip
      />
      <el-table-column
        label="计划交期"
        min-width="100"
        prop="plandeliverDate"
        show-overflow-tooltip
      />
      <el-table-column
        label="订单交期"
        min-width="100"
        prop="order_date"
        show-overflow-tooltip
      />
      <el-table-column label="订单状态" prop="status">
        <template slot-scope="scope">
          {{ dict.label.PCS_SALE_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="create_name"/>
      <el-table-column
        :min-width="flexWidth('createTime', crud.data, '创建时间')"
        label="创建时间"
        prop="createTime"
      />
    </el-table>
    <!--分页组件-->
    <pagination/>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import crudMaterialbase from '@/api/wms/basedata/master/materialbase'
import CRUD, { header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'OrderDtl',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({
      title: '订单', url: 'api/mpsSaleOrder',
      idField: 'sale_id',
      sort: 'plandeliver_date,seq_no desc',
      optShow: {},
      crudMethod: { ...crudMaterialbase }
    })
  },
  mixins: [presenter(), header()],
  dicts: ['PCS_SALE_STATUS', 'product_series'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: true
    },
    materOptCode: {
      type: String,
      default: '00'
    }
  },
  data() {
    return {
      dialogVisible: false,
      classes: [],
      tableRadio: null,
      class_idStr: null,
      checkrow: null,
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    clickChange(item) {
      this.tableRadio = item
    },
    seriesFormat(row) {
      return this.dict.label.product_series[row.product_series]
    },
    handleSelectionChange(val, row) {
      if (this.isSingle) {
        if (val.length > 1) {
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(val.pop())
        } else {
          this.checkrow = row
        }
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
    },
    submit() {
      // 处理单选
      if (this.isSingle && this.tableRadio) {
        this.$emit('update:dialogShow', false)
        this.$emit('setOrderValue', this.tableRadio)
        return
      }
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选订单')
        return
      }
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
      this.$emit('setOrderValue', this.rows)
    }
  }
}
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 0px;
}
</style>

