<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="租户" prop="tenantId">
        <tenant-select v-model="queryParams.tenantId" @change="handleQuery" />
      </el-form-item>

      <el-form-item label="公司名" prop="companyId">
          <el-select filterable  style="width: 220px" 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="logsType">
        <el-select style="width: 220px" v-model="queryParams.logsType" placeholder="请选择类型" @change="changeLogsType" clearable size="small">
              <el-option
                v-for="dict in typesOptions"
                :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==3 || queryParams.logsType==4 || queryParams.logsType==5 || queryParams.logsType==6" >
          <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==3 || queryParams.logsType==4 || queryParams.logsType==5 || queryParams.logsType==6">
          <el-input
            v-model="queryParams.userPhone"
            placeholder="请输入收件人手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item label="付款单号" prop="payCode" v-if="queryParams.logsType==3 || queryParams.logsType==4 ||queryParams.logsType==5 || queryParams.logsType==6 || queryParams.logsType==10 ">
        <el-input
          v-model="queryParams.payCode"
          placeholder="请输入付款单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTimeRange">
        <el-date-picker
          style="width:205.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>
        <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"
		   :loading="exportLoading"
          @click="handleExport(0)"
          v-hasPermi="['platform:companyMoneyLogs:export']"
        >导出全部</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
		   :loading="exportLoading"
          @click="handleExport(1, 0)"
          v-hasPermi="['platform:companyMoneyLogs:export1']"
        >导出商城订单明细</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
		   :loading="exportLoading"
          @click="handleExport(1, 1)"
          v-hasPermi="['platform:companyMoneyLogs:export1']"
        >导出直播订单明细</el-button>

        <!-- <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport(2)"
          v-hasPermi="['platform: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="companyName" />
      <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==3 || queryParams.logsType==4 ||queryParams.logsType==5 || queryParams.logsType==6"  />
      <el-table-column label="支付类型" align="center" prop="payTypeCode"  v-if="queryParams.logsType==3 || queryParams.logsType==4 ||queryParams.logsType==5 || queryParams.logsType==6 " />

      <el-table-column label="备注" align="center" prop="remark" />
      <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" prop="logsType" >
        <template slot-scope="scope">
              <el-tag prop="logsType" v-for="(item, index) in typesOptions"   v-if="scope.row.logsType==item.dictValue">{{item.dictLabel}}</el-tag>
        </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==3||scope.row.logsType==4||scope.row.logsType==5||scope.row.logsType==6 || scope.row.logsType==12 || scope.row.logsType==13|| scope.row.logsType==14"
            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-drawer
      :with-header="false"
      size="75%"
      :title="show.title" :visible.sync="show.open"
      >
      <storeOrderDetails  ref="order" />
    </el-drawer>
    <el-drawer
      :with-header="false"
      size="75%"
      :title="liveShow.title" :visible.sync="liveShow.open"
      >
      <liveOrderDetails  ref="liveOrder" />
    </el-drawer>
    <el-drawer :with-header="false" size="75%" :title="inquiryShow.title" :visible.sync="inquiryShow.open">
     <inquiryOrderDetails  ref="Details" />
   </el-drawer>
  </div>
</template>

<script>
import { listCompanyMoneyLogs, getCompanyMoneyLogs, delCompanyMoneyLogs, addCompanyMoneyLogs, updateCompanyMoneyLogs, exportCompanyMoneyLogs } from "@/api/company/companyMoneyLogs";
import { getCompanyList } from "@/api/company/company";
import { getTask } from "@/api/common";
import TenantSelect from '@/components/TenantSelect/index'
import storeOrderDetails  from '../../components/his/storeOrderDetails.vue';
import liveOrderDetails from '../../live/liveOrder/liveOrderDetails.vue';
import inquiryOrderDetails from '../../components/his/inquiryOrderDetails.vue';
export default {
  components: { TenantSelect, storeOrderDetails, liveOrderDetails, inquiryOrderDetails},
  name: "CompanyMoneyLogs",
  data() {
    return {
      taskId:null,
      time:null,
      exportLoading: false,
      show:{
        open:false,
        title:"订单详情"
      },
      liveShow:{
        open:false,
        title:"直播订单详情"
      },
      inquiryShow:{
        open:false,
        title:"订单详情"
      },
      typesOptions:[],
      companys:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业账户记录表格数据
      companyMoneyLogsList: [],
      createTimeRange:[],
      // 弹出层标题
      title: "",
      dateRange:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        orderCode:null,
        money: null,
        userPhone:null,
        tradeCode:null,
        beginTime:null,
        endTime:null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("sys_company_money_logs_type").then((response) => {
      this.typesOptions = response.data;
    });
    getCompanyList().then(response => {
      this.companys = response.data;

    });
    this.getList();
  },
  methods: {
    handleDetails(row){
      const orderId = row.businessId ;
      // 根据type字段区分商城订单和直播订单
      if(row.logsType==3||row.logsType==4||row.logsType==5||row.logsType==6 ||row.logsType==13||row.logsType==14){
        // type=0为商城订单，type=1为直播订单
        if(row.type == 1){
          // 直播订单
          this.liveShow.open=true;
          setTimeout(() => {
            this.$refs.liveOrder.getDetails(orderId);
          }, 500);
        } else {
          // 商城订单
          this.show.open=true;
          setTimeout(() => {
            this.$refs.order.getDetails(orderId);
          }, 500);
        }
      }
      if(row.logsType==12){
        this.inquiryShow.open = true;
        setTimeout(() => {
            this.$refs.Details.getDetails(orderId);
      }, 1);
      }

    },
    /** 查询企业账户记录列表 */
    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;
      }
      listCompanyMoneyLogs(this.$withTenant(this.queryParams)).then(response => {
        this.companyMoneyLogsList = response.rows;
        console.log(this.queryParams)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logsId: null,
        companyId: null,
        money: null,
        remark: null,
        createTime: null,
        beginTime:null,
        endTime:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange=[]
      this.dateRange=[];
      this.resetForm("queryForm");
      this.queryParams.beginTime=null;
      this.queryParams.endTime=null;
      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.$withTenant(this.form)).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCompanyMoneyLogs(this.$withTenant(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, orderType) {
		var that=this;
      if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTimeRange=null;
      }
      const queryParams=this.$withTenant(this.addDateRange(this.queryParams, this.dateRange))
      queryParams.type=type;
      if(orderType !== undefined){
        queryParams.orderType=orderType;
      }
      this.$confirm('是否确认导出账户记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
			    that.exportLoading = true;
          return exportCompanyMoneyLogs(queryParams);
        }).then(response => {
          console.log(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);
          }
          else{
            that.msgError(response.msg);
            that.exportLoading = false;
          }
        }).catch(function() {
            that.exportLoading = false;
        });
    },
    changeLogsType(){
      this.getList();
    }
  }
};
</script>
