<template>
  <my-page>
    <template slot="left">
      <CustList style="width: 320px;" @change="handleCustsChange"></CustList>
    </template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="单据号" prop="billNo">
          <el-input class="filter-input" v-model="queryParams.billNo" placeholder="请输入单据号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginDate">
          <el-date-picker class="filter-input" v-model="queryParams.beginDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker class="filter-input" v-model="queryParams.endDate" value-format="yyyy-MM-dd" type="date" 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" style="height: 28px;padding-top: 3px;">
          <el-radio-group v-model="queryParams.rpFlag" @change="handleQuery">
            <el-radio label="">全部</el-radio>
            <el-radio label="0">收入</el-radio>
            <el-radio label="1">支出</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="1.5">
          <el-tooltip class="item" effect="dark" :disabled="!!cust.id" content="请先选择客户" placement="top">
            <el-button type="primary" :disabled="!cust.id" plain icon="el-icon-plus" size="mini" @click="handleAdd('0')"
            v-hasPermi="['finance:cash:add']">新增收入</el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="1.5">
          <el-tooltip class="item" effect="dark" :disabled="!!cust.id" content="请先选择客户" placement="top">
            <el-button type="primary" :disabled="!cust.id" plain icon="el-icon-plus" size="mini" @click="handleAdd('1')"
            v-hasPermi="['finance:cash:add']">新增支出</el-button>
          </el-tooltip>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['finance:cash: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="['finance:cash: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="['finance:cash:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="cashList" :highlight-current-row="true" @sort-change="handleSortChange"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" type="index" width="55" align="center" />
        <el-table-column label="单据号" align="center" prop="billNo" sortable='custom' />
        <el-table-column label="收/付款单位" align="center" prop="custName" sortable='custom' />
        <el-table-column label="收/支" align="center" prop="rpFlag" >
          <template slot-scope="scope">
            <el-tag :type="scope.row.rpFlag == 1 ? 'success':''" size="small">{{ scope.row.rpFlag == 0 ? '收' : '支' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="收支金额" align="center" prop="paidMoney" sortable='custom' />
        <el-table-column label="币种" align="center" prop="currencyCode" sortable='custom'>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_curr_type" :value="scope.row.currencyCode" />
          </template>
        </el-table-column>
        <el-table-column label="已核销金额" align="center" prop="hxAmount" sortable='custom' />
        <el-table-column label="未核销金额" align="center" prop="whxAmount" sortable='custom' />
        <el-table-column label="收支时间" align="center" prop="paidTime" width="180" sortable='custom'>
        </el-table-column>
        <!-- <el-table-column label="经办人" align="center" prop="userId" sortable='custom' /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleRead(scope.row)"
            v-hasPermi="['finance:cash:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['finance:cash:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['finance:cash:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination hide-on-single-page :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改现金日记账对话框 -->
      <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
        <CashEdit ref="edit" v-if="open" :rpFlag="rpFlag" :cust="cust" :bid="showId" :readonly="readonly" @saved="refresh"></CashEdit>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!readonly">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </my-page>
</template>

<script>
import { pageListCash, getCash, delCash, addCash, updateCash } from "@/api/finance/cash";
import CustList from '@/views/tm/components/CustList';
import CashEdit from '@/views/finance/components/CashEdit';

export default {
  name: "Cash",
  dicts: ['sys_curr_type'],
  components: {
    CustList,
    CashEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 现金日记账表格数据
      cashList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custId: null,
        billNo: null,
        beginDate: null,
        endDateL: null,
        rpFlag: '',
        orderByColumn: 'paidTime',
        isAsc: 'DESC',
      },
      showId: null,
      readonly: false,
      cust: {},
      rpFlag: '0'
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleCustsChange(custs) {
      const cust = custs[0] || {};
      this.queryParams.custId = cust.id;
      this.cust = cust;
      this.handleQuery();
    },
    /** 查询现金日记账列表 */
    getList() {
      this.loading = true;
      pageListCash(this.queryParams).then(response => {
        this.cashList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.names = selection.map(item => item.billNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleRead(row) {
      const id = row.id || this.ids;
      this.open = true;
      this.showId = id;
      this.readonly = true;
      this.title = '查看现金日记账';
    },
    /** 新增按钮操作 */
    handleAdd(type) {
      this.rpFlag = type;
      this.open = true;
      this.showId = '';
      this.readonly = false;
      this.title = "新增现金日记账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.open = true;
      this.showId = id;
      this.readonly = false;
      this.title = "编辑现金日记账";
      
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.edit.submitForm();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.billNo || this.names;
      this.$modal.confirm('是否确认删除单据号为"' + names + '"的日记账？').then(function () {
        return delCash(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/cash/export', {
        ...this.queryParams
      }, `cash_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
