<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="surveyFlag" placeholder="调查单状态" class="filter-item" style="width: 260px">
        <el-option
          v-for="item in billFlagOptions"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input size="mini" v-model="search.orderCode" placeholder="订单编号" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.clientName" placeholder="客户名称" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.tsTitle" placeholder="投诉标题" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.crePerson" placeholder="登记人" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.billCode" placeholder="投诉单号" style="width: 200px;" class="filter-item"/>
      <el-input size="mini" v-model="search.sevPerson" placeholder="客服追踪人" style="width: 200px;" class="filter-item"/>
      <el-date-picker
        size="mini"
        v-model="searchTime"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd">
      </el-date-picker>
      <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"
      v-loading="listLoading"
      max-height="700"
      style="width: 100%;margin-bottom: 20px;"
      ref="table"
      size="mini"
      row-key="id"
      :row-style="customRowStyle"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            @click="surveyBill(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="complaintBillCode"
        width="150px"
        show-overflow-tooltip
      >
        <template slot-scope="{row}">
          <el-link type="primary" @click="openStep(row)">{{row.complaintBillCode}}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="surveyCode"
        width="130px"
        label="调查单号">
      </el-table-column>
      <el-table-column
        prop="surveyDept"
        width="150px"
        label="调查部门">
      </el-table-column>
      <el-table-column
        prop="maxPaperSize"
        label="部门调查状态"
      width="85">
        <template slot-scope="{row}">
          <el-tag v-if="row.surveyFlag==0" type="primary" size="mini">
            待反馈
          </el-tag>
          <el-tag v-else-if="row.surveyFlag==1" type="warning" size="mini">
            待审核
          </el-tag>
          <el-tag v-else-if="row.surveyFlag==2" type="success" size="mini">
            审核完成
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderName"
        label="订单名称"
      show-overflow-tooltip>
      </el-table-column>

      <el-table-column
        prop="orderCode"
        label="订单编号"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="tsType"
        label="投诉类型">
        <template slot-scope="{row}">
          <span v-if="row.tsType == '1'">内部投诉</span>
          <span v-else-if="row.tsType == '2'">外部投诉</span>
        </template>
      </el-table-column>
      <el-table-column
        label="客户名称"
        prop="clientName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="登记人"
        prop="crePerson"
      >
      </el-table-column>
      <el-table-column
        label="投诉标题"
        prop="tsTitle"
      >
      </el-table-column>
      <el-table-column
        label="涉及金额"
        prop="relateMoney"
      >
      </el-table-column>
      <el-table-column
        label="客服追踪人"
        prop="sevPerson"
      >
      </el-table-column>
      <el-table-column
        label="调查单状态"
        width="85">
        <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="warning" size="mini">
            待行政审核
          </el-tag>
          <el-tag v-else-if="row.surveyState==5" type="success" size="mini">
            行政审核完成
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="surveyPerson"
        label="调查人"
        width="125">
      </el-table-column>
      <el-table-column
        prop="feedBackDate"
        label="调查时间"
        width="160">
        <template slot-scope="{row}">
          <span v-if="row.feedBackDate != null">{{ row.feedBackDate | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="deptReviewer"
        label="部门审核人"></el-table-column>
      <el-table-column
        prop="deptReviewDate"
        label="审核时间"
        width="160">
        <template slot-scope="{row}">
          <span v-if="row.deptReviewDate != null">{{ row.deptReviewDate | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="sevMoneyOut"
        label="损失金额"
        width="160">
      </el-table-column>
      <el-table-column
        label="登记部门"
        prop="creDept"
      >
      </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='feedBackSuccess' :currentDeptId = 'currentDeptId'></complaint-details>
<!--    </el-dialog>-->
    <complaint-step ref="complaintStep"></complaint-step>
  </div>
</template>

<script>
import {
  getDeptSurveyInfoList
} from '@/api/complaint/complaintSurvey'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import ComplaintDetails from '@/components/complaintDetails/complaintDetails'
import {ifPermission} from "@/api/order/orderList";
import ComplaintStep from "@/components/complaintStep/complaintStep";

export default {
  name: 'complaintSurvey',
  components: {
    ComplaintStep,
    ComplaintDetails,
    Cancledialog, Pagination
  },
  data() {
    return {
      detailsType: 0,
      form: {},
      search: {
        'keyWord': '',
        // 'finishFlag':1,
        complaintBeginTime: '',
        complaintEndTime: '',
        orderCode: '',
        clientName: '',
        tsTitle: '',
        crePerson: '',
        billCode: '',
        sevPerson: '',
        surveyFlag:'',
      },

      surveyFlag:[0,1],
      searchTime:[],
      billFlagOptions: [
        {
          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,
      permissionFlag: false,
      // 弹窗投诉单详情
      complaintDialog: false,
      currentDeptId: '' // 当前调查单所属部门id
    }
  },
  watch: {
    complaintDialog(val) {
      // console.log(val);
      if (!val) {
        this.getInfo()
      }
    }
  },
  mounted() {
    this.getInfo()
    this.checkPermission()
  },
  methods: {
    // 获取列表
    getInfo() {
      const _that = this
      _that.listLoading = true

      // _that.search.surveyState = _that.surveyState.join(',')
      _that.search.surveyFlag = _that.surveyFlag.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]
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }

      getDeptSurveyInfoList(data).then(response => {
        console.log('getDeptSurveyInfoList', 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()
    },
    // 对比时间
    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()
    },
    // 调查
    surveyBill(row) {
      const _that = this
      _that.detailsType = 2
      _that.complaintDialog = true

      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.examineFlag = _that.permissionFlag
        _that.$refs.complaintDetail.complaintId = row.idComplaintBill
        _that.$refs.complaintDetail.currentDeptName = row.surveyDept
        _that.$refs.complaintDetail.deptSurveyId = row.id
        _that.$refs.complaintDetail.surveyId = row.idSurveyBill
        _that.$refs.complaintDetail.currentDeptId = row.idSurveyDept
        _that.$refs.complaintDetail.surveyType = 1
        _that.$refs.complaintDetail.getInfo()
      })
    },
    // 受理成功
    feedBackSuccess() {
      // this.complaintDialog = false
      this.getInfo()
    },

    // 判断权限
    checkPermission() {
      const _that = this
      var data = {
        permission: 'complaint:survey:check'
      }
      ifPermission(data).then(res => {
        if (res.data == 1) {
          _that.permissionFlag = true
        }
      })
    },

    // 调查单脚印
    openStep(row,type) {
      const _that = this
        _that.$refs.complaintStep.idComplaintBill = row.idComplaintBill
      _that.$refs.complaintStep.getComplaintStep()
    },
    // 查看
    seeDetails(row) {
      const _that = this
      _that.detailsType = 0
      _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.complaintId = row.id
        _that.$refs.complaintDetail.deptList = []
        _that.$refs.complaintDetail.complaintBill.sevPersonDoc = ''
        _that.$refs.complaintDetail.getInfo()
      })
    }
  }
}
</script>

<style scoped>

.textRed {
  color: red;
}

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