<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">PICC审核</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">佣金管理</a>
        <!-- 佣金管理 PICC -->
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!--
      要resetFields起作用，必须配置:model和prop
    -->
    <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
      <el-row>
        <el-col>
          <el-form-item label="投保险种" prop="definitionId">
            <el-select
              v-model="queryModel.definitionId"
              filterable
              placeholder="请选择"
              style="width:200px"
            >
              <el-option
                v-for="result in queryResult"
                :key="result.id"
                :label="result.name"
                :value="result.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="企业名称" prop="companyName">
            <el-input type="text" v-model="queryModel.companyName"></el-input>
          </el-form-item>
          <el-form-item label="推广员" prop="promoter">
            <el-input type="text" v-model="queryModel.promoter"></el-input>
          </el-form-item>
          <el-form-item label="保险代理人" prop="agentId">
            <el-select
              v-model="queryModel.agentId"
              filterable
              placeholder="请选择"
              style="width:200px"
            >
              <el-option
                v-for="agent in agentData"
                :key="agent.id "
                :label="agent.name"
                :value="agent.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="创建时间" prop="createTimes">
            <el-date-picker
              v-model="queryModel.createTimes"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="出单时间" prop="fisinhTimes">
            <el-date-picker
              v-model="queryModel.fisinhTimes"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="ios-search" @click="changePage(1)" :loading="loading">查询</el-button>&nbsp;
            <el-button type="info" style="margin-left: 8px" @click="handleReset('queryForm')">重置</el-button>&nbsp;
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider></el-divider>
    <el-tabs v-model="activeName" type="card" activeName @tab-click="handleClick">
      <el-tab-pane name="all">
        <span slot="label">全部</span>
      </el-tab-pane>
      <el-tab-pane name="0">
        <span slot="label">未录入佣金信息</span>
      </el-tab-pane>
      <el-tab-pane name="1">
        <span slot="label">已录入佣金信息</span>
      </el-tab-pane>
      <el-table
        :data="tableData"
        style="min-height:400px;"
        v-loading="loading"
        stripe
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" label="序号" :index="1"></el-table-column>
        <el-table-column prop="policyNo" label="投保单号" width="200"></el-table-column>
        <el-table-column prop="definition.name" label="投保险种" width="150"></el-table-column>
        <el-table-column prop="company.name" label="企业名称" width="150"></el-table-column>
        <el-table-column prop="insuranceFee" label="投保金额" width="100"></el-table-column>
        <el-table-column label="投保期间" width="200">
          <template slot-scope="{row}">{{row.startTime}}至{{row.endTime}}</template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column prop="finishTime" label="出单时间" width="180"></el-table-column>
        <el-table-column prop="commissionTime" label="佣金打款时间" width="180"></el-table-column>
        <el-table-column prop="agent.name" label="保险代理人" width="100"></el-table-column>
        <el-table-column prop="commission" label="佣金收益" width="100"></el-table-column>
        <el-table-column label="操作" width="250px" fixed="right">
          <template slot-scope="{row}">
            <el-row>
              <el-col :span="12">
                <el-button size="mini" type="info" @click="downloadFile(row)">下载电子保单</el-button>
              </el-col>
              <el-col :span="12">
                <span v-if="row.commissionTime == null || row.commissionTime == ''">
                  <el-button size="mini" type="success" @click="commissionView(row)">录入佣金信息</el-button>
                </span>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-tabs>
    <el-pagination
      :current-page.sync="pageIndex"
      :total="totalElements"
      :page-sizes="pageSizeList"
      @current-change="changePage"
      @size-change="pageSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <insuranceJobs-detail
      v-if="showModal"
      :id="businessKey"
      :policyId="policyId"
      @close="onDetailModalClose"
    ></insuranceJobs-detail>
    <commission-detail
      v-if="showCommissionModal"
      :businessKey="businessKey"
      @close="onDetailModalClose"
    ></commission-detail>
  </div>
</template>
<script>
import Constant from "@/constant";
import insuranceJobsDetail from "./insurancePolicy-firstDetails";
import commissionDetail from "./commission-detail";
import commissionApi from "@/api/business/commission";

