<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <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="documentStatus">
        <el-select v-model="queryParams.documentStatus" placeholder="请选择单据状态" clearable>
          <el-option label="暂存" value="Z" />
          <el-option label="审核中" value="C" />
          <el-option label="已审核" value="A" />
        </el-select>
      </el-form-item>
      <el-form-item label="生产组织" prop="prdOrgName">
        <el-input
          v-model="queryParams.prdOrgName"
          placeholder="请输入生产组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据日期" prop="billDate">
        <el-date-picker
          v-model="queryParams.billDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </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-hasPermi="['erp:productionInstock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['erp:productionInstock:edit']"
        >修改</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-hasPermi="['erp:productionInstock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['erp:productionInstock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="instockList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" prop="id" width="120" align="center" />
      <el-table-column label="单据编号" align="center" prop="billNo" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="单据状态" align="center" prop="documentStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.documentStatus === 'Z'" type="info">暂存</el-tag>
          <el-tag v-else-if="scope.row.documentStatus === 'A'" type="warning">审核中</el-tag>
          <el-tag v-else-if="scope.row.documentStatus === 'C'" type="success">已审核</el-tag>
          <span v-else>{{ scope.row.documentStatus }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取消状态" align="center" prop="cancelStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.cancelStatus === 'A'" type="danger">已取消</el-tag>
          <el-tag v-else type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['erp:productionInstock:query']"
          >详情</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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="基本信息" name="basic">
            <el-row>
              <el-col :span="12">
                <el-form-item label="单据编号" prop="billNo">
                  <el-input v-model="form.billNo" placeholder="请输入单据编号" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单据状态" prop="documentStatus">
                  <el-select v-model="form.documentStatus" placeholder="请选择单据状态" :disabled="formMode === 'view'">
                    <el-option label="暂存" value="Z" />
                    <el-option label="审核中" value="C" />
                    <el-option label="已审核" value="A" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="单据日期" prop="billDate">
                  <el-date-picker
                    v-model="form.billDate"
                    type="datetime"
                    placeholder="选择日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled="formMode === 'view'"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单据类型ID" prop="billTypeId">
                  <el-input v-model="form.billTypeId" placeholder="请输入单据类型ID" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="生产组织ID" prop="prdOrgId">
                  <el-input-number v-model="form.prdOrgId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="库存组织ID" prop="stockOrgId">
                  <el-input-number v-model="form.stockOrgId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          
          <el-tab-pane label="组织信息" name="org">
            <el-row>
              <el-col :span="12">
                <el-form-item label="货主类型" prop="ownerTypeId">
                  <el-input v-model="form.ownerTypeId" placeholder="请输入货主类型" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="货主ID" prop="ownerId">
                  <el-input-number v-model="form.ownerId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="车间ID" prop="workShopId">
                  <el-input-number v-model="form.workShopId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="收货员ID" prop="stokerId">
                  <el-input-number v-model="form.stokerId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="币别ID" prop="currId">
                  <el-input-number v-model="form.currId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否委托" prop="isEntrust">
                  <el-switch v-model="form.isEntrust" :active-value="1" :inactive-value="0" :disabled="formMode === 'view'"></el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          
          <el-tab-pane label="其他信息" name="other">
            <el-row>
              <el-col :span="12">
                <el-form-item label="取消状态" prop="cancelStatus">
                  <el-input v-model="form.cancelStatus" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="跨组织业务" prop="iosBizTypeId">
                  <el-input-number v-model="form.iosBizTypeId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="跨组织财务" prop="isIosForFin">
                  <el-switch v-model="form.isIosForFin" :active-value="1" :inactive-value="0" :disabled="formMode === 'view'"></el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="生成跨组织" prop="isGenForIos">
                  <el-switch v-model="form.isGenForIos" :active-value="1" :inactive-value="0" :disabled="formMode === 'view'"></el-switch>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="委托入库单ID" prop="entrustInStockId">
                  <el-input-number v-model="form.entrustInStockId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          
          <el-tab-pane label="入库明细" name="details">
            <el-table :data="form.details || []" border stripe max-height="500">
              <el-table-column label="序号" type="index" width="60" align="center" />
              <el-table-column label="生产入库单ID" prop="mainId" width="120" align="center" />
              <el-table-column label="物料编码" prop="materialNumber" min-width="120" align="center" />
              <el-table-column label="物料名称" prop="materialName" min-width="150" align="center" />
              <el-table-column label="物料规格" prop="materialSpec" min-width="120" align="center" />
              <el-table-column label="产品类型" prop="productType" width="100" align="center" />
              <el-table-column label="入库类型" prop="inStockType" width="100" align="center" />
              <el-table-column label="单位" prop="unitName" width="100" align="center" />
              <el-table-column label="应发数量" prop="mustQty" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.mustQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="实发数量" prop="realQty" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.realQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="基本单位应发数量" prop="baseMustQty" width="140" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.baseMustQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="基本单位实发数量" prop="baseRealQty" width="140" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.baseRealQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="仓库" prop="stockName" width="120" align="center" />
              <el-table-column label="车间" prop="workShopName" width="120" align="center" />
              <el-table-column label="生产订单编号" prop="moBillNo" min-width="150" align="center" />
              <el-table-column label="单价" prop="price" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.price }}</span>
                </template>
              </el-table-column>
              <el-table-column label="金额" prop="amount" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.amount }}</span>
                </template>
              </el-table-column>
              <el-table-column label="生产日期" prop="produceDate" width="180" align="center">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="有效期至" prop="expiryDate" width="180" align="center">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="批号文本" prop="qpbzText" width="120" align="center" />
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" v-if="formMode !== 'view'">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductionInstock, getProductionInstock, delProductionInstock, addProductionInstock, updateProductionInstock } from "@/api/erp/productionInstock";

export default {
  name: "ProductionInstock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产入库单表格数据
      instockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单模式 edit/view
      formMode: "edit",
      // 当前激活的标签页
      activeTab: "basic",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNo: null,
        documentStatus: null,
        prdOrgName: null,
        billDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billNo: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产入库单列表 */
    getList() {
      this.loading = true;
      listProductionInstock(this.queryParams).then(response => {
        this.instockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNo: null,
        documentStatus: null,
        billDate: null,
        billTypeId: null,
        prdOrgId: null,
        stockOrgId: null,
        ownerTypeId: null,
        ownerId: null,
        workShopId: null,
        stokerId: null,
        currId: null,
        isEntrust: 0,
        cancelStatus: null,
        iosBizTypeId: null,
        isIosForFin: 0,
        isGenForIos: 0,
        entrustInStockId: null,
        details: []
      };
      this.activeTab = "basic";
      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 = "添加生产入库单";
      this.formMode = "edit";
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getProductionInstock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "生产入库单详情";
        this.formMode = "view";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductionInstock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产入库单";
        this.formMode = "edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductionInstock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductionInstock(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 delProductionInstock(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('prdInstockMain/export', {
        ...this.queryParams
      }, `productionInstock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


