<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.deliver_code" clearable placeholder="发货单号" style="width: 200px;"
                      class="filter-item"/>
          </el-form-item>
          <el-form-item label="物料搜索">
            <el-input
              v-model="query.material"
              clearable
              size="small"
              placeholder="物料编码、名称或规格"
              style="width: 200px;"
              class="filter-item"
            />
          </el-form-item>
          <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 label="状态">
            <el-select
              v-model="query.status"
              style="width: 200px"
              clearable
              filterable
              placeholder="请选择"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.PCS_DELI_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品系列">
            <treeselect
              v-model="query.product_series"
              :options="classes3"
              :auto-load-root-options="false"
              :load-options="loadChildNodes"
              style="width: 200px;"
              placeholder="请选择"
            />
          </el-form-item>
          <el-form-item label="是否正常">
            <el-select
              v-model="query.is_success"
              style="width: 200px"
              clearable
              filterable
              placeholder="请选择"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.IS_OR_NOT"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation :crud="crud"/>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="importin"
        >
          同步
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <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 prop="deliver_code" label="发货单号"/>
        <el-table-column prop="seq_no" label="明细序号"/>
        <el-table-column prop="deliver_type" label="发货单类型" width="120">
          <template slot-scope="scope">
            {{ dict.label.PCS_DELIVER_TYPE[scope.row.deliver_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="material_code" label="物料编码"/>
        <el-table-column prop="material_name" label="物料名称"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.PCS_DELI_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="delivery_qty" label="发货数量"/>
        <el-table-column prop="cust_code" label="客户编码"/>
        <el-table-column prop="cust_coname" label="客户名称"/>
        <el-table-column prop="unit_name" label="计量单位" show-overflow-tooltip min-width="150"/>
        <el-table-column prop="sale_code" label="销售单号"/>
        <el-table-column prop="sale_seq_no" label="销售明细序号" show-overflow-tooltip min-width="150"/>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip min-width="200"/>
        <el-table-column prop="create_name" label="创建人"/>
        <el-table-column prop="create_time" label="创建时间"/>
        <el-table-column prop="update_optname" label="修改人"/>
        <el-table-column prop="update_time" label="修改时间"/>
        <!--        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
                  <template slot-scope="scope">
                    <udOperation
                      :data="scope.row"
                      :permission="permission"
                    />
                  </template>
                </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
    <Dialog :dialog-show.sync="dialogShow"/>
  </div>
</template>

<script>
import crudDeliveryOrder from '@/api/wms/pcs/deliveryOrder'
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 Dialog from '@/views/wms/pcs/deliveryorder/Dialog'
import pagination from '@crud/Pagination'
import Treeselect, {LOAD_CHILDREN_OPTIONS} from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import crudClassstandard from '@/api/wms/basedata/master/classstandard'

const defaultForm = {
  deliver_id: null,
  deliver_code: null,
  seq_no: null,
  deliver_type: null,
  material_id: null,
  status: null,
  delivery_qty: null,
  cust_id: null,
  qty_unit_id: null,
  sale_id: null,
  sale_code: null,
  sale_seq_no: null,
  create_id: null,
  create_name: null,
  create_time: null,
  update_optid: null,
  update_optname: null,
  update_time: null,
  cust_code: null,
  cust_name: null
}
export default {
  name: 'DeliveryOrder',
  dicts: ['PCS_DELIVER_TYPE', 'PCS_DELI_STATUS'],
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect, Dialog},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '发货单', url: 'api/deliveryOrder', idField: 'deliver_id', sort: 'deliver_id,desc',
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      },
      crudMethod: {...crudDeliveryOrder}
    })
  },
  data() {
    return {
      classes3: [],
      dialogShow: false,
      permission: {},
      rules: {},
      queryTypeOptions: [
        {key: 'deliver_code', display_name: '发货单号'}
      ]
    }
  },
  created() {
    this.initClass3()
  },
  methods: {
    queryClassId() {
      const param = {
        'class_idStr': this.class_idStr
      }
      crudClassstandard.queryClassById(param).then(res => {
        this.classes = res.content.map(obj => {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    importin() {
      this.dialogShow = true
    },
    buildTree(classes) {
      classes.forEach(data => {
        if (data.children) {
          this.buildTree(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null // 重点代码
        }
      })
    },
    // 获取子节点数据
    loadChildNodes({action, parentNode, callback}) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudClassstandard.getClass({pid: parentNode.id}).then(res => {
          parentNode.children = res.content.map(function (obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    initClass3() {
      const param = {
        parent_class_code: '07'
      }
      crudClassstandard.getClassType(param).then(res => {
        const data = res.content
        this.buildTree(data)
        this.classes3 = data
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
