<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <el-form-item label="公司名" prop="companyId">
          <el-select filterable v-model="queryParams.companyId" placeholder="请选择公司名" clearable size="small">
              <el-option
                v-for="item in companys"
                :key="item.companyId"
                :label="item.companyName"
                :value="item.companyId"/>
          </el-select>
      </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="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="外部订单" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          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="mobile">
        <el-input
          v-model="queryParams.mobile"
          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="createTimeRange">
        <el-date-picker
          style="width: 215.4px"
          clearable size="small"
          v-model="createTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-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 label="小程序" prop="appId">
        <el-select v-model="queryParams.appId" placeholder="请选择所属小程序" clearable size="small">
          <el-option
            v-for="dict in appMallOptions"
            :key="dict.appid"
            :label="dict.name + '(' + dict.appid + ')'"
            :value="dict.appid"
          />
        </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-col :span="1.5">
        <el-button
          type="warning"
          size="mini"
          @click="handlePayNotify"
          v-hasPermi="['store:storePayment:payNotify']"
        >同步订单状态</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs type="card" v-model="queryParams.businessType" @tab-click="handleClick">
      <el-tab-pane label="收款记录" name="1"></el-tab-pane>
      <el-tab-pane label="订单支付记录" name="2"></el-tab-pane>
    </el-tabs>
    <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="orderCode"  width="120px" />
      <el-table-column label="外部订单号" align="center" prop="tradeNo"  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="payMode" >
        <template slot-scope="scope">
          <el-tag prop="status" v-for="(item, index) in payModeOptions"    v-if="scope.row.payMode==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <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="miniProgramName" width="120px" />
      <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" width="120px" 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>
          <el-button
            v-if="scope.row.status==1&&scope.row.businessType==1"
            size="mini"
            type="text"
            @click="handleRefund(scope.row)"
            v-hasPermi="['store:storePayment:refund']"
          >退款</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="payNotify.title" :visible.sync="payNotify.open" width="600px" append-to-body>
      <el-form ref="payNotifyForm" :model="payNotifyForm" :rules="payNotifyRules" label-width="100px">
        <el-form-item label="外部单号" prop="tradeNo"  >
          <el-input v-model="payNotifyForm.tradeNo" placeholder="请输入物流单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPayNotifyForm">确 定</el-button>
        <el-button @click="payNotify.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改支付明细对话框 -->
    <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>
    <el-dialog :title="refund.title" :visible.sync="refund.open" width="500px" append-to-body>
      <el-form ref="refundForm" :model="refundForm" :rules="refundRules" label-width="100px">
        <el-form-item label="支付订单号" prop="payCode">
          <el-input v-model="refundForm.payCode" disabled placeholder="请输入支付订单号" />
        </el-form-item>
        <el-form-item label="支付类型" prop="payTypeCode">
          <el-input v-model="refundForm.payTypeCode" disabled placeholder="请输入支付类型" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payMoney">
          <el-input v-model="refundForm.payMoney" disabled placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundMoney">
          <el-input-number :min="0" v-model="refundForm.refundMoney"  placeholder="请输入退款金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRefundForm">确 定</el-button>
        <el-button @click="cancelRefund">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { refundStorePayment,listStorePayment, getStorePayment, delStorePayment, addStorePayment, updateStorePayment, exportStorePayment,setPayNotify } from "@/api/hisStore/storePayment";
import { getCompanyList } from "@/api/company/company";
import {list as getAppMallOptions} from '@/api/course/coursePlaySourceConfig';
export default {
  name: "HisStorePayment",
  data() {
    return {
      appMallOptions:[],
      payModeOptions:[
        { dictLabel: "微信支付", dictValue: "wx" },
        { dictLabel: "汇付", dictValue: "hf" }
      ],
      refund:{
        title:"退款",
        open:false,
      },
      refundForm:{
        refundMoney:0
      },
      refundRules:{

      },
      payNotify:{
        title:"同步订单状态",
        open:false,
      },
      payNotifyForm:{
        tradeNo:null,
      },
      payNotifyRules:{
        tradeNo: [
          { required: true, message: "外部单号不能为空", trigger: "blur" }
        ],
      },
      companys:[],
      statusOptions:[],
      createTimeRange:[],
      showLoading:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      dateRange:[],
      refundDateRange:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付明细表格数据
      storePaymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        businessType:"1",
        pageNum: 1,
        pageSize: 10,
        payCode: null,
        payTypeCode: null,
        payMoney: null,
        payTime: null,
        tradeNo: null,
        userId: null,
        openId: null,
        businessOrderId: null,
        status: null,
        companyId: null,
        companyUserId: null,
        deptId: null,
        bankTransactionId:null,
        companyUserNickName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("store_payment_status").then((response) => {
      this.statusOptions = response.data;
    });

    getCompanyList().then(response => {
        this.companys = response.data;
      });
    this.getList();
    // 获取小程序选项列表
    this.getAppMallOptions();
  },
  methods: {
    // 获取小程序选项列表
    getAppMallOptions() {
      getAppMallOptions({pageNum:1,pageSize:100,isMall:1}).then(response => {
        this.appMallOptions = response.rows;
      })
    },
    submitPayNotifyForm(){
      this.$refs["payNotifyForm"].validate(valid => {
        if (valid) {
          setPayNotify(this.payNotifyForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess("操作成功");
              this.payNotify.open = false;
              this.getList();
            }
          });
        }
      });
    },
    handlePayNotify(){
      this.payNotify.open = true;
      this.payNotifyForm.tradeNo = null;
    },
    handleClick(tab, event) {
      this.queryParams.businessType=tab.name
      this.getList();
    },
    handleRefund(row){
      this.refundForm.paymentId=row.paymentId;
      this.refundForm.payCode=row.payCode;
      this.refundForm.payTypeCode=row.payTypeCode;
      this.refundForm.payMoney=row.payMoney;
      this.refundForm.refundMoney=row.payMoney;
      this.refund.open=true;
    },
    cancelRefund(){
      this.refund.open = false;
    },
    submitRefundForm() {
      this.$refs["refundForm"].validate(valid => {
        if (valid) {
            this.showLoading = this.$loading({
              lock: true,
              text: '处理中...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            refundStorePayment(this.refundForm).then(response => {
              this.showLoading.close()
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.refund.open = false;
                this.getList();
              }
              else{
                this.msgError(response.msg);
              }
            });
        }
      });
    },
    /** 查询支付明细列表 */
    getList() {
      this.loading = true;
      if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTimeRange=null;
      }
      listStorePayment(this.addDateRange(this.queryParams, this.dateRange),this.refundDateRange).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,
        remark: null,
        companyId: null,
        companyUserId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange=[];
      this.refundDateRange=[];
      this.createTimeRange=[];
      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>
