<template>
  <div>
    <!--条件-->
    <el-form :model="listQuery" ref="queryForm" :inline="true">
      <el-form-item label="同行单信息">
        <el-input
          v-model="listQuery.billInfo"
          placeholder="请输入同行单号码或名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData('search')">查询</el-button
        ><el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="right-button">
      <el-button
        type="primary"
        class="lake-Blue"
        icon="el-icon-plus"
        @click="handleAddOrEdit('create')"
        >新增票据
      </el-button>
    </div>
    <!--列表-->
    <div>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中"
        fit
        stripe
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="票据名称" prop="billName" />
        <el-table-column label="票据金额" prop="billAmt" />
        <el-table-column label="票据单号" prop="invoNum" />
        <el-table-column label="开票时间" prop="billDate" />
        <el-table-column label="上传时间" prop="upldDate" />
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              class="cyan"
              type="text"
              @click="handleAddOrEdit('update', scope.row)"
              >编辑</el-button
            >
            <el-button class="red" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <yl-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getData"
    />

    <!--添加/修改-->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="420px"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="80px"
      >
        <el-form-item label="票据名称" prop="billName" :rules="rules.required">
          <el-input v-model.trim="temp.billName" />
        </el-form-item>
        <el-form-item label="票据金额" prop="billAmt" :rules="rules.numPot4">
          <el-input v-model.trim="temp.billAmt" />
        </el-form-item>
        <el-form-item label="票据单号" prop="invoNum" :rules="rules.required">
          <el-input v-model.trim="temp.invoNum" />
        </el-form-item>
        <el-form-item
          label="开票时间"
          prop="billDate"
          :rules="rules.dateRequired"
        >
          <el-date-picker
            v-model="temp.billDate"
            type="date"
            placeholder="请选择开票时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          label="发票文件"
          prop="fileId"
          :rules="rules.uploadRequired"
        >
          <yl-upload
            :fileId="temp.fileId"
            @getUpload="getUpload"
            urlName="acco"
          >
            <template v-slot:dec>只能上传pdf文件</template>
          </yl-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="commitData()"> 确认 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
import rules from "@/utils/rules";
import ylUpload from "@/components/yl-upload";
import ylPagination from "@/components/yl-pagination";
import {
  getBillPage,
  saveBill,
  deleteBill
} from "@/api/twovotesManage-sup/index.js";

export default {
  components: {
    ylPagination,
    ylUpload
  },

  data() {
    return {
      rules,
      menuData: [], //菜单
      list: null, //列表
      total: 0, //分页
      listLoading: true,
      listQuery: {
        //条件查询,
        billType: "acco",
        billInfo: "",
        current: 1,
        size: 10
      },
      dialogFormVisible: false, //是否对话框
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "添加"
      },
      temp: {}
    };
  },

  created() {
    this.getData();
  },

  methods: {
    // 上传文件
    getUpload(id) {
      this.temp.fileId = id;
      if (id) {
        this.$refs.dataForm.clearValidate("fileId");
      } else {
        this.$refs.dataForm.validateField("fileId");
      }
    },

    // 重置页面
    reset() {
      this.listQuery.billInfo = "";
      this.getData();
    },

    //获取列表数据
    getData(type) {
      if (type === "search") {
        this.listQuery.current = 1;
      }

      this.listLoading = true;
      getBillPage(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.list.forEach((item) => {
            item.billDate = moment(item.billDate).format("YYYY-MM-DD");
          });
          this.total = response.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },

    //提交
    commitData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveBill(this.temp)
            .then((res) => {
              if (res.success == true) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.getData();
              }
            })
            .catch(() => {});
        }
      });
    },

    //添加/修改对话框
    handleAddOrEdit(type, row) {
      if (type === "update") {
        this.temp = {
          id: row.id,
          billName: row.billName,
          billAmt: row.billAmt,
          invoNum: row.invoNum,
          billDate: row.billDate,
          fileId: row.fileId,
          billType: "acco"
        };
      } else {
        this.temp = {
          id: "",
          billName: "",
          billAmt: "",
          invoNum: "",
          billDate: "",
          fileId: "",
          billType: "acco"
        };
      }

      this.dialogStatus = type;
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },

    //删除
    handleDelete(row) {
      this.$confirm("确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteBill({ id: row.id }).then((res) => {
          if (res.success == true) {
            this.$message({
              message: "操作成功",
              type: "success"
            });
            this.getData();
          }
        });
      });
    }
  }
};
</script>

<style scoped>
</style>
