<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="供应商ID" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商ID"
          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:purchaseOrder: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:purchaseOrder: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:purchaseOrder: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:purchaseOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseOrderList" @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="warning">审核中</el-tag>
          <el-tag v-else-if="scope.row.documentStatus === 'A'" type="success">已审核</el-tag>
          <span v-else>{{ scope.row.documentStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="businessType" min-width="120" />
      <el-table-column label="供应商ID" align="center" prop="supplierId" min-width="120" />
      <el-table-column label="供应商联系人" align="center" prop="providerContact" width="120" />
      <el-table-column label="供应商地址" align="center" prop="providerAddress" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="采购日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关闭状态" align="center" prop="closeStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.closeStatus === 'A'" type="danger">已关闭</el-tag>
          <el-tag v-else type="success">正常</el-tag>
        </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:purchaseOrder: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="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="C" />
                    <el-option label="已审核" value="A" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="采购日期" prop="date">
                  <el-date-picker
                    v-model="form.date"
                    type="datetime"
                    placeholder="选择日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :disabled="formMode === 'view'"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="采购组织ID" prop="purchaseOrgId">
                  <el-input-number v-model="form.purchaseOrgId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="采购部门ID" prop="purchaseDeptId">
                  <el-input-number v-model="form.purchaseDeptId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          
          <el-tab-pane label="供应商信息" name="supplier">
            <el-row>
              <el-col :span="12">
                <el-form-item label="供应商ID" prop="supplierId">
                  <el-input v-model="form.supplierId" placeholder="请输入供应商ID" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供应商联系人" prop="providerContact">
                  <el-input v-model="form.providerContact" placeholder="请输入供应商联系人" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="供应商地址" prop="providerAddress">
                  <el-input v-model="form.providerAddress" type="textarea" :rows="2" placeholder="请输入供应商地址" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="结算方ID" prop="settleId">
                  <el-input-number v-model="form.settleId" :min="0" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="收款方ID" prop="chargeId">
                  <el-input-number v-model="form.chargeId" :min="0" :disabled="formMode === 'view'" />
                </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="closeStatus">
                  <el-input v-model="form.closeStatus" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <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-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="网络订单号" prop="netOrderBillNo">
                  <el-input v-model="form.netOrderBillNo" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="版本号" prop="versionNo">
                  <el-input v-model="form.versionNo" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="确认状态" prop="confirmStatus">
                  <el-input v-model="form.confirmStatus" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="变更状态" prop="changeStatus">
                  <el-input v-model="form.changeStatus" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="辅助文本" prop="fQpbzText">
                  <el-input v-model="form.fQpbzText" type="textarea" :rows="3" :disabled="formMode === 'view'" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          
          <el-tab-pane label="订单明细" name="entries">
            <el-table :data="form.entries || []" border stripe max-height="400">
              <el-table-column label="序号" type="index" width="60" align="center" />
              <el-table-column label="采购订单ID" prop="purchaseOrderId" width="120" align="center" />
              <el-table-column label="物料" prop="materialId" min-width="120" align="center" />
              <el-table-column label="单位" prop="unitId" width="100" align="center" />
              <el-table-column label="数量" prop="qty" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.qty }}</span>
                </template>
              </el-table-column>
              <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="taxPrice" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.taxPrice }}</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="allAmount" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.allAmount }}</span>
                </template>
              </el-table-column>
              <el-table-column label="税额" prop="taxAmount" width="100" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.taxAmount }}</span>
                </template>
              </el-table-column>
              <el-table-column label="交货日期" prop="deliveryDate" width="180" align="center">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="交货状态" prop="deliveryStatus" width="100" align="center" />
              <el-table-column label="备注" prop="note" min-width="150" :show-overflow-tooltip="true" />
            </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 { listPurchaseOrder, getPurchaseOrder, delPurchaseOrder, addPurchaseOrder, updatePurchaseOrder } from "@/api/erp/purchaseOrder";

export default {
  name: "PurchaseOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      purchaseOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单模式 edit/view
      formMode: "edit",
      // 当前激活的标签页
      activeTab: "basic",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNo: null,
        documentStatus: null,
        businessType: null,
        supplierId: 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;
      listPurchaseOrder(this.queryParams).then(response => {
        this.purchaseOrderList = 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,
        date: null,
        purchaseOrgId: null,
        purchaseDeptId: null,
        supplierId: null,
        providerContact: null,
        providerAddress: null,
        settleId: null,
        chargeId: null,
        closeStatus: null,
        cancelStatus: null,
        netOrderBillNo: null,
        versionNo: null,
        confirmStatus: null,
        changeStatus: null,
        fQpbzText: null,
        entries: []
      };
      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
      getPurchaseOrder(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
      getPurchaseOrder(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) {
            updatePurchaseOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchaseOrder(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 delPurchaseOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erpPurchaseOrder/export', {
        ...this.queryParams
      }, `purchaseOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


