<template>
  <div class="app-container">
   
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="所属部门" prop="deptId">
              <treeselect style="width:205.4px" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属部门" />
          </el-form-item>
          <el-form-item label="付款单号" prop="payCode">
            <el-input
              v-model="queryParams.payCode"
              placeholder="请输入付款单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="交易单号" prop="bankTransactionId">
            <el-input
              v-model="queryParams.bankTransactionId"
              placeholder="请输入交易单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="银行单号" prop="bankSerialNo">
            <el-input
              v-model="queryParams.bankSerialNo"
              placeholder="请输入银行单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>


      <el-form-item label="员工姓名" prop="companyUserNickName">
          <el-input
            v-model="queryParams.companyUserNickName"
            placeholder="请输入员工姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>

          <el-form-item label="提交时间" prop="createTime">
            <el-date-picker clearable size="small" style="width: 205.4px"
              v-model="queryParams.createTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择提交时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="支付时间" prop="payTime">
            <el-date-picker
            style="width: 215.4px"
              clearable size="small"
              v-model="dateRange"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="退款时间" prop="refundTime">
              <el-date-picker 
                style="width: 215.4px"
                clearable size="small" 
                v-model="refundDateRange"
                type="daterange"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>


          <el-form-item label="状态" prop="status">
            <el-select   v-model="queryParams.status" placeholder="请选择状态" clearable size="small" >
            <el-option
                    v-for="item in statusOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" 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="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['store:storePayment:export']"
            >导出</el-button>
          </el-col>
        
        </el-row>

        <el-table height="500" border v-loading="loading" :data="storePaymentList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="paymentId" /> 
          <el-table-column label="付款单号" align="center" prop="payCode" width="120px" />
          <el-table-column label="会员手机号" align="center" prop="userPhone"  width="120px" />
          <el-table-column label="支付类型" align="center" prop="payTypeCode" />
          <el-table-column label="支付金额" align="center" prop="payMoney" />
          <el-table-column label="退款金额" align="center" prop="refundMoney" />
          <el-table-column label="交易单号" align="center" prop="bankTransactionId" />
          <el-table-column label="银行单号" align="center" prop="bankSerialNo" />
          <el-table-column label="所属公司" align="center" prop="companyName" />
          <el-table-column label="所属部门" align="center" prop="deptName" />
          <el-table-column label="员工" align="center" prop="companyUserNickName" />
          <el-table-column label="状态" align="center" prop="status" >
              <template slot-scope="scope">
                  <el-tag prop="status" v-for="(item, index) in statusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
              </template>
          </el-table-column>
          <el-table-column label="提交时间" align="center" prop="createTime" width="180">
          </el-table-column>
          <el-table-column label="支付时间" align="center" prop="payTime" width="180">
          </el-table-column>
          <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" fixed="right" 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)"
                v-hasPermi="['store:storePayment:edit']"
              >修改</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="100px">
        <el-form-item label="支付订单号" prop="payCode">
          <el-input v-model="form.payCode" disabled placeholder="请输入支付订单号" />
        </el-form-item>
        <el-form-item label="支付类型" prop="payTypeCode">
          <el-input v-model="form.payTypeCode" disabled placeholder="请输入支付类型" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payMoney">
          <el-input v-model="form.payMoney" disabled placeholder="请输入支付金额" />
        </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"  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 { listStorePayment, getStorePayment, delStorePayment, addStorePayment, updateStorePayment, exportStorePayment } from "@/api/store/storePayment";
import { treeselect } from "@/api/company/companyDept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "StorePayment",
  components: { Treeselect },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      
      statusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dateRange:[],
      refundDateRange:[],
      // 总条数
      total: 0,
      // 支付明细表格数据
      storePaymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payCode: null,
        payTypeCode: null,
        payMoney: null,
        payTime: null,
        tradeNo: null,
        userId: null,
        openId: null,
        businessType: null,
        businessOrderId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
        this.$refs.tree.filter(val);
      },
  },
  created() {
    this.getTreeselect();
    this.getDicts("store_payment_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    /** 查询支付明细列表 */
    getList() {
      this.loading = true;
      if(this.refundDateRange!=null&&this.refundDateRange.length==2){
        this.queryParams.refundBeginTime=this.refundDateRange[0];
        this.queryParams.refundEndTime=this.refundDateRange[1];
      }
      else{
        this.queryParams.refundBeginTime=null;
        this.queryParams.refundEndTime=null;
      }
      listStorePayment(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.storePaymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paymentId: null,
        payCode: null,
        payTypeCode: null,
        payMoney: null,
        payTime: null,
        createTime: null,
        tradeNo: null,
        userId: null,
        openId: null,
        businessType: null,
        businessOrderId: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paymentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paymentId = row.paymentId || this.ids
      getStorePayment(paymentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支付明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.paymentId != null) {
            updateStorePayment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addStorePayment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paymentIds = row.paymentId || this.ids;
      this.$confirm('是否确认删除支付明细编号为"' + paymentIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStorePayment(paymentIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有支付明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStorePayment(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
