<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">人才超市</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/businessInsuranceApplication">新增投保审批</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!--
      要resetFields起作用，必须配置:model和prop
    -->
    <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
      <el-form-item label="险种信息" prop="definitionId">
        <el-select
          v-model="queryModel.definitionId"
          filterable
          placeholder="请选择"
          style="width:200px"
          size="mini"
        >
          <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="policyNo">
        <el-input type="text" size="mini" v-model="queryModel.policyNo"></el-input>
      </el-form-item>
      <el-form-item label="投保企业" prop="companyName">
        <el-input type="text" size="mini" v-model="queryModel.companyName"></el-input>
      </el-form-item>
      <el-form-item label="投保人姓名" prop="insuredName">
        <el-input type="text" size="mini" v-model="queryModel.insuredName"></el-input>
      </el-form-item>
      <!-- <el-form-item label="投保期间" prop="companyId">
        <el-date-picker
          v-model="queryModel.startTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          icon="ios-search"
          @click="changePage(1)"
          :loading="loading"
        >查询</el-button>&nbsp;
        <el-button
          type="info"
          size="mini"
          style="margin-left: 8px"
          @click="handleReset('queryForm')"
        >重置</el-button>&nbsp;
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-tabs type="card" activeName @tab-click="handleClick">
      <el-tab-pane name="all">
        <span slot="label">
          <el-badge v-if="statusListNum['all']>0" :value="statusListNum['all']" class="item tab-badge">全部</el-badge>
          <span v-else>全部</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="20">
        <span slot="label">
          <el-badge v-if="statusListNum['20']>0" :value="statusListNum['20']" class="item tab-badge">人才超市核准</el-badge>
          <span v-else>人才超市核准全部</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="15">
        <span slot="label">
          <el-badge v-if="statusListNum['15']>0" :value="statusListNum['15']" class="item tab-badge">材料不齐</el-badge>
          <span v-else>材料不齐</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="30">
        <span slot="label">
          <el-badge v-if="statusListNum['30']>0" :value="statusListNum['30']" class="item tab-badge">中国人保审核</el-badge>
          <span v-else>中国人保审核</span>
        </span>
      </el-tab-pane>
      <el-tab-pane label="待缴费" name="40">
        <span slot="label">
          <el-badge v-if="statusListNum['40']>0" :value="statusListNum['40']" class="item tab-badge">待缴费</el-badge>
          <span v-else>待缴费</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="50">
        <span slot="label">
          <el-badge v-if="statusListNum['50']>0" :value="statusListNum['50']" class="item tab-badge">待制单</el-badge>
          <span v-else>待制单</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="60">
        <span slot="label">
          <el-badge v-if="statusListNum['60']>0" :value="statusListNum['60']" class="item tab-badge">待出单</el-badge>
          <span v-else>待出单</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="70">
        <span slot="label">
          <el-badge v-if="statusListNum['70']>0" :value="statusListNum['70']" class="item tab-badge">已出单</el-badge>
          <span v-else>已出单</span>
        </span>
      </el-tab-pane>
      <el-tab-pane name="80">
        <span slot="label">
          <el-badge v-if="statusListNum['80']>0" :value="statusListNum['80']" class="item tab-badge">已过期</el-badge>
          <span v-else>已过期</span>
        </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="selection" width="55"></el-table-column>
        <el-table-column prop="statusN" label="投保单状态" width="150"></el-table-column>
        <el-table-column prop="definitionName" label="保险种类" width="150"></el-table-column>
        <el-table-column prop="policyNo" label="投保单号" width="200"></el-table-column>
        <el-table-column prop="companyName" label="投保企业" width="180"></el-table-column>
        <!-- <el-table-column prop="insuredName" label="投保人姓名" width="180"></el-table-column> -->
        <!-- <el-table-column prop="insuredTel" label="联系电话" width="180"></el-table-column>-->
        <el-table-column prop="insuredNumber" label="投保人数" width="80"></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="insuranceFee" label="保险费合计" width="100">
          <template slot-scope="scope">
            <span class="insure-money">￥{{scope.row.insuranceFee}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160"></el-table-column>
        <el-table-column prop="finishTime" label="出单时间" width="160"></el-table-column>
        <el-table-column prop="accountBank" label="入账银行" width="160"></el-table-column>
        <el-table-column prop="issuingBank" label="出账银行" width="160"></el-table-column>
        <el-table-column prop="singlNumber" label="单号" width="100"></el-table-column>
        <el-table-column prop="transferTime" label="转账时间" width="160"></el-table-column>
        <el-table-column label="操作" width="250" fixed="right">
          <template slot-scope="{row}">
            <el-button size="mini" @click="openFirstDetails(row)">查看详情</el-button>&#12288;&#12288;
            <span v-if="row.status=='15'">
              <el-button size="mini" type="primary" @click="returnFirst(row)">返回初审</el-button>
            </span>
            <span v-if="row.status=='20'">
              <el-button size="mini" type="primary" @click="openFirstApproval(row)">处理</el-button>
            </span>
            <span v-if="row.status=='50'">
              <el-button size="mini" type="primary" @click="openTransfer(row)">处理</el-button>
            </span>
            <span v-if="row.status > '60' ">
              &#12288;&#12288;
              <el-button size="mini" type="success" @click="openPolicyFile(row)">查看投保单</el-button>
            </span>
            <!-- <el-button size="mini" @click="handleDelete(row)">退回理由</el-button>
            <el-button size="mini" @click="handleDelete(row)">投保历史信息</el-button>-->
          </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>
    <first-details v-if="showModal" :id="id" @close="onDetailModalClose"></first-details>
  </div>
</template>
<script>
import Constant from "@/constant";
import firstDetails from "./insuranceFirst-firstDetails";
import insuranceApplicationApi from "@/api/business/insuranceApplication";
import insuranceDefinitionApi from "@/api/base/insuranceDefinition";
import approvalApi from "@/api/business/approval";

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

export default {
  components: {
    "first-details": firstDetails
  },
  data() {
    var self = this;

    return {
      queryModel: {
        companyId: "",
        companyName: "",
        definitionId: "",
        insuredName: "",
        insuredTel: "",
        startTime: "",
        endTime: "",
        number: "",
        insuranceFee: "",
        status: "",
        userType: "",
        policyNo:""
      },
      loading: false,
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      totalPages: 0,
      totalElements: 0,
      field: "",
      direction: "",
      pageSizeList: [10, 20, 30],
      multipleSelection: [],
      showModal: false,
      id: "",
      tabStatus: "",
      queryResult: [],
      statusListNum:{}
    };
  },
  methods: {
    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("companyId", self.queryModel.companyId);
      formData.append("companyName", self.queryModel.companyName);
      formData.append("definitionId", self.queryModel.definitionId);
      formData.append("insuredName", self.queryModel.insuredName);
      formData.append("insuredTel", self.queryModel.insuredTel);
      formData.append("startTime", self.queryModel.startTime);
      formData.append("endTime", self.queryModel.endTime);
      formData.append("insuranceFee", self.queryModel.insuranceFee);
      formData.append("policyNo", self.queryModel.policyNo);

      formData.append("status", this.tabStatus);
      formData.append("userType", "0");

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

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

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

        var jsonData = response.data;
        
        if(jsonData.result>0){
          var tableData = jsonData.data;

          self.tableData = tableData.data;
          self.totalPages = tableData.totalPages;
          self.totalElements = tableData.recordsTotal;
        }
      })
      .catch(error => {
        self.loading = false;
        // self.$message.error(error + "");
      });

      insuranceApplicationApi.tabCount(formData)
      .then(response=>{
          var jsonData = response.data;

          if(jsonData.result){
            this.statusListNum = jsonData.data;
          }
      });
    },
    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();
    },
    openFirstDetails(record) {
      this.id = record.id;
      this.showModal = true;
    },
    openFirstApproval(record) {
      this.$router.push({
        path: "/business/insuranceFirst/firstApproval",
        query: { id: record.id }
      });
    },
    openTransfer(record) {
      this.$router.push({
        path: "/business/insuranceFirst/transfer",
        query: { id: record.id }
      });
    },
    handleDelete(record) {
      var self = this;

      self
        .$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          insuranceApplicationApi.remove(record.id).then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              // var index = self.tableData.indexOf(record);
              // self.tableData.splice(index, 1);
              self.changePage(self.pageIndex);

              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
    },
    handleBatchDelete() {
      var self = this;

      var idList = this.multipleSelection.map(record => {
        return record.id;
      });

      this.$confirm("是否确认删除选中项？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        insuranceApplicationApi.batchRemove(idList).then(function(response) {
          var jsonData = response.data;

          if (jsonData.result) {
            self.changePage(self.pageIndex);

            self.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        });
      });
    },
    onDetailModalClose(refreshed) {
      //保存成功后回调
      this.showModal = false;

      if (refreshed) {
        this.changePage(this.pageIndex);
      }
    },
    handleClick(tab, event) {
      if(tab.name=='all'){
        this.tabStatus = '';
      }
      else{        
        this.tabStatus = tab.name;
      }
      
      this.changePage(1);
      if (this.tabStatus == "20") {
        //待初审20
      } else if (this.tabStatus == "50") {
        //带制单50
      }
    },
    returnFirst(record) {
      //返回初审20
      var status = "20";
      var formData = new FormData();
      formData.append("applicationId", record.id);
      formData.append("policyId", "");
      formData.append("status", status);

      approvalApi.rollbackOrder(formData).then((response)=>{
        var jsonData = response.data;

        if (jsonData.result) {
          this.changePage(this.pageIndex);

          this.$message({
            type: "success",
            message: "返回成功!"
          });
        }
        else{
          this.$message.error(jsonData.message);
        }
      });
    },
    openPolicyFile(record) {
      var formData = new FormData();
      formData.append("applicationId", record.id);
      insuranceApplicationApi
        .insureDetail(formData)
        .then(function(response) {
          var jsonData = response.data;
          self.loading = false;
          if (jsonData.result) {
            var insureData = jsonData.data;
            var policyFile = insureData.insurancePolicy.insurancePolicyFile;
            window.open(policyFile);
          } else {
            self.$message.error(jsonData.message + "");
          }
        });
    }
  },
  mounted: function() {
    this.changePage(1);
    insuranceDefinitionApi.list().then(response => {
      var jsonData = response.data;
      this.queryResult = jsonData.data;
    });
  }
};
</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;
}

.tab-badge {
  display: inline;
  position: relative !important;
}
</style>