<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="businessType">
        <el-input
          v-model="queryParams.businessType"
          placeholder="请输入业务类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker
          v-model="queryParams.createDate"
          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:inspectBill: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:inspectBill: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:inspectBill: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:inspectBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectBillList" @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 === 'C'" type="success">已审核</el-tag>
          <el-tag v-else-if="scope.row.documentStatus === 'A'" type="warning">审核中</el-tag>
          <span v-else>{{ scope.row.documentStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="fbillTypeId" min-width="120" />
      <el-table-column label="检验人" align="center" prop="inspectorId" width="100" />
      <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="approveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approveDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="同步状态" align="center" prop="isSynced" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isSynced === 1" type="success">已同步</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:inspectBill: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="1200px" 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="businessType">
                  <el-input v-model="form.businessType" placeholder="请输入业务类型" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <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="A" />
                    <el-option label="已审核" value="C" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="检查数量" prop="checkQty">
                  <el-input-number v-model="form.checkQty" :min="0" :precision="2" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="检验日期" prop="fDate">
                  <el-date-picker
                    v-model="form.fDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    :disabled="formMode === 'view'"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="默认扫描箱" prop="defScanBox">
                  <el-input v-model="form.defScanBox" placeholder="请输入默认扫描箱" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="辅助文本1" prop="fQpbzText1">
                  <el-input v-model="form.fQpbzText1" type="textarea" :rows="3" placeholder="请输入辅助文本" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="检验明细" name="entityList">
            <el-table :data="form.entityList || []" border stripe max-height="500">
              <el-table-column label="序号" type="index" width="60" align="center" />
              <el-table-column label="质检单据ID" prop="inspectBillId" width="120" align="center" />
              <el-table-column label="物料ID" prop="materialId" min-width="120" align="center" />
              <el-table-column label="QC方案ID" prop="qcSchemeId" width="100" align="center" />
              <el-table-column label="单位ID" prop="unitId" width="100" align="center" />
              <el-table-column label="检验数量" prop="inspectQty" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.inspectQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="基本检验数量" prop="baseInspectQty" width="120" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.baseInspectQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="合格数量" prop="qualifiedQty" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.qualifiedQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="不合格数量" prop="unqualifiedQty" width="120" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.unqualifiedQty }}</span>
                </template>
              </el-table-column>
              <el-table-column label="检验结果" prop="inspectResult" width="100" align="center" />
              <el-table-column label="批次文本" prop="lotText" width="120" align="center" />
              <el-table-column label="检验开始日期" prop="inspectStartDate" width="180" align="center">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.inspectStartDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="检验结束日期" prop="inspectEndDate" width="180" align="center">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.inspectEndDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
              </el-table-column>
            </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 { listInspectBill, getInspectBill, delInspectBill, addInspectBill, updateInspectBill } from "@/api/erp/inspectBill";

export default {
  name: "InspectBill",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验单表格数据
      inspectBillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单模式 edit/view
      formMode: "edit",
      // 当前激活的标签页
      activeTab: "basic",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNo: null,
        documentStatus: null,
        businessType: null,
        createDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billNo: [
          { required: true, message: "单据号不能为空", trigger: "blur" }
        ],
        businessType: [
          { required: true, message: "业务类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检验单列表 */
    getList() {
      this.loading = true;
      listInspectBill(this.queryParams).then(response => {
        this.inspectBillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNo: null,
        documentStatus: null,
        businessType: null,
        checkQty: null,
        fDate: null,
        defScanBox: null,
        fQpbzText1: null,
        entityList: []
      };
      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
      getInspectBill(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
      getInspectBill(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) {
            updateInspectBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspectBill(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 delInspectBill(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erpInspectBill/export', {
        ...this.queryParams
      }, `inspectBill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


