<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-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加投诉登记
      </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"
      style="width: 100%;margin-bottom: 20px;"
      size="mini"
      row-key="id"
      border
      :row-style="customRowStyle"
      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.billFlag == 0" @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
<!--          <el-button v-else-if="row.billFlag == 3 || row.billFlag == 4" @click="registerBill(row)" type="primary" size="mini">-->
<!--            结单-->
<!--          </el-button>-->
          <el-button type="green" size="mini" @click="seeDetails(row)">查看</el-button>
          <el-button v-if="row.billFlag == 0" type="danger" size="mini" @click="deleteBill(row)">删除</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="tsTitle"
      >
      </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
        prop="maxPaperSize"
        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="billCode"
        label="投诉单号"
        width="125">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="clientName"
        label="客户名称"></el-table-column>
      <el-table-column
        prop="orderCode"
        label="订单编号"
        width="125"></el-table-column>
      <el-table-column
        prop="sevPerson"
        label="客服跟踪人"
      width="95"></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>
<!--        <template slot-scope="{row}">-->
<!--          <span :class="timeContrast(row.tsLimitDate) ? 'textRed':''">-->
<!--            <span v-if="row.tsLimitDate != null">{{ row.tsLimitDate | getTime }}</span>-->
<!--          </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"/>

    <complaint-registration ref="complaintRegistration" @tableRefresh="tableRefresh"></complaint-registration>
    <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>-->
  </div>
</template>

<script>
import {
  getComplaintBillList,
} from '@/api/complaint/complaintRegister'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import ComplaintDetails from '@/components/complaintDetails/complaintDetails'
import ComplaintRegistration from "@/components/complaintRegistration/complaintRegistration";

export default {
  name: 'ComplaintRegister',
  components: {
    ComplaintRegistration,
    ComplaintDetails,
    Cancledialog, Pagination
  },
  data() {
    return {
      detailsType: 0,
      complaintDialog: false,

      search: {
        keyWord: '',
        createBy: '',
        complaintFlag: 1
      },
      tableData: [],
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      confirmdialog: false,
      listLoading: false,
      rules: {
        finishHours: [{ required: true, trigger: 'blur', message: '请填写完成时间' }],
        tsTitle: { required: true, trigger: 'blur', message: '请选择投诉标题' },
        tsType: { required: true, trigger: 'blur', message: '请选择投诉类型' }
      }
    }
  },
  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)
      }

      getComplaintBillList(data).then(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'
          }
        }
      }
    },

    // 对比时间
    timeContrast(time) {
      if (time == null) {
        return false
      }
      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
      }
    },
    // 增加投诉登记
    handleCreateAdd() {
      const _that = this
      _that.$refs.complaintRegistration.handleCreateAdd()
    },
    // 刷新表格
    tableRefresh() {
      this.getInfo()
    },
    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.$refs.complaintRegistration.handleUpdate(row)
    },
    // 结单
    registerBill(row) {
      const _that = this
      _that.detailsType = 3
      // _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.complaintId = row.id
        _that.$refs.complaintDetail.registerInfo.finishMemo = ''
        _that.$refs.complaintDetail.getInfo()
      })
    },
    // 查看
    seeDetails(row) {
      const _that = this
      // console.log(row);
      // _that.complaintDialog = true
      _that.$nextTick(() => {
        _that.$refs.complaintDetail.complaintDialog = true
        _that.$refs.complaintDetail.complaintId = row.id
        _that.$refs.complaintDetail.getInfo()
      })
    },
    // 成功
    acceptSuccess() {
      this.complaintDialog = false
      this.getInfo()
    },
    deleteBill(row) {
      var _that = this
      var printerTypeInfo = {
        id: row.id,
        voidFlag: '1'
      }
      var params = {
        'complaintBill': JSON.stringify(printerTypeInfo)
      }

      var url = '/complaint/voidComplaintBill'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getInfo()
    },

  }
}
</script>
<style>

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
input[type='number'] {
  -moz-appearance: textfield;
}
</style>
<style scoped>
.textRed {
  color: red;
}
</style>
