<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.sale_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_SALE_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>
          <rrOperation :crud="crud"/>
        </el-form>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="deleteApsData"
        >
          清空测试数据
        </el-button>

      </div>
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          icon="el-icon-upload2"
          size="mini"
          @click="uploadShow = true"
        >
          导入
        </el-button>

        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          @click="importin"
        >
          aps提交
        </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="1200px"
      >
        <el-form
          ref="form"
          :inline="true"
          :model="form"
          label-width="160px"
          style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;"
        >
          <el-form-item label="车间" prop="product_area">
            <!-- unit_list-->
            <el-select v-model="form.product_area" :disabled="crud.status.edit > 0 || crud.status.view > 0"
                       placeholder=""
            >
              <el-option
                v-for="item in dict.product_area"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="销售单号" prop="sale_code">
            <el-input v-model="form.sale_code" :disabled="crud.status.edit > 0 || crud.status.view > 0"
                      style="width: 200px;"
            />
          </el-form-item>
          <el-form-item label="合同号" prop="contract_code">
            <el-input v-model="form.contract_code" :disabled="crud.status.edit > 0 || crud.status.view > 0"
                      style="width: 200px;"
            />
          </el-form-item>
          <!-- <el-form-item label="计量单位" prop="unit_name">
            <el-select v-model="form.qty_unit_id" placeholder="">
              <el-option
                v-for="item in unit_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="物料编码" prop="material_code">
            <el-input v-model="form.material_code" :disabled="crud.status.edit > 0 || crud.status.view > 0"
                      style="width: 200px;"
            />
          </el-form-item>
          <!-- <el-form-item label="客户名称" prop="cust_name">
            <el-select v-model="form.cust_code" placeholder="">
              <el-option
                v-for="item in customer_list"
                :key="item.label"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="计划交期时间" prop="plandeliver_date">
            <el-date-picker
              v-model="form.plandeliverDate"
              value-format="yyyy-MM-dd"
              type="date"
              style="width: 200px;"
            />
            <el-form-item label="订单数量" prop="sale_qty">
              <el-input-number v-model="form.sale_qty" style="width: 200px;"/>
            </el-form-item>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <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.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="sale_code"
          label="销售单号"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="seq_no"
          label="销售单行号"
          min-width="100"
          show-overflow-tooltip
        />
        <!-- <el-table-column prop="seq_no" label="明细序号" /> -->

        <el-table-column prop="contract_code" label="合同号"
                         :min-width="flexWidth('contract_code', crud.data, '合同号')"
        />

        <el-table-column prop="material_code" label="物料编码"
                         :min-width="flexWidth('material_code', crud.data, '物料编码')"
        />

        <el-table-column
          prop="material_spec"
          label="物料规格"
          :min-width="flexWidth('material_spec', crud.data, '物料规格')"
        />

        <el-table-column
          prop="material_name"
          label="物料名称"
          :min-width="flexWidth('material_name', crud.data, '物料名称')"
          show-overflow-tooltip
        />

        <el-table-column prop="product_area" label="工段">
          <template slot-scope="scope">
            {{ dict.label.product_area[scope.row.product_area] }}
          </template>
        </el-table-column>

        <el-table-column prop="sale_qty" label="订单数量"/>

        <el-table-column
          prop="qty_unit_name"
          label="计量单位"
          show-overflow-tooltip
          min-width="80"
        />
        <el-table-column
          prop="plandeliverDate"
          label="前段计划交期"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column prop="status" label="前段状态">
          <template slot-scope="scope">
            {{ dict.label.PCS_SALE_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="behindPlandeliverDate"
          label="后段计划交期"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column prop="behind_submit_flag" label="后段状态">
          <template slot-scope="scope">
            {{ dict.label.PCS_SALE_STATUS[scope.row.behind_submit_flag] }}
          </template>
        </el-table-column>

        <el-table-column
          prop="rawmaterial_name"
          label="原材料信息"
          :min-width="flexWidth('rawmaterial_name', crud.data, '原材料信息')"
          show-overflow-tooltip
        />
        <el-table-column
          prop="rate_qty"
          label="单重"
          :min-width="flexWidth('rate_qty', crud.data, '单重')"
          show-overflow-tooltip
        />
        <el-table-column
          prop="canuse_qty"
          label="原材料库存"
          :min-width="flexWidth('canuse_qty', crud.data, '原材料库存')"
          show-overflow-tooltip
        />
        <!--        <el-table-column-->
        <!--          prop="early_start_time"-->
        <!--          label="最早开始时间"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <el-table-column
          prop="product_qty"
          label="生产数量"
          :min-width="flexWidth('product_qty', crud.data, '生产数量')"
          show-overflow-tooltip
        />
        <el-table-column
          prop="sale_type"
          label="销售单类型"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ dict.label.PCS_SAL_TYPE[scope.row.sale_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="create_mode" label="生成方式" min-width="100">
          <template slot-scope="scope">
            {{ dict.label.ST_CREATE_MODE[scope.row.create_mode] }}
          </template>
        </el-table-column>
        <el-table-column prop="create_name" label="创建人"/>
        <el-table-column
          prop="createTime"
          label="创建时间"
          :min-width="flexWidth('createTime', crud.data, '创建时间')"
        />
        <el-table-column prop="update_time" label="修改时间"
                         :min-width="flexWidth('update_time',crud.data,'修改时间')"
        />
        <el-table-column
          prop="remark"
          label="备注"
          show-overflow-tooltip
          min-width="200"
        />
        <!-- <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>
    <UploadDialog
      :dialog-show.sync="uploadShow"
      @tableChanged3="tableChanged3"
    />
    <Dialog :dialog-show.sync="dialogShow"/>
  </div>
</template>

<script>
import crudSaleOrder from '@/views/wms/pcs/saleorder/saleOrder'
import customerbase from '@/api/wms/basedata/master/customerbase'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import UploadDialog from '@/views/wms/pcs/saleorder/UploadDialog'
import Dialog from '@/views/wms/pcs/saleorder/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'
import crudBigScreen from '@/api/wms/bigScreen/bigScreen'

const defaultForm = {
  sale_id: null,
  sale_code: null,
  seq_no: null,
  contract_code: null,
  sale_type: null,
  material_id: null,
  status: null,
  sale_qty: null,
  produce_seq: null,
  cust_id: null,
  qty_unit_id: null,
  plandeliver_date: 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: 'SaleOrder',
  dicts: ['PCS_SALE_STATUS', 'PCS_SAL_TYPE', 'IS_OR_NOT', 'product_area', 'ST_CREATE_MODE'],
  components: {
    pagination,
    crudOperation,
    rrOperation,
    Treeselect,
    UploadDialog,
    Dialog
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '销售订单',
      url: 'api/mpsSaleOrder',
      idField: 'sale_id',
      sort: 'sale_id,desc',
      optShow: {
        add: true,
        edit: true,
        del: false,
        reset: true,
        download: false
      },
      crudMethod: { ...crudSaleOrder }
    })
  },
  data() {
    return {
      unit_list: [{ value: 16, label: '个\\只' }],
      customer_list: [],
      classes3: [],
      uploadShow: false,
      dialogShow: false,
      permission: {
        add: ['admin', 'roles:add'],
        edit: ['admin', 'roles:edit'],
        del: ['admin', 'roles:del']
      },
      queryTypeOptions: [{ key: 'sale_code', display_name: '销售单号' }]
    }
  },
  created() {
    this.initClass3()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      customerbase.selectList().then(res => {
        this.customer_list = res.content
      })
    },
    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
    },
    tableChanged3() {
      this.crud.toQuery()
    },
    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
      })
    },
    deleteApsData() {
      crudSaleOrder.deleteTestData().then((res) => {
        this.crud.notify('清空成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped></style>
