<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产车间" prop="deptId">
        <dept-tree-select v-model="queryParams.deptId" class="small-tree-select"/>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下达时间" prop="releaseTime">
        <el-date-picker clearable
          v-model="queryParams.releaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="源单编号" prop="sourceNo">
        <el-input
          v-model="queryParams.sourceNo"
          placeholder="请输入源单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求单据" prop="demandBill">
        <el-input
          v-model="queryParams.demandBill"
          placeholder="请输入需求单据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划跟踪号" prop="planTrackNo" label-width="6em">
        <el-input
          v-model="queryParams.planTrackNo"
          placeholder="请输入计划跟踪号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-has-permi="['yh:prodOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-has-permi="['yh:prodOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['yh:prodOrder:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleSync"
          v-has-permi="['yh:prodOrder:sync']"
        >同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prodOrderList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'bizStatus'">
              <dict-tag :value="d.row[column.key]" :options="dict.type.prod_order_biz_status"/>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-has-permi="['yh:prodOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-has-permi="['yh:prodOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <form-col :span="span"  label="单据编号" prop="billNo">
            <el-input v-model="form.billNo" placeholder="请输入单据编号" />
          </form-col>
          <form-col :span="span"  label="单据日期" prop="billDate">
            <el-date-picker clearable
              v-model="form.billDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择单据日期">
            </el-date-picker>
          </form-col>
          <form-col :span="span"  label="规格型号" prop="spec">
            <el-input v-model="form.spec" placeholder="请输入规格型号" />
          </form-col>
          <form-col :span="span"  label="生产车间ID" prop="deptId">
            <el-input v-model="form.deptId" placeholder="请输入生产车间ID" />
          </form-col>
          <form-col :span="span"  label="单位" prop="unit">
            <el-input v-model="form.unit" placeholder="请输入单位" />
          </form-col>
          <form-col :span="span"  label="数量" prop="num">
            <el-input v-model="form.num" placeholder="请输入数量" />
          </form-col>
          <form-col :span="span"  label="批号" prop="batchNo">
            <el-input v-model="form.batchNo" placeholder="请输入批号" />
          </form-col>
          <form-col :span="span"  label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </form-col>
          <form-col :span="span"  label="下达时间" prop="releaseTime">
            <el-date-picker clearable
              v-model="form.releaseTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择下达时间">
            </el-date-picker>
          </form-col>
          <form-col :span="span"  label="源单编号" prop="sourceNo">
            <el-input v-model="form.sourceNo" placeholder="请输入源单编号" />
          </form-col>
          <form-col :span="span"  label="需求单据" prop="demandBill">
            <el-input v-model="form.demandBill" placeholder="请输入需求单据" />
          </form-col>
          <form-col :span="span"  label="辅助属性(json)" prop="auxProp">
            <el-input v-model="form.auxProp" placeholder="请输入辅助属性(json)" />
          </form-col>
          <form-col :span="span"  label="计划跟踪号" prop="planTrackNo">
            <el-input v-model="form.planTrackNo" placeholder="请输入计划跟踪号" />
          </form-col>
          <form-col :span="span"  label="物料编码" prop="materialNo">
            <el-input v-model="form.materialNo" placeholder="请输入物料编码" />
          </form-col>
          <form-col :span="span"  label="合格品入库数量" prop="quaStore">
            <el-input v-model="form.quaStore" placeholder="请输入合格品入库数量" />
          </form-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProdOrder,
  getProdOrder,
  delProdOrder,
  addProdOrder,
  updateProdOrder,
  syncProdOrder
} from "@/api/yh/prodOrder";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import DeptTreeSelect from "@/components/Business/Dept/DeptTreeSelect.vue";

// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "ProdOrder",
  mixins: [$showColumns],
  dicts: ['prod_order_biz_status'],
  components: {DeptTreeSelect, FormCol},
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        {key: 'id', visible: false, label: '生产订单ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'erpId', visible: true, label: '单据ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'billNo', visible: true, label: '单据编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'type', visible: true, label: '单据类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'billDate', visible: true, label: '单据日期', minWidth: null, sortable: false, overflow: false, align: 'center', width: "100"},
        {key: 'status', visible: true, label: '单据状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'transferStatus', visible: true, label: '调拨状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'pickStatus', visible: true, label: '领料状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'materialName', visible: true, label: '物料名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'spec', visible: true, label: '规格型号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'unit', visible: true, label: '单位', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'num', visible: true, label: '数量', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'bizStatus', visible: true, label: '业务状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'batchNo', visible: true, label: '批号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'remark', visible: true, label: '备注', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'releaseTime', visible: true, label: '下达时间', minWidth: null, sortable: false, overflow: false, align: 'center', width: "100"},
        {key: 'sourceNo', visible: true, label: '源单编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'demandBill', visible: true, label: '需求单据', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'planTrackNo', visible: true, label: '计划跟踪号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'productType', visible: true, label: '产品类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'materialNo', visible: true, label: '物料编码', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'quaStore', visible: true, label: '合格品入库数量', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'deptId', visible: true, label: '部门ID(本系统)', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'erpDeptId', visible: true, label: '部门ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产订单表格数据
      prodOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        billNo: null,
        type: null,
        status: null,
        transferStatus: null,
        pickStatus: null,
        spec: null,
        deptId: null,
        unit: null,
        bizStatus: null,
        batchNo: null,
        releaseTime: null,
        sourceNo: null,
        demandBill: null,
        planTrackNo: null,
        productType: null,
        materialNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billNo: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSync() {
      syncProdOrder().then(res => {
        if (res.code === 200) {
          this.$message.success("同步成功");
        }
      })
    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询生产订单列表 */
    getList() {
      this.loading = true;
      listProdOrder(this.queryParams).then(response => {
        this.prodOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNo: null,
        type: null,
        billDate: null,
        status: null,
        transferStatus: null,
        pickStatus: null,
        spec: null,
        deptId: null,
        unit: null,
        num: null,
        bizStatus: null,
        batchNo: null,
        remark: null,
        releaseTime: null,
        sourceNo: null,
        demandBill: null,
        auxProp: null,
        planTrackNo: null,
        productType: null,
        materialNo: null,
        quaStore: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProdOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProdOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProdOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产订单编号为"' + ids + '"的数据项？').then(function() {
        return delProdOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yh/prodOrder/export', {
        ...this.queryParams
      }, `prodOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
