<template>
    <div>
        <crumbs title="产品申请列表" style="min-width: 1085px">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="申请时间：">
                    <el-date-picker v-model="filter.time" size="mini" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:01', '23:59:59']"
                                    type="daterange"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="订单号：">
                    <el-input size="mini" v-model="filter.orderNum" placeholder="订单号" clearable></el-input>
                </el-form-item>
                <el-form-item label="机构名称：">
                    <el-input size="mini" v-model="filter.orgName" placeholder="机构名称" clearable></el-input>
                    </el-select>
                </el-form-item>
                <el-form-item label="申请状态：">
                    <el-select size="mini" v-model="filter.status" placeholder="申请状态" clearable>
                        <el-option v-for="item in status" :label="item.name" :value="item.key"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini" @click="filterDatas">确认</el-button>
                    <el-button size="mini" type="primary" @click="exportProductLines">导出</el-button>
                </el-form-item>
            </el-form>
        </crumbs>
        <div class="contents">
            <el-table :data="list"
                      border
                      @expand-change="showPickup"
                      :row-class-name="proLineRow"
                      width="100%"
                      class="pro-line-table">
                <el-table-column
                        type="index"
                        :index="indexMethod"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="time"
                        label="申请时间"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="num"
                        label="订单号"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        prop="orgName"
                        label="申请机构" width="100">
                </el-table-column>
                <el-table-column
                        prop="linkman"
                        label="申请人" width="100">
                </el-table-column>
                <el-table-column
                        prop="username"
                        label="手机号">
                </el-table-column>
                <el-table-column
                        prop="email"
                        label="邮箱"
                        :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column
                        label="操作" width="120">
                    <template slot-scope="scope">
                        <span class="status-span" v-if="scope.row.auditStatus==0">不通过</span>
                        <span class="status-span" v-if="scope.row.auditStatus==1">通过</span>
                        <el-button v-if="scope.row.auditStatus==2" type="text" size="small">{{scope.row.isExpand?"收起":"待审核"}}</el-button>
                        <span class="status-span" v-if="scope.row.auditStatus==3">部分通过</span>
                        <el-button v-if="scope.row.auditStatus==4" type="text" size="small">{{scope.row.isExpand?"收起":"部分审核"}}</el-button>
                        <el-button type="text" size="small" :disabled="scope.row.auditStatus==2||scope.row.auditStatus==4">{{scope.row.isExpand&&(scope.row.auditStatus!=2&&scope.row.auditStatus!=4)?"收起":"查看"}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column type="expand">
                    <!---------详情展开内容------------>
                    <template slot-scope="scope">
                        <el-table :data="scope.row.orderDetial">
                            <el-table-column label="编号"
                                             type="index"
                                             width="60"
                                             :index="indexMethod"></el-table-column>
                            <el-table-column label="申请时间"
                                             prop="orderTime"
                                             :show-overflow-tooltip="true"></el-table-column>
                            <el-table-column label="开通功能" prop="productName"></el-table-column>
                            <el-table-column label="付款状态" prop="payStatusName"></el-table-column>
                            <el-table-column label="审核状态" width="130">
                                <template slot-scope="detail">
                                    <el-popover
                                            v-if="detail.row.auditStatus==2"
                                            placement="top-start"
                                            width="260"
                                            v-model="detail.row.visible"
                                            >
                                        <el-form label-position="top" :inline="true" >
                                            <span class="closeDate"  @click="detail.row.visible = false">×</span>
                                            <el-form-item label="服务开始时间">
                                                <el-date-picker size="mini" type="date"
                                                                value-format="yyyy-MM-dd"
                                                                placeholder="选择开始时间"
                                                                :picker-options="startPicker"
                                                                v-model="detailParam.startTime"
                                                                style="width: 100%;"></el-date-picker>
                                            </el-form-item>
                                            <el-form-item label="服务结束时间">
                                                <el-date-picker size="mini" type="date"
                                                                value-format="yyyy-MM-dd"
                                                                :picker-options="endPicker"
                                                                placeholder="选择结束时间"
                                                                v-model="detailParam.endTime"
                                                                style="width: 100%;"></el-date-picker>
                                            </el-form-item>
                                            <div style="text-align: center">
                                                <el-button type="primary" size="mini" @click="handleAuthStatus(detail.row,1,scope.row)">确定</el-button>
                                            </div>
                                        </el-form>
                                        <el-button slot="reference" size="mini"> 通过</el-button>
                                    </el-popover>
                                    <span v-if="detail.row.auditStatus==2" style="margin:0 3px;">|</span>
                                    <el-popover
                                            v-if="detail.row.auditStatus==2"
                                            placement="bottom-start"
                                            width="380"
                                            v-model="detail.row.visible2"
                                            >
                                        <el-form>
                                          <span class="closeDate"  @click="detail.row.visible2 = false">×</span>
                                            <h4>原因说明</h4>
                                            <el-form-item>
                                                <el-radio-group v-model="detailParam.rejectType" @change="detailParam.rejectReason = ''">
                                                    <p v-for="(it,index) in reasons" style="margin: 10px 0;">
                                                        <el-radio :label="it.key">{{it.name}}</el-radio>
                                                    </p>
                                                </el-radio-group>
                                            </el-form-item>
                                            <el-form-item>
                                                <el-input v-model="detailParam.rejectReason" @focus="detailParam.rejectType=-1" type="textarea" placeholder="其他原因说明"></el-input>
                                            </el-form-item>
                                            <el-form-item>
                                                <el-button type="primary" @click="handleAuthStatus(detail.row,0,scope.row)">完成</el-button>
                                            </el-form-item>
                                        </el-form>
                                        <el-button slot="reference" size="mini">不通过</el-button>
                                    </el-popover>
                                    <span class="status-span" v-if="detail.row.auditStatus==1">通过</span>
                                    <span class="status-span" v-if="detail.row.auditStatus==0">不通过</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="产品服务有效期" width="250">
                                <template slot-scope="scope">
                                    {{scope.row.startTime&&scope.row.startTime.substr(0,10)}}～{{scope.row.endTime&&scope.row.endTime.substr(0,10)}}
                                </template>
                            </el-table-column>
                            <el-table-column label="不通过说明" width="180">
                                <template slot-scope="scope">
                                    {{scope.row.rejectReason||scope.row.rejectTypeName}}
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                    <!---------详情展开内容end------------>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination v-if="total>pageSize"
                       :current-page.sync="currentPage"
                       @current-change="currentChange"
                       background
                       :page-size="pageSize"
                       layout="total,prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
    </div>
</template>

<script>
  import api from '@api/admin.js';
  import utils from '@api/utils.js';

  export default {
    name: 'product-lines',
    data: function () {
      return {
        visible: false,
        visible2: false,
        list: [],
        status: [],
        cacheData: {},
        endPicker:{
          disabledDate:(current)=>{   //小于开始时间或今天之前不可选
            const today = new Date(new Date(new Date().toLocaleDateString()).getTime());
            return current<new Date(this.detailParam.startTime)||current<today;
          }
        },
        startPicker:{
          disabledDate:(current)=>{         //大于结束时间或今天之前不能选
            const today = new Date(new Date(new Date().toLocaleDateString()).getTime());
            return current > new Date(new Date(this.detailParam.endTime).getTime()-1000*60*60*24)||current<today;
          }
        },
        reasons:[],
        detailParam:{
          endTime:'',
          rejectReason:'',
          rejectType:-1,
          auditStatus:'',
          startTime:''
        },
        currentPage: 1,
        pageSize: 10,
        total: 0,
        filter: {
          orderNum: '',
          orgName: '',
          status: '',
          time:[]
        }
      }
    },
    created() {
      this.getDataList();
      const enums = JSON.parse(localStorage.getItem('productEnumsData'));
      this.reasons = enums&&enums.notPassEnum;          //审核不通过原因枚举
      this.status = enums&&enums.auditStatusEnum;       //申请状态枚举
    },
    methods: {
      showPickup(row){
        row.isExpand = !row.isExpand;
      },
      proLineRow(row){
        const status = row.row.auditStatus;
        if(status==2||status==4){         //查看
          return 'pro-line-auth';
        }else{                  //待审核
          return 'pro-line-view';
        }
      },
      filterDatas(){
        this.currentPage = 1;
        this.getDataList();
      },
      getDataList() {
        const param = this.getFilterItems();
        api.getProductLines(param).then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            this.list = data.records.map((item) =>{
              let orderDetial = item.orderDetial.map((detail) => {
                return {...detail, visible: false, visible2: false}
              })
              return {...item, orderDetial}
            });
            this.cacheData[param.current] = data.records;
            this.total = data.total;
          }
        }).catch((error) => {
          console.log(error);
        });
      },
      clearStatus(){
        this.detailParam.rejectReason = '';
        this.detailParam.rejectType = -1;
        this.detailParam.endTime = '';
        this.detailParam.startTime = '';
      },
      handleAuthStatus(row,status,parent){
        this.detailParam.id = row.id;
        this.detailParam.auditStatus = status;
        if(status==1){      //通过参数
          if(!this.detailParam.endTime||!this.detailParam.startTime){
            this.$message({
              showClose: true,
              message:'请填写服务时间',
              type:'warning'
            });
            return;
          }
          this.detailParam.rejectReason = undefined;
          this.detailParam.rejectType = undefined;
        }else{              //不通过参数
          if(this.detailParam.rejectType===-1&&this.detailParam.rejectReason===''){
            this.$message({
              showClose: true,
              message:'请选择/填写不通过原因',
              type:'warning'
            });
            return;
          }
          if(this.detailParam.rejectReason.length>100){
            this.$message({
              showClose: true,
              message:'不通过原因最多可输入100字',
              type:'warning'
            });
            return;
          }
          this.detailParam.startTime = undefined;
          this.detailParam.endTime = undefined;
        }
        api.auditProductStatus(this.detailParam).then((res)=>{
          if(res.data.code=='0'){
            row.auditStatus = this.detailParam.auditStatus;   //通过后修改显示状态
            //判断详情中审核状态-修改父级审核状态
            const details = parent.orderDetial;
            const unAuth = details.filter((it)=>{
              return it.auditStatus ==2;
            });
            const pass = details.filter((it)=>{
              return it.auditStatus ==1;
            });
            const unPass = details.filter((it)=>{
              return it.auditStatus ==0;
            });
            if(unAuth.length>0){        //部分审核
              parent.auditStatus = 4;
            }else if(pass.length==details.length){      //全部通过
              parent.auditStatus = 1;
            }else if(unPass.length==details.length){      //全部不通过
              parent.auditStatus = 0;
            }else{      //部分通过
              parent.auditStatus = 3;
            }

            if(status==1){      //通过成功
              row.startTime = this.detailParam.startTime;
              row.endTime = this.detailParam.endTime;
              row.payStatusName = '已付款';
            }else{              //不通过成功
              if(this.detailParam.rejectReason===''){
                const reason = this.reasons.filter((it)=>{
                  return it.key === this.detailParam.rejectType;
                }); 
                row.rejectTypeName = reason[0]&&reason[0].name;
              }else{
                row.rejectTypeName = this.detailParam.rejectReason;console.log(this.detailParam.rejectReason)
              }
            }
            this.$message({
              showClose: true,
              message:'操作成功',
              type:'success'
            });
            //this.getDataList(); //操作成功刷新列表
          }else{
            this.$message({
              showClose: true,
              message:res.data.msg,
              type:'warning'
            });
            this.getDataList();//操作成功刷新列表
          }
          this.clearStatus();
        });
      },/*
      getDetailTable(row){
        const param = {
          orderNum: row.num
        };
        api.proDetails(param).then((res)=>{
          if(res.data.code=='0'){
            this.detail = res.data.data;        //对象被修改不会更新vue数据，所以单独用一个变量来引起变化
            this.detailData[row.num] = res.data.data;
          }else{
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'warning'
            });
          }
        }).catch((error)=>{
          this.$message({
            showClose: true,
            message: error,
            type: 'warning'
          });
        });
      },*/
      getFilterItems() {
        const time = this.filter.time;
        const startTime = time&&time[0]||undefined;
        const endTime = time&&time[1]||undefined;
        const param = {
          startTime,
          endTime,
          num: this.filter.orderNum,
          orgName: this.filter.orgName,
          auditStatus: this.filter.status,
          current: this.currentPage,
          size: this.pageSize
        };
        return param;
      },
      indexMethod(index) {
        return ((this.currentPage - 1) * this.pageSize) + index + 1;
      },
      currentChange(next) {
        this.currentPage = next;
        if (this.cacheData[next]) {       //如果已请求过该页数据，则使用缓存不重复请求
          this.list = this.cacheData[next];
        } else {
          this.getDataList();
        }
      },
      exportProductLines() {
        const canExport = utils.exportCondition(this.filter.time)
        if(typeof canExport == 'string'){
          this.$message({
            showClose: true,
            message:canExport,
            type:'warning'
          });
          return;
        }
        const param = this.getFilterItems();
        api.exportProductLines(param).then((res) => {
          if (res.data.code = '0') {
            utils.downloadExportedData(res.data,'产品申请列表.xls');
            this.$message({showClose: true,message: "导出成功", type: 'success'});
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'warning'
            });
          }
        }).catch((error) => {
          this.$message({
            showClose: true,
            message: "服务器正忙...",
            type: 'warning'
          });
        });
      }
    }
  }
</script>

<style lang="less" scoped>
    @import "../../less/admin.less";
    .status-span{
        font-size: 14px;
        margin-right:10px;
        color: #aaa;
    }
    .closeDate {
      position: absolute;
      top: 0px;
      right: 7px;
      cursor: pointer;
      font-size: 20px;
    }
</style>