import insuranceAgentApi from "@/api/base/insuranceAgent";
import insuranceDefinitionApi from "@/api/base/insuranceDefinition";

import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style

export default {
  components: {
    "insuranceJobs-detail": insuranceJobsDetail,
    "commission-detail": commissionDetail
  },
  data() {
    var self = this;

    return {
      queryModel: {
        definitionId: "",
        companyName: "",
        promoter: "",
        agentId: "",
        createTimes: "",
        fisinhTimes: "",
        startTimes: ""
      },
      loading: false,
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      totalPages: 0,
      totalElements: 0,
      field: "",
      direction: "",
      pageSizeList: [10, 20, 30],
      multipleSelection: [],
      showModal: false,
      showCommissionModal: false,
      modalTitle: "",
      businessKey: "",
      queryResult: [],
      agentData: [],
      tabStatus: "",
      activeName: "all"
    };
  },
  methods: {
    handleClick(tab, event) {
      if (tab.name == "all") {
        this.tabStatus = "";
      } else {
        this.tabStatus = tab.name;
      }
      this.changePage(1);
    },
    changePage(pageIndex) {
      var self = this;

      self.loading = true;

      self.pageIndex = pageIndex;
      var formData = new FormData();

      formData.append("pageIndex", self.pageIndex);
      formData.append("pageSize", self.pageSize);

      formData.append("isCommission", self.tabStatus);

      formData.append("definitionId", self.queryModel.definitionId);
      formData.append("companyName", self.queryModel.companyName);
      formData.append("promoter", self.queryModel.promoter);
      formData.append("agentId", self.queryModel.agentId);
      var createTimes = self.queryModel.createTimes + "";
      if (createTimes != null && createTimes != "") {
        createTimes = createTimes.split(",");
        formData.append("startCreat", createTimes[0]);
        formData.append("endCreat", createTimes[1]);
      }

      var fisinhTimes = self.queryModel.fisinhTimes + "";
      if (fisinhTimes != null && fisinhTimes != "") {
        fisinhTimes = fisinhTimes.split(",");
        formData.append("startFinish", fisinhTimes[0]);
        formData.append("endFinish", fisinhTimes[1]);
      }

      if (this.field != null) {
        formData.append("field", this.field);
      }

      if (this.direction != null) {
        formData.append("direction", this.direction);
      }

      commissionApi
        .pageList(formData)
        .then(function(response) {
          self.loading = false;

          var jsonData = response.data.data;

          self.tableData = jsonData.data;
          self.totalPages = jsonData.totalPage;
          self.totalElements = jsonData.recordsTotal;
        })
        .catch(error => {
          self.loading = false;
          // self.$message.error(error + "");
        });
    },
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;

      this.$nextTick(() => {
        this.changePage(this.pageIndex);
      });
    },
    sortChange(data) {
      this.field = data.column.field;
      this.direction = data.order;

      this.changePage(this.pageIndex);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    onDetailModalClose(refreshed) {
      //保存成功后回调
      this.showModal = false;
      this.showCommissionModal = false;

      if (refreshed) {
        this.changePage(this.pageIndex);
      }
    },
    loadSelectList() {
      var formData = new FormData();

      formData.append("type", "1");

      insuranceAgentApi.list(formData).then(response => {
        var jsonData = response.data;
        this.agentData = jsonData.data;
      });

      insuranceDefinitionApi.list().then(response => {
        var jsonData = response.data;
        this.queryResult = jsonData.data;
      });
    },
    commissionView(record) {
      //查看详细信息
      this.businessKey = record.id;
      this.showCommissionModal = true;
    },
    downloadFile(record) {
      //下载单子保单
      var policyFile = record.insurancePolicyFile;
      window.open(policyFile);
    }
  },
  mounted: function() {
    this.changePage(1);
    this.loadSelectList();
  }
};
</script>
<style lang="scss" scoped>
.el-breadcrumb {
  margin: 10px;
  line-height: 20px;
}

.el-divider {
  margin: 5px 0;
}

.demo-form-inline {
  margin-left: 10px;
  text-align: left;
}

.button-group {
  margin-left: 10px;
  text-align: left;
}
</style>