<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开始时间" prop="startTime">
         <el-date-picker v-model="dateRange" size="small" style="width: 205.4px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="logsType">
        <el-select v-model="queryParams.logsType" placeholder="请选择类型"  @change="changeLogsType" clearable size="small">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>

       <el-form-item label="商城订单号" label-width="100px" prop="orderCode" v-if="queryParams.logsType==4 || queryParams.logsType==5">
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入商城订单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>

      <el-form-item label="收件人手机号" label-width="120px" prop="userPhone" v-if="queryParams.logsType==4 || queryParams.logsType==5">  
          <el-input
            v-model="queryParams.userPhone"
            placeholder="请输入收件人手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      
      <el-form-item label="交易单号" prop="tradeCode" v-if="queryParams.logsType==4 || queryParams.logsType==5 || queryParams.logsType==8 || queryParams.logsType==9">
          <el-input
            v-model="queryParams.tradeCode"
            placeholder="请输入交易单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
      </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">
        合计金额：<span style="font-size:20px;color:red;">{{totalMoney.toFixed(2)}}</span>元
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport(0)"
          v-hasPermi="['company:companyMoneyLogs:export']"
        >导出全部</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport(1)"
          v-hasPermi="['company:companyMoneyLogs:export1']"
        >导出商城订单明细</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport(2)"
          v-hasPermi="['company:companyMoneyLogs:export2']"
        >导出收款订单明细</el-button>
      </el-col>
      
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="500" border v-loading="loading" :data="companyMoneyLogsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="logsId" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="当前余额" align="center" prop="balance" />

      <el-table-column label="商城订单号" align="center" prop="orderCode"  v-if="queryParams.logsType==4 || queryParams.logsType==5"  />
      <el-table-column label="交易单号" align="center" prop="bankTransactionId"  v-if="queryParams.logsType==4 || queryParams.logsType==5 || queryParams.logsType==8 || queryParams.logsType==9 " />
      <el-table-column label="支付类型" align="center" prop="payTypeCode"  v-if="queryParams.logsType==4 || queryParams.logsType==5 || queryParams.logsType==8 || queryParams.logsType==9 " />
    

      <el-table-column label="说明" align="center" prop="remark" />
      <el-table-column label="类型" align="center" prop="logsType" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in typeOptions"   v-if="scope.row.logsType==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.logsType==4||scope.row.logsType==5"
            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-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" 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="show.title" v-if="show.open" :visible.sync="show.open" width="1000px" append-to-body>
        <product-order  ref="order" />
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyTotalMoneys,listCompanyMoneyLogs, exportCompanyMoneyLogs } from "@/api/company/companyMoneyLogs";
import productOrder from "../../store/components/productOrder";

export default {
  name: "CompanyMoneyLogs",
  components:{
    productOrder
  },
  data() {
    return {
      show:{
        open:false,
        title:"订单详情"
      },
      totalMoney:0,
      typeOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业账户记录表格数据
      companyMoneyLogsList: [],
      // 弹出层标题
      title: "",
      dateRange:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        money: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("company_money_logs_type").then((response) => {
        this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    handleDetails(row){
      this.show.open=true;
      var that=this;
      console.log(row)
      const orderId = row.businessId ;
      setTimeout(() => {
        
        that.$refs.order.getOrder(orderId);
      }, 500);
    },
    /** 查询企业账户记录列表 */
    getList() {
      this.loading = true;
      listCompanyMoneyLogs(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.companyMoneyLogsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getCompanyTotalMoneys();
      });
    },
    getCompanyTotalMoneys(){
      getCompanyTotalMoneys(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.totalMoney = response.totalMoneys;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logsId: null,
        companyId: null,
        money: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业账户记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const logsId = row.logsId || this.ids
      getCompanyMoneyLogs(logsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业账户记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.logsId != null) {
            updateCompanyMoneyLogs(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCompanyMoneyLogs(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logsIds = row.logsId || this.ids;
      this.$confirm('是否确认删除企业账户记录编号为"' + logsIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompanyMoneyLogs(logsIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport(type) {
      const queryParams = this.queryParams;
      queryParams.type=type;
      this.$confirm('是否确认导出所有企业账户记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompanyMoneyLogs(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    changeLogsType(){
      this.getList();
    }
  }
};
</script>
