<template>
  <div class="container">
    <div>
      <el-form :inline="true" :model="reimbursementQurey">
        <el-form-item label="项目名称">
          <el-input
            v-model="reimbursementQurey.projectName"
            placeholder="项目名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="报销人">
          <el-input
            v-model="reimbursementQurey.userName"
            placeholder="报销人"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="reimbursementQurey.reimbursementState" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="query()"
            >查询</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            @click="reset()"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="conter">
      <el-table
        :data="reimbursementList"
        style="width: 100%; text-align: center"
        border
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <div style="padding: 10px">
              <el-descriptions title="报销详情" :column="5" border>
                <el-descriptions-item label="支付方式" :span="5">{{
                  props.row.paymentMethod
                }}</el-descriptions-item>
                <el-descriptions-item label="组长审批状态" :span="2"
                  ><el-tag
                    :type="
                      props.row.reimbursementState == 0
                        ? 'info'
                        : props.row.reimbursementState == 2
                        ? 'danger'
                        : 'success'
                    "
                  >
                    {{
                      props.row.reimbursementState == 0
                        ? "待审批"
                        : props.row.reimbursementState == 2
                        ? "驳回"
                        : "通过"
                    }}
                  </el-tag></el-descriptions-item
                >
                <el-descriptions-item label="组长审批时间" :span="3">{{
                  props.row.headmanTime
                }}</el-descriptions-item>
                <el-descriptions-item label="组长驳回缘由" :span="5">{{
                  props.row.headmanRefute || "无"
                }}</el-descriptions-item>
                <el-descriptions-item label="财务审批状态" :span="2"
                  ><el-tag
                    :type="
                      props.row.reimbursementState == 4
                        ? 'danger'
                        : props.row.reimbursementState == 3
                        ? 'success'
                        : 'info'
                    "
                  >
                    {{
                      props.row.reimbursementState == 4
                        ? "驳回"
                        : props.row.reimbursementState == 3
                        ? "通过"
                        : "待审批"
                    }}
                  </el-tag></el-descriptions-item
                >
                <el-descriptions-item label="财务审批时间" :span="3">{{
                  props.row.financeTime
                }}</el-descriptions-item>
                <el-descriptions-item label="财务驳回缘由" :span="5">{{
                  props.row.finaceRefute || "无"
                }}</el-descriptions-item>
                <el-descriptions-item label="查看详情" :span="5">
                  <el-button
                    @click="getdetails(props.row)"
                    type="primary"
                    style="margin: 5px"
                    size="mini"
                    >查看详情</el-button
                  >
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </template>
        </el-table-column>

        <el-table-column align="center" label="序号" width="50px">
          <template slot-scope="scope">
            {{
              (reimbursementQurey.pageNum - 1) * reimbursementQurey.pageSize +
              scope.$index +
              1
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="userName" label="报销人" />
        <el-table-column align="center" prop="projectName" label="项目" />
        <el-table-column
          align="center"
          prop="applyTotalAmountSmall"
          label="申报总金额(元)"
        />
        <el-table-column align="center" label="核报总金额(元)">
          <template slot-scope="props">
            {{ props.row.verifyTotalAmountSmall || "还未核实金额" }}
          </template>
        </el-table-column>
        <el-table-column label="组长审批人" align="center">
          <template slot-scope="props">
            {{ props.row.headmanName || "暂无" }}
          </template>
        </el-table-column>
        <el-table-column label="财务审批人" align="center">
          <template slot-scope="props">
            {{ props.row.financeName || "暂无" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="fillingTime"
          label="报销日期"
          align="center"
          :formatter="timestampToTime"
        />
        <el-table-column prop="prop" label="发票图片" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="ReviseImage(scope.row)"
              >点击查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="附件下载" align="center">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="selectImage(scope.row)"
              >点击下载</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="reimbursementState" label="状态" align="center">
          <template slot-scope="props">
            <el-tag
              :type="
                props.row.reimbursementState == 0
                  ? 'info'
                  : props.row.reimbursementState == 1 ||
                    props.row.reimbursementState == 3
                  ? 'success'
                  : 'danger'
              "
            >
              {{
                props.row.reimbursementState == 0
                  ? "待小组长通过"
                  : props.row.reimbursementState == 1
                  ? "待财务长通过"
                  : props.row.reimbursementState == 2
                  ? "小组长驳回"
                  : props.row.reimbursementState == 3
                  ? "财务长通过"
                  : "财务长驳回"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.reimbursementState == 0">
              <el-button
                style="margin: 5px"
                size="mini"
                type="success"
                @click="agree(scope.row)"
                >同意</el-button
              >
              <el-button
                style="margin: 5px"
                size="mini"
                type="warning"
                @click="refuse(scope.row)"
                >驳回</el-button
              >
            </div>
            <div v-else style="width：100%;text-align: center;color:#13ce66">
              已操作
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看详情 -->
      <el-drawer
        title="查看详情"
        :visible.sync="drawer"
        :direction="direction"
        size="35%"
      >
        <el-form :inline="true" label-width="80px">
          <el-row>
            <el-col
              v-for="(item, index) in reimbursementDetails.list"
              :key="index"
            >
              <el-form-item :label="item.name">
                <el-input v-model="item.money"></el-input>
              </el-form-item>
              <!-- <el-form-item label="说明">
                <el-input v-model="item.remarks"></el-input>
              </el-form-item> -->
              <el-form-item label="说明">
                <el-input
                  type="textarea"
                  :rows="4"
                  placeholder="请输入内容"
                  v-model="item.remarks"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-drawer>
      <!-- 发票详情 -->
      <el-drawer
        title="发票详情"
        :visible.sync="invoiceShow"
        :direction="direction"
        size="30%"
      >
        <!-- filePath-->
        <el-table
          :data="getinvoivelist"
          style="width: 100%; text-align: center"
          border
        >
          <el-table-column
            type="index"
            label="序号"
            width="80"
          ></el-table-column>
          <el-table-column prop="prop" label="发票详情">
            <template slot-scope="scope">
              <div v-if="formatProtocolType(scope.row)">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.filePath"
                  :preview-src-list="[scope.row.filePath]"
                ></el-image>
              </div>
              <div v-else>
                <a class="a-style" @click="handlePreView(scope.row)"
                  >点击查看文件</a
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-drawer>
    </div>
    <div class="footer">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="reimbursementQurey.pageNum"
        :page-size="reimbursementQurey.pageSize"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 附件的下载与添加 -->
    <div>
      <el-drawer
        title="附件操作"
        :visible.sync="drawer2"
        :direction="direction"
        size="50%"
      >
        <el-button
          type="primary"
          style="margin-left: 5%; margin: 30px 50px"
          disabled
          @click="updatecard"
          >添加附件</el-button
        >

        <el-card
          class="box-card"
          style="width: 90%; margin-left: 5%; margin-top: 20px"
          v-show="getshow == 1"
        >
          <el-row class="row-bg">
            <div
              v-for="index in queryDTOList"
              :key="index.enclosureId"
              class="lsitall"
            >
              <el-col :span="10">
                <div style="margin-top: 6px; margin-left: 40px">
                  附件{{ index.enclosureId }}
                </div>
              </el-col>
              <el-col :span="12" style="margin-left: -50px">
                <el-button
                  type="primary"
                  icon="el-icon-folder-checked"
                  class="a-style"
                  size="mini"
                  style="font-size: 13px"
                  @click="delProject(index)"
                  >下载</el-button
                >

                <el-popover placement="top" width="160" v-model="index.visible">
                  <p>确定要删除此条附件吗</p>
                  <div style="text-align: right; margin: 0">
                    <el-button
                      size="mini"
                      type="text"
                      @click="index.visible = false"
                      >取消</el-button
                    >
                    <el-button
                      type="primary"
                      size="mini"
                      @click="deleteProject(index)"
                      >确定</el-button
                    >
                  </div>
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    class="a-style"
                    size="mini "
                    style="font-size: 13px"
                    slot="reference"
                    disabled
                    >删除</el-button
                  >
                </el-popover>
              </el-col>
            </div>
          </el-row>
        </el-card>
        <!-- 附件上传 -->
        <el-card
          class="box-card"
          style="width: 90%; margin-left: 5%; margin-top: 20px"
          v-show="getshow == 0"
        >
          <div style="width: 60%; margin-left: 30px">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入附件详情"
              v-model="proutlist1.enclosureDescribe"
              style="margin: 20px 0px"
            ></el-input>
            <el-upload
              class="upload-demo"
              ref="upload1"
              drag
              :action="action1"
              :file-list="fileList1"
              :on-remove="delfile1"
              :auto-upload="false"
              :on-change="onChangepdf"
              :on-success="Refresh"
              :data="proutlist1"
              multiple
              style="margin: 20px 0px"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传附件</em>
              </div>
              <div class="el-upload__tip" slot="tip">
                只能上传文件，且不超过500kb
              </div>
            </el-upload>
          </div>
          <div style="margin-left: 75%">
            <el-button @click="remove()">取 消</el-button>
            <el-button
              type="primary"
              @click="determine()"
              style="margin-top: 20px"
              >确 定</el-button
            >
          </div>
        </el-card>

        <div class="box-card" v-show="getshow == 3"></div>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { selectByHeadman, headamApproval } from "@/api/reimbursement";
import { selectByReimbursementId } from "@/api/reimbursementDetails";
import {
  selectAllById,
  deletBystatus,
  selectByIdF,
  deleteById,
} from "@/api/project";
export default {
  data() {
    return {
      //抽屉
      drawer: false,
      direction: "rtl",
      reimbursementDetails: {
        list: [],
      },
      options: [
        {
          value: "0",
          label: "待小组长通过",
        },
        {
          value: "1",
          label: "待财务长通过",
        },
        {
          value: "2",
          label: "小组长驳回",
        },
        {
          value: "3",
          label: "财务长通过",
        }, {
          value: "4",
          label: "财务长驳回",
        }
      ],
      reimbursementQurey: {
        userId: null,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      reimbursementList: [],
      from: {
        reimbursementId: null,
        headmanId: null,
        headmanRefute: "",
        headmanTime: null,
        reimbursementState: null, //0待审批 1小组长通过 2小组长驳回 3财务通过 4财务驳回
      },
      invoiceShow: false,
      getinvoivelist: [],
      //查询图片给的值
      getinvoiveId: {
        companyId: null,
        reimbursementId: 0,
      },
      //附件
      drawer: false,
      direction: "rtl",
      fileList1: [], //合同文件
      disabled: false, //隐藏
      getshow: 1, //判断show的显示与隐藏,
      //查询附件
      queryDTO: {
        companyId: null,
        reimbursementId: null,
      },
      queryDTOList: [], //收集附件的数据
      //附件抽屉
      drawer2: false,
      //发票对话框
      dialogFormVisible: false,
      invoiceListAdd: [], //查询发票的集合
      getFormslist: {
        userId: null,
        companyId: null,
        type: 0,
      }, //查询的能选择的表单信息
      getListincom: [], //收集选择的数组
      incomeIdgrt: null, //收集incomeId数据
      getshow: 1, //判断show的显示与隐藏,
      //合同文件上传携带参数
      proutlist1: {
        Id: null,
        enclosureDescribe: "",
        status: 2, //0收入 1支出
        companyId: null,
        userId: null,
      },
      fileTypepdf: false, //判断合同文件是否存在
      fileList1: [], //附件文件
      disabled: false, //隐藏
    };
  },
  created() {
    this.getinvoiveId.companyId = this.$store.getters.companyId;
    this.reimbursementQurey.userId = this.$store.getters.urId;
    this.queryDTO.companyId = this.$store.getters.companyId;
    this.action1 = process.env.VUE_APP_BASE_API + "/file/updataEnclosures"; //保存附件与下载的接口
    this.getReimbursement();
  },
  methods: {
     //重置
     reset(){
      this.reimbursementQurey = {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        companyId: null,
      };
      this.reimbursementQurey.userId = this.$store.getters.urId;
      this.reimbursementQurey.companyId = this.$store.getters.companyId;
      this.getReimbursement();
    },
    query(){
      this.reimbursementQurey.pageNum = 1
      this.getReimbursement();
    },
    //图片格式
    formatProtocolType(row) {
      if (row.filePath == null) {
        return flase;
      }else{
        let fileNamepdf=row.filePath;
        let fileType = fileNamepdf.substring(fileNamepdf.lastIndexOf("."));
        if (fileType != ".pdf") {
          return true;
        } else {
          return false;
        }
      }
    },
    //获取pdf格式
    handlePreView(row){
  const photopath =row.filePath;
   window.open(photopath,"_blank")
    },
    //查看详情
    getdetails(data) {
      this.drawer = true;
      let param = {
        reimbursementId: data.reimbursementId,
      };
      selectByReimbursementId(param)
        .then((res) => {
          if (res.success) {
            //获取数据
            this.reimbursementDetails.list = res.data;
            this.$message.success(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error("查询失败");
        });
    },
    //获取申请报销列表
    getReimbursement() {
      selectByHeadman(this.reimbursementQurey)
        .then((res) => {
          if (res.success) {
            this.reimbursementList = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error("回去数据失败");
        });
    },
    //换页
    handleCurrentChange(val) {
      this.reimbursementQurey.pageNum = val;
      this.getReimbursement();
    },
    agree(data) {
      this.$confirm("是否同意此报销单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //处理数据
          // console.log(value.value)
          this.from.reimbursementId = data.reimbursementId;
          this.from.headmanId = this.$store.getters.urId;
          this.from.headmanRefute = "";
          this.from.headmanTime = this.time();
          this.from.reimbursementState = 1;
          headamApproval(this.from)
            .then((res) => {
              if (res.success) {
                this.$message.success("操作成功");
                this.getReimbursement();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((error) => {
              this.$message.error("操作失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消操作",
          });
        });
    },
    //驳回
    refuse(data) {
      this.$prompt("驳回理由", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputErrorMessage: "驳回理由必填",
        type: "warning",
        inputValidator: (value) => {
          if (value) {
            return true;
          } else {
            return false;
          }
        },
      })
        .then((value) => {
          //处理数据
          this.from.reimbursementId = data.reimbursementId;
          this.from.headmanId = this.$store.getters.urId;
          this.from.headmanRefute = value.value || "";
          this.from.headmanTime = this.time();
          this.from.reimbursementState = 2;
          headamApproval(this.from)
            .then((res) => {
              if (res.success) {
                this.$message.success("操作成功");
                this.getReimbursement();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((error) => {
              this.$message.error("操作失败");
            });
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "取消操作",
          });
        });
    },
    //时间处理 yyyy-MM-dd hh:mm:ss
    time() {
      var date = new Date();
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    },
    //时间处理
    timestampToTime(row, column, cellValue) {
      return cellValue.split(" ")[0];
    },
    //查看图片的方法
    ReviseImage(row) {
      this.invoiceShow = true;
      this.getinvoiveId.reimbursementId = row.reimbursementId;
      this.Selectinsertlsit();
    },
    //查询发票图片
    Selectinsertlsit() {
      selectAllById(this.getinvoiveId)
        .then((res) => {
          if (res.success) {
            this.getinvoivelist = res.data;
            this.getinvoivelist.forEach((e) => {
              e.filePath = process.env.VUE_APP_BASE_API + e.filePath;
            });
            console.log(this.getinvoivelist);
          } else {
            this.$message.error(res.msg);
            this.back();
          }
        })
        .catch((error) => {
          this.$message.error("获取发票图片失败");
          this.back();
        });
    },
    //查看项目附件.....
    selectImage(row) {
      console.log(row);
      this.queryDTO.reimbursementId = row.reimbursementId;
      this.proutlist1.Id = row.reimbursementId;
      selectByIdF(this.queryDTO)
        .then((res) => {
          if (res.success) {
            this.queryDTOList = res.data;
            if (this.queryDTOList.length > 0) {
              this.drawer2 = true;
              this.getshow = 1;
              this.disabled = false;
            } else {
              this.drawer2 = true;
              this.disabled = false;
              this.getshow = 3;
            }
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error("获取附件文件失败");
        });
    },
    //下载附件
    delProject(row) {
      var a = document.createElement("a"); //创建一个<a></a>标签
      a.href = process.env.VUE_APP_BASE_API + row.enclosureFile; // 给a标签的href属性值加上地址，注意，这里是绝对路径，不用加 点.
      //a.download = row.enclosureFile; //设置下载文件文件名，这里加上.xlsx指定文件类型，pdf文件就指定.fpd即可
      a.target = "_blank";
      a.style.display = "none"; // 障眼法藏起来a标签
      document.body.appendChild(a); // 将a标签追加到文档对象中
      a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
      a.remove(); // 一次性的，用完就删除a标签
    },
    //显示添加附件
    updatecard() {
      this.getshow = 0;
      this.disabled = true;
      this.delfile1();
    },
    //删除附件
    deleteProject(row) {
      deleteById(row)
        .then((res) => {
          if (res.success) {
            this.selectImage(row);
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.visible = false;
            console.log(res.msg);
          }
        })
        .catch((errror) => {
          this.$message.error(res.msg);
        });
    },
    //清除附件文件
    delfile1(file) {
      this.fileList1 = this.fileList1.filter((item) => item.uid !== file.uid);
      this.proutlist1.enclosureDescribe = ""; //初始化数据
      this.fileTypepdf = false;
    },
    //确定提交附件
    determine() {
      //判断是否有附件文件
      if (this.fileTypepdf) {
        this.proutlist1.userId = this.$store.getters.urId;
        this.proutlist1.companyId = this.$store.getters.companyId;
        this.$refs.upload1.submit();
        this.getshow = 1; //隐藏与显示
        this.disabled = false;
        this.$message({
          showClose: true,
          message: "上传附件成功",
          type: "success",
        });
        this.Refresh();
      } else {
        this.$message.error("请添加附件，在进行提交");
      }
    },
    //刷新附件
    Refresh() {
      console.log(this.proutlist1.Id);
      this.queryDTO.reimbursementId = this.proutlist1.Id;
      console.log(this.proutlist1.Id);
      selectByIdF(this.queryDTO)
        .then((res) => {
          if (res.success) {
            this.queryDTOList = res.data;
            this.proutlist1.enclosureDescribe = ""; //初始化数据
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error("获取附件文件失败");
        });
    },
    //附件取消
    remove() {
      if (this.queryDTOList.length > 0) {
        this.getshow = 1;
        this.disabled = false;
        this.delfile1();
      } else {
        this.getshow = 3;
        this.disabled = false;
        this.delfile1();
      }
    },
    //附件文件校验
    onChangepdf(file) {
      console.log(this.proutlist1);
      const fileNamepdf = file.name;
      let fileType = fileNamepdf.substring(fileNamepdf.lastIndexOf("."));
      if (fileType != "") {
        this.fileTypepdf = true;
      } else {
        this.$message.warning("附件只能只能上传文件");
        this.$refs.upload1.clearFiles();
        this.fileTypepdf = false;
      }
    },
    //删除关系
    delinvoice(data) {
      deletBystatus(data).then((res) => {
        if (res.success) {
          this.getinvoive(data);
        }
      });
    },
  },
};
</script>

<style scoped>
.container {
  padding: 10px;
}

.footer {
  text-align: right;
}
.lsitall .el-col {
  margin: 20px 10px;
}
.lsitall .el-button {
  margin-left: 16px;
}
.row-bg {
  margin-left: 15%;
}
.dialog-footer {
  width: 90%;
  margin: 20px 5%;
}
.lsitall .el-col {
  margin: 20px 10px;
}
.lsitall .el-button {
  margin-left: 16px;
}
.row-bg {
  margin-left: 15%;
}
.dialog-footer {
  width: 90%;
  margin: 20px 5%;
}
</style>