<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"/>
      <el-select size="mini" multiple v-model="surveyState" placeholder="状态" class="filter-item" style="width: 490px">
        <el-option
          v-for="item in surveyStateOptions"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        size="mini"
        v-model="searchTime"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd HH:mm:ss"
        :default-time="['00:00:00', '23:59:59']">
      </el-date-picker>
      <!--调查部门搜索-->
      <el-input size="mini" v-model="search.surveyDept" placeholder="调查部门" style="width: 200px;" class="filter-item"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <div class="top-right-btn">
        <el-switch
          size="mini"
          class="filter-item"
          v-model="queryDeptFinish"
          style="margin-right: 1em"
          inactive-color="#13ce66"
          active-text="调查完成"
          inactive-text="调查未完成"
          @change="getInfo()"
        >
        </el-switch>

        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      max-height="700"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      size="mini"
      ref="table"
      row-key="id"
      :row-style="customRowStyle"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
<!--          <el-button-->
<!--            v-if="row.surveyState==5"-->
<!--            @click="seeDetails(row)"-->
<!--            type="green"-->
<!--            size="mini">-->
<!--            查看-->
<!--          </el-button>-->
          <el-button
            @click="reviewBill(row)"
            type="primary"
            size="mini">
            行政审核
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="50">
        <template slot-scope="scope">
          <span>{{ (listData.page - 1) * listData.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调查单号" prop="surveyBillCode" width="130">
      </el-table-column>
      <el-table-column
        prop="complaintBillCode"
        label="投诉单号"
        width="130">
        <template slot-scope="{row}">
          <el-link type="primary" @click="openStep(row)">{{row.complaintBillCode}}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="tsType"
        label="调查类型"
        width="70"
        align="center">
        <template slot-scope="{row}">
          <el-tag size="mini" v-if="row.surveyType == 1" type="warning">内部</el-tag>
          <el-tag size="mini" v-else-if="row.surveyType == 2" type="primary">外部</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="调查单状态"
        width="100">
        <template slot-scope="{row}">
<!--          <el-tag v-if="row.surveyState==0" type="primary" size="mini">-->
<!--            初始-->
<!--          </el-tag>-->
          <el-tag v-if="row.surveyState==1 || row.surveyState==0" type="warning" size="mini">
            待调查
          </el-tag>
          <el-tag v-else-if="row.surveyState==2" type="danger" size="mini">
            待主管审核
          </el-tag>
          <el-tag v-else-if="row.surveyState==3" type="info" size="mini">
            待结单
          </el-tag>
          <el-tag v-else-if="row.surveyState==4" type="success" size="mini">
            待行政审核
          </el-tag>
          <el-tag v-else-if="row.surveyState==5" type="info" size="mini">
            行政审核完成
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="投诉标题"
        prop="tsTitle"
        width="100"
      >
      </el-table-column>
      <el-table-column
        label="登记人"
        prop="crePerson"
        width="70"
      >
      </el-table-column>
      <el-table-column
        prop="creDept"
        label="登记部门"
        width="100">
      </el-table-column>
      <el-table-column
        prop="clientName"
        label="客户名称"
      show-overflow-tooltip></el-table-column>
      <el-table-column
        prop="orderCode"
        label="订单编号"
        width="125"></el-table-column>
      <el-table-column
        prop="sevPerson"
        label="客服跟踪人"
        width="91"></el-table-column>
      <el-table-column
        prop="creDate"
        label="登记时间"
        width="160">
        <template slot-scope="{row}">
          <span v-if="row.creDate != null">{{ row.creDate | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="tsLimitDate"
        label="到期时间"
        width="160">
        <template slot-scope="{row}">
          <span v-if="row.tsLimitDate != null">{{ row.tsLimitDate | getTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="listData.total>0"
      :total="listData.total"
      :page.sync="listData.page"
      :limit.sync="listData.pageSize"
      @pagination="getInfo"/>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
<!--    <el-dialog title="投诉单" :visible.sync="complaintDialog" width="1250px" top="20px" class="complaintDialog">-->
      <complaint-details ref="complaintDetail" :type="detailsType" @tableReload='acceptSuccess'></complaint-details>
<!--    </el-dialog>-->
    <complaint-step ref="complaintStep"></complaint-step>
  </div>
</template>

<script>
import {
  getSurveyBillList
} from '@/api/complaint/complaintStatement'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import ComplaintDetails from '@/components/complaintDetails/complaintDetails'
import ComplaintStep from "@/components/complaintStep/complaintStep";

const form = {}
export default {
  name: 'complaintReview',
  components: {
    ComplaintStep,
    ComplaintDetails,
    Cancledialog, Pagination
  },
  data() {
    return {
      queryDeptFinish: false,
      detailsType: 0,
      form: Object.assign({}, form),
      search: {
        'keyWord': '',
        complaintBeginTime:'',
        complaintEndTime:'',
        surveyState: '',
        surveyDept:'',
        deptFinishFlag:0,
      },
      surveyState: [0,1,2,3,4,5],
      searchTime: [],
      surveyStateOptions: [
        {
          label: '初始',
          value: 0
        },
        {
          label: '待反馈',
          value: 1
        },
        {
          label: '待审核',
          value: 2
        },
        {
          label: '待结单',
          value: 3
        },
        {
          label: '待行政审核',
          value: 4
        },
        {
          label: '行政审核完成',
          value: 5
        },
      ],
      tableData: [],
      listData: {
        total: 0,
        page: 1,
        pageSize: 20
      },
      listLoading: false,

      // 弹窗投诉单详情
      complaintDialog: false
    }
  },
  watch: {
    complaintDialog(val) {
      // console.log(val);
      if (!val) {
        this.getInfo()
      }
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    // 获取列表
    getInfo() {
      const _that = this
      _that.listLoading = true

      _that.search.surveyState = _that.surveyState.join(',')
      if (_that.searchTime == null || _that.searchTime < 0) {
        _that.search.complaintBeginTime = ''
        _that.search.complaintEndTime = ''
      } else {
        _that.search.complaintBeginTime = _that.searchTime[0]
        _that.search.complaintEndTime = _that.searchTime[1]
      }
      if (_that.queryDeptFinish) {
        _that.search.deptFinishFlag = 1
      } else {
        _that.search.deptFinishFlag = 0
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }

      getSurveyBillList(data).then(response => {
        console.log('getSurveyBillList', response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getInfo()
    },
    // 调查单脚印
    openStep(row) {
      const _that = this
      _that.$refs.complaintStep.idComplaintBill = row.idComplaintBill
      _that.$refs.complaintStep.getComplaintStep()
    },

    customRowStyle({row, rowIndex}) {
      if (row.tsLimitDate) {
        var times = row.tsLimitDate.substring(0, 19)
        var nowTime = new Date() // 当前时间
        var endD = new Date(times) // 对比时间
        if (nowTime.getTime() > endD.getTime()) {
          return {
            color: 'red'
          }
        }
      }
    },
    // 对比时间
    timeContrast(time) {
      var times = time.substring(0, 19)
      var nowTime = new Date() // 当前时间
      var endD = new Date(times) // 对比时间
      if (nowTime.getTime() > endD.getTime()) { // getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
        // alert('当前时间大于对比时间')
        return true
      } else {
        // alert('当前时间小于对比时间')
        return false
      }
    },
    // 刷新表格
    tableRefresh() {
      this.getInfo()
    },
    // 受理成功
    acceptSuccess() {
      this.complaintDialog = false
      this.getInfo()
    },
    // 行政审核
    reviewBill(row) {
      const _that = this
      _that.detailsType = 4
      _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.surveyId = row.id;
        _that.$refs.complaintDetail.complaintId = row.idComplaintBill
        _that.$refs.complaintDetail.registerInfo.finishMemo = ''
        _that.$refs.complaintDetail.reviewInfo.adminReviewDoc = ''
        _that.$refs.complaintDetail.getInfo()
        _that.$refs.complaintDetail.getSurveyList()
      })
    },
    // 查看
    seeDetails(row) {
      const _that = this
      _that.detailsType = 4
      _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.complaintId = row.idComplaintBill
        _that.$refs.complaintDetail.getInfo()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.complaintDialog{
   ::v-deep .el-dialog__body{
     padding-top: 0px ;
   }
}

</style>
