<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="产品名称">
        <el-input
          v-model="queryParams.产品名称"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="采购入库单">
        <el-input
          v-model="queryParams.采购入库单"
          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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table :height="740" v-loading="loading" :data="应付账款List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="产品名称" />
      <el-table-column label="单位" align="center" prop="单位" />
      <el-table-column label="采购数量" align="center" prop="采购数量" />
      <el-table-column label="实际采购价含税" align="center" prop="实际采购价含税" />
      <el-table-column label="实际采购价不含税" align="center" prop="实际采购价不含税" />
      <el-table-column label="应付状态" align="center" prop="应付状态" />
      <el-table-column label="应付金额" align="center" prop="应付金额" />
      <el-table-column label="应付对账时间" align="center" prop="应付对账时间" />
      <el-table-column label="付款状态" align="center" prop="付款状态" />
      <el-table-column label="付款金额" align="center" prop="付款金额" />
      <el-table-column label="付款时间" align="center" prop="付款时间" />
      <el-table-column label="采购入库单" align="center" prop="采购入库单" />
      <el-table-column label="本次入库总数" align="center" prop="本次入库总数" />
      <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)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品名称" prop="产品名称">
          <el-input v-model="form.产品名称" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="单位" prop="单位">
          <el-input v-model="form.单位" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="采购数量" prop="采购数量">
          <el-input v-model="form.采购数量" placeholder="请输入采购数量" />
        </el-form-item>
        <el-form-item label="实际采购价含税" prop="实际采购价含税">
          <el-input v-model="form.实际采购价含税" placeholder="请输入实际采购价含税" />
        </el-form-item>
        <el-form-item label="实际采购价不含税" prop="实际采购价不含税">
          <el-input v-model="form.实际采购价不含税" placeholder="请输入实际采购价不含税" />
        </el-form-item>
        <el-form-item label="应付状态" prop="应付状态">
          <el-input v-model="form.应付状态" placeholder="请输入应付状态" />
        </el-form-item>
        <el-form-item label="应付金额" prop="应付金额">
          <el-input v-model="form.应付金额" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="应付对账时间" prop="应付对账时间">
          <el-input v-model="form.应付对账时间" placeholder="请输入应付对账时间" />
        </el-form-item>
        <el-form-item label="付款状态" prop="付款状态">
          <el-input v-model="form.付款状态" placeholder="请输入付款状态" />
        </el-form-item>
        <el-form-item label="付款金额" prop="付款金额">
          <el-input v-model="form.付款金额" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="付款时间" prop="付款时间">
          <el-input v-model="form.付款时间" placeholder="请输入付款时间" />
        </el-form-item>
        <el-form-item label="采购入库单" prop="采购入库单">
          <el-input v-model="form.采购入库单" placeholder="请输入采购入库单" />
        </el-form-item>
        <el-form-item label="本次入库总数" prop="本次入库总数">
          <el-input v-model="form.本次入库总数" placeholder="请输入本次入库总数" />
        </el-form-item>
      </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 { list应付账款, get应付账款, del应付账款, add应付账款, update应付账款 } from "@/api/system/应付账款";

export default {
  name: "Yfzk",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      应付账款List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        产品名称: null,
        单位: null,
        采购数量: null,
        实际采购价含税: null,
        实际采购价不含税: null,
        应付状态: null,
        应付金额: null,
        应付对账时间: null,
        付款状态: null,
        付款金额: null,
        付款时间: null,
        采购入库单: null,
        本次入库总数: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      list应付账款(this.queryParams).then(response => {
        this.应付账款List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        产品名称: null,
        单位: null,
        采购数量: null,
        实际采购价含税: null,
        实际采购价不含税: null,
        应付状态: null,
        应付金额: null,
        应付对账时间: null,
        付款状态: null,
        付款金额: null,
        付款时间: null,
        采购入库单: null,
        本次入库总数: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.产品名称)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const 产品名称 = row.产品名称 || this.ids
      get应付账款(产品名称).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.产品名称 != null) {
            update应付账款(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add应付账款(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const 产品名称s = row.产品名称 || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + 产品名称s + '"的数据项？').then(function() {
        return del应付账款(产品名称s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/应付账款/export', {
        ...this.queryParams
      }, `应付账款_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
