<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="结算单号" prop="settlementNumber">
        <el-input
          v-model="queryParams.settlementNumber"
          placeholder="请输入结算单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算名称" prop="settlementName">
        <el-input
          v-model="queryParams.settlementName"
          placeholder="请输入结算名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算时间" prop="settlementTime">
        <el-date-picker clearable
                        v-model="queryParams.settlementTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结算时间">
        </el-date-picker>
      </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="['business:settlementMaterial: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="['business:settlementMaterial: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="['business:settlementMaterial: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="['business:settlementMaterial:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settlementMaterialList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="结算单号" align="center" prop="settlementNumber" />
      <el-table-column label="结算名称" align="center" prop="settlementName" />
      <el-table-column label="结算时间" align="center" prop="settlementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="vendorName" />
      <el-table-column label="结算金额" align="center" prop="settlementMoney" />
      <el-table-column label="结算方式" align="center" prop="settlementType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_material_type" :value="scope.row.settlementType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:settlementMaterial:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:settlementMaterial: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>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="结算单号" prop="settlementNumber">
          <el-input v-model="form.settlementNumber" placeholder="请输入结算单号" />
        </el-form-item>
        <el-form-item label="结算名称" prop="settlementName">
          <el-input v-model="form.settlementName" placeholder="请输入结算名称" />
        </el-form-item>
        <el-form-item label="结算时间" prop="settlementTime">
          <el-date-picker clearable
            v-model="form.settlementTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商ID" prop="vendorId">
          <el-input v-model="form.vendorId" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="vendorName">
          <el-input v-model="form.vendorName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="结算金额" prop="settlementMoney">
          <el-input v-model="form.settlementMoney" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出库结算单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="80%" append-to-body>
      <el-scrollbar style="height: 75vh;">
        <span style="font-weight: bold; font-size: 18px">结算信息</span>
        <el-divider />
        <el-descriptions>
          <el-descriptions-item label="结算单号">{{form.settlementNumber}}</el-descriptions-item>
          <el-descriptions-item label="结算名称">{{form.settlementName}}</el-descriptions-item>
          <el-descriptions-item label="结算时间">{{form.settlementTime}}</el-descriptions-item>
          <el-descriptions-item label="供应商">{{form.vendorName}}</el-descriptions-item>
          <el-descriptions-item label="结算方式">
            <dict-tag :options="dict.type.settlement_material_type" :value="form.settlementType" />
          </el-descriptions-item>
          <el-descriptions-item label="结算金额">{{form.settlementMoney}}</el-descriptions-item>
        </el-descriptions>
        <span style="font-weight: bold; font-size: 18px">结算明细</span>
        <el-divider />
        <el-table :data="form.pickingList" default-expand-all>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-table :data="props.row.settlementMaterialInfoList">
                <el-table-column label="分类名称" align="center" prop="materialTypeName" />
                <el-table-column label="货位类型" align="center" prop="warehouseLocationType" />
                <el-table-column label="供应商" align="center" prop="vendorName" />
                <el-table-column label="物资编码" align="center" prop="materialCode" />
                <el-table-column label="物资名称" align="center" prop="materialName" />
                <el-table-column label="物资规格" align="center" prop="materialSpecifications" />
                <el-table-column label="物资单位" align="center" prop="materialUnit" />
                <el-table-column label="质保期" align="center" prop="materialWarrantyPeriod" />
                <el-table-column label="数量" align="center" prop="settlementNumber" width="120px" />
                <el-table-column label="单价" align="center" prop="settlementPrice" width="120px" />
                <el-table-column label="金额" align="center" prop="settlementMoney" width="120px" />
              </el-table>
            </template>
          </el-table-column>
          <el-table-column label="出库单号" align="center" prop="outNumber" />
          <el-table-column label="报修单号" align="center" prop="repairNumber" />
          <el-table-column label="车牌号" align="center" prop="plateNumber" />
          <el-table-column label="出库金额" align="center" prop="outMoney" />
          <el-table-column label="领料人" align="center" prop="receiveUserName" />
          <el-table-column label="领料时间" align="center" prop="receiveTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="state">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.picking_state" :value="scope.row.state"/>
            </template>
          </el-table-column>
          <el-table-column label="出库时间" align="center" prop="outTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </el-dialog>

  </div>
</template>

<script>
import { listSettlementMaterial, getSettlementMaterial, delSettlementMaterial, addSettlementMaterial, updateSettlementMaterial } from "@/api/business/settlementMaterial";

export default {
  name: "SettlementMaterial",
  dicts: ['settlement_material_type', 'settlement_state','picking_state'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算物料表格数据
      settlementMaterialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settlementNumber: undefined,
        settlementName: undefined,
        settlementTime: undefined,
        vendorName: undefined,
        state: undefined,
        createDeptId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        settlementNumber: [
          { required: true, message: "结算单号不能为空", trigger: "blur" }
        ],
        settlementName: [
          { required: true, message: "结算名称不能为空", trigger: "blur" }
        ],
        settlementTime: [
          { required: true, message: "结算时间不能为空", trigger: "blur" }
        ],
        vendorId: [
          { required: true, message: "供应商ID不能为空", trigger: "blur" }
        ],
        settlementType: [
          { required: true, message: "结算方式不能为空", trigger: "change" }
        ],
        settlementMoney: [
          { required: true, message: "结算金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询结算物料列表 */
    getList() {
      this.loading = true;
      listSettlementMaterial(this.queryParams).then(response => {
        this.settlementMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        settlementNumber: undefined,
        settlementName: undefined,
        settlementTime: undefined,
        vendorId: undefined,
        vendorName: undefined,
        settlementType: undefined,
        settlementMoney: undefined,
        state: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createDeptId: undefined
      };
      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
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      getSettlementMaterial(row.id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "出库结算单详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算物料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSettlementMaterial(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改结算物料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSettlementMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSettlementMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除结算物料编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSettlementMaterial(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/settlementMaterial/export', {
        ...this.queryParams
      }, `settlementMaterial_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
