<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" v-model="search.isFinish">
        <el-option
         label="未结单"
         value="0"></el-option>
        <el-option
        label="已结单"
        value="1"></el-option>
      </el-select>
      <el-button size="mini"  class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <div class="top-right-btn">
        <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"
      :row-style="customRowStyle"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      size="mini"
      ref="table"
      row-key="id"
      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>3"
            @click="seeDetails(row)"
            type="green"
            size="mini">
            查看
          </el-button>
          <el-button
            v-else-if="row.surveyState<=3"
            @click="registerBill(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="135">
      </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
        label="投诉标题"
        width="105"
        prop="tsTitle"
      >
      </el-table-column>
      <el-table-column
        label="调查类型">
        <template slot-scope="{row}">
          <span v-if="row.surveyType == 1">内部调查</span>
          <span v-else-if="row.surveyType == 2">外部调查</span>
        </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-else-if="row.surveyState==1" 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="info" 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="投诉单状态"
        width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.billFlag==0" type="primary" size="mini">
            新建
          </el-tag>
          <el-tag v-else-if="row.billFlag==1" type="warning" size="mini">
            待受理
          </el-tag>
          <el-tag v-else-if="row.billFlag==2" type="danger" size="mini">
            待分配
          </el-tag>
          <el-tag v-else-if="row.billFlag==3" type="info" size="mini">
            调查中
          </el-tag>
          <el-tag v-else-if="row.billFlag==4" type="success" size="mini">
            调查完成
          </el-tag>
          <el-tag v-else-if="row.billFlag==5" type="danger" size="mini">
            登记人结单
          </el-tag>
          <el-tag v-else-if="row.billFlag==6" type="green" size="mini">
            行政审核完成
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="lockMan"
        label="锁定人"
        width="91">
        <template slot-scope="{row}">
          <span
            :class="(row.lockFlag == 1 && row.idLockMan == $store.state.user.userId) ?'text-blue':''">{{
              row.lockMan
            }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="clientName"
        label="客户名称"
      width="120px"
      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">-->
      <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: 'CustomerPage',
  components: {
    ComplaintStep,
    ComplaintDetails,
    Cancledialog, Pagination
  },
  data() {
    return {
      detailsType: 0,
      form: Object.assign({}, form),
      search: {
        'keyWord': '',
        'createBy': '',
        'isFinish': '0',
        'surveyType': 1
      },
      tableData: [],
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      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.createBy = _that.$store.state.user.userId
      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('getComplaintBillList', 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
      })
    },

    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'
          }
        }
      }
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getInfo()
    },

    // 调查单脚印
    openStep(row) {
      const _that = this
      _that.$refs.complaintStep.idComplaintBill = row.idComplaintBill
      _that.$refs.complaintStep.getComplaintStep()
    },
    // 对比时间
    timeContrast(time) {
      var times
      if (time) {
        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()
    },
    // 结单
    registerBill(row) {
      const _that = this
      _that.detailsType = 3
      _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.getInfo()
      })
    },
    // 查看
    seeDetails(row) {
      const _that = this
      _that.detailsType = 0
      _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.complaintId = row.idComplaintBill
        _that.$refs.complaintDetail.getInfo()
      })
    }
  }
}
</script>

<style scoped>

.textRed {
  color: red;
}

.text-blue {
  color: dodgerblue;
}
</style>
