<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <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="businessCode">
        <el-input
          v-model="queryParams.businessCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方单号" prop="prescribeCode">
        <el-input
          v-model="queryParams.prescribeCode"
          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="bankSerialNo">
        <el-input
          v-model="queryParams.bankSerialNo"
          placeholder="请输入银行单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

       <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入会员手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务商类型" prop="payMode">
        <el-select v-model="queryParams.payMode" placeholder="请选择业务类型" clearable size="small">
          <el-option
            v-for="dict in payModeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择业务类型" clearable size="small">
          <el-option
            v-for="dict in busineOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="coursePlaySourceConfigId">
         <el-select v-model="queryParams.coursePlaySourceConfigId" placeholder="请选择所属小程序" clearable size="small">
           <el-option
             v-for="dict in appMallOptions"
             :key="dict.id"
             :label="dict.name + '(' + dict.appid + ')'"
             :value="dict.id"
           />
         </el-select>
       </el-form-item>
       <el-form-item label="创建时间" prop="createTime">
          <el-date-picker v-model="createTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="change"></el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
          <el-date-picker v-model="payTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="change"></el-date-picker>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker v-model="refundTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="change"></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">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:storePayment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs type="card" v-model="actName" @tab-click="handleClickX">
      <el-tab-pane label="全部订单" name="10"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in statusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table height="600" v-loading="loading" border :data="storePaymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="处方单号" align="center" prop="prescribeCode" width="170px"/>
      <el-table-column label="支付订单号" align="center" prop="payCode" width="170px"/>
      <el-table-column label="支付类型" align="center" prop="payTypeCode" />
      <el-table-column label="支付服务商" align="center" prop="payMode" width="100">
        <template slot-scope="scope">
          <dict-tag :options="payModeOptions" :value="scope.row.payMode"/>
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="payMoney" />
      <el-table-column label="外部订单号" align="center" prop="tradeNo" width="180"/>
      <el-table-column label="订单编号" align="center" prop="businessCode" width="180"/>
      <el-table-column label="业务类型 " align="center" prop="businessType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="busineOptions" :value="scope.row.businessType"/>
        </template>
      </el-table-column>

      <el-table-column label="小程序名称" align="center" prop="miniProgramName" width="120px" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="员工" align="center" prop="companyUserNickName" />
      <el-table-column label="备注" align="center" prop="remark" width="120px"  />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="150" />
      <el-table-column label="交易单号" align="center" prop="bankTransactionId" width="170px" />
      <el-table-column label="银行流水号" align="center" prop="bankSerialNo" width="150px"/>
      <el-table-column label="退款金额" align="center" prop="refundMoney" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180" />
      <el-table-column label="分账状态" align="center" prop="shareStatus">
        <template slot-scope="scope">
          <dict-tag :options="shareOptions" :value="scope.row.shareStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="分账金额" align="center" prop="shareMoney" />
     <el-table-column label="分账单号" align="center" prop="shareCode" width="150px"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
           <el-button
                      size="mini"
                      type="text"
                      @click="handledetails(scope.row)"
                    >详情
          </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-drawer
          :with-header="false"
           size="75%"
            :title="show.title" :visible.sync="show.open">
        <storePayDetails  ref="Details" />
      </el-drawer>
  </div>
</template>

<script>
import { listStorePayment, getStorePayment, delStorePayment, addStorePayment, updateStorePayment, exportStorePayment } from "@/api/his/storePayment";
import { getTask } from "@/api/common";
import storePayDetails from '../../components/his/storePayDetails.vue';
import { getCompanyList } from "@/api/company/company";
import {list as getAppMallOptions} from '@/api/course/coursePlaySourceConfig';
export default {
  name: "StorePayment",
  components: { storePayDetails },
  data() {
    return {
      show:{
              open:false,
            },
       createTime:null,
       refundTime:null,
       payTime:null,
       companys:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付明细表格数据
      storePaymentList: [],
      payModeOptions:[],
      shareOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态 0未支付 1已支付 -1 已退款字典
      statusOptions: [],
      busineOptitons: [],
       actName:"10",
      appMallOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payCode: null,
        payTypeCode: null,
        payMoney: null,
        payTime: null,
        tradeNo: null,
        userId: null,
        openId: null,
        sTime:null,
        eTime:null,
        businessType: null,
        businessId: null,
        status: null,
        bankTransactionId: null,
        bankSerialNo: null,
        refundMoney: null,
        refundTime: null,
        storeId: null,
        businessCode:null,
        phone:null,
        coursePlaySourceConfigId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    getCompanyList().then(response => {
        this.companys = response.data;
      });
    this.getList();
    this.getDicts("sys_store_payment_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_store_payment_business_type").then(response => {
      this.busineOptions = response.data;
    });
    this.getDicts("sys_pay_mode").then(response => {
      this.payModeOptions = response.data;
    });
    this.getDicts("sys_store_payment_share_status").then(response => {
      this.shareOptions = response.data;
    });

    // 获取小程序选项列表
    this.getAppMallOptions();
  },
  methods: {
    handleClickX(tab, event) {
     if(tab.name=="10"){
       this.queryParams.status=null;
     }else{
       this.queryParams.status=tab.name;
     }
      this.handleQuery();
    },
    /** 查询支付明细列表 */
    getList() {
      this.loading = true;
      listStorePayment(this.queryParams).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,
        businessId: null,
        status: 0,
        remark: null,
        bankTransactionId: null,
        bankSerialNo: null,
        refundMoney: null,
        refundTime: null,
        storeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      this.queryParams.refundBeginTime=null;
      this.queryParams.refundEndTime=null;
      this.queryParams.payBeginTime=null;
      this.queryParams.payEndTime=null;
      this.refundTime=null
      this.payTime=null
      this.handleQuery();
    },
    change(){
          if(this.createTime!=null){
            this.queryParams.sTime=this.createTime[0];
            this.queryParams.eTime=this.createTime[1];
          }else{
            this.queryParams.sTime=null;
            this.queryParams.eTime=null;
          }
          if(this.refundTime!=null){
            this.queryParams.refundBeginTime=this.refundTime[0];
            this.queryParams.refundEndTime=this.refundTime[1];
          }else{
            this.queryParams.refundBeginTime=null;
            this.queryParams.refundEndTime=null;
          }
          if(this.payTime!=null){
            this.queryParams.payBeginTime=this.payTime[0];
            this.queryParams.payEndTime=this.payTime[1];
          }else{
            this.queryParams.payBeginTime=null;
            this.queryParams.payEndTime=null;
          }

        },
     handledetails(row){
             this.show.open=true;
             setTimeout(() => {
                  this.$refs.Details.getDetails(row.paymentId);
             }, 1);
         },

    // 多选框选中数据
    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 => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorePayment(this.form).then(response => {
              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(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
		var that=this;
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有支付明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStorePayment(queryParams);
        }).then(response => {
          if(response.code==200){
          that.msgSuccess(response.msg);
          that.taskId=response.data;
          that.time=setInterval(function(){
            //查订单
            getTask(that.taskId).then(res => {
              if(res.data.status==1){
                that.exportLoading = false;
                clearTimeout(that.time)
                that.time=null;
                that.download(res.data.fileUrl);
              }
            });
          },10000);
        }
        }).catch(() => {});
    },
    // 获取小程序选项列表
    getAppMallOptions() {
      getAppMallOptions({pageNum:1,pageSize:100,isMall:1}).then(response => {
        this.appMallOptions = response.rows;
      })
    },
  }
};
</script>
