<template>
  <div>
    <div class="pt-8 border-b-solid-lighter-1">
      <div class="height-30 flex-row align-items-center justify-content-between mb-24">
        <div class="f18 color-text-primary">筛选</div>
        <span v-if="this.DialogDetail.id != undefined" class="f-weight500 f16 cursor-pointer border-solid-lighter-1 p-8 border-radius-8" @click="$emit('update:DialogDetail', null)"><i class="el-icon-back"></i>返回</span>
      </div>
      <el-collapse-transition>
        <el-row :gutter="56">
          <el-col :span="6">
            <div class="flex-row-center align-items-center height-40 mb-24">
              <span class="tabs__search-criteria-title flex-shrink-0 pr-16">短信接收人</span>
              <el-input v-model.trim="queryParam.contact"
                        clearable placeholder="请输入短信接收人"
                        @keydown.enter.native="handleSearch">
              </el-input>
            </div>
          </el-col>
          <el-col :span="5">
            <div class="flex-row-center align-items-center height-40 mb-24">
              <span class="tabs__search-criteria-title flex-shrink-0 pr-16">电话号码</span>
              <el-input v-model.trim="queryParam.phone"
                        clearable placeholder="请输入电话号码"
                        @keydown.enter.native="handleSearch">
              </el-input>
            </div>
          </el-col>
          <el-col :span="9">
            <div class="justify-content-start  align-items-center height-40 mb-24">
              <span class="tabs__search-criteria-title flex-shrink-0 pr-16">操作时间</span>
              <el-date-picker
                  v-model="queryDate"
                  type="daterange"
                  clearable
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  @change="handleChangeDate">
              </el-date-picker>
            </div>
          </el-col>
          <el-col :span="4">
            <div class="flex-row align-items-center justify-content-end">
              <el-button @click="hanldeReset">重置</el-button>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </div>
          </el-col>
        </el-row>
      </el-collapse-transition>
    </div>
    <!-- table -->
    <div class="pt-8">
      <div class="height-56 flex-row align-items-center justify-content-between">
        <div class="f18 color-text-primary">明细列表</div>
        <div class="flex-row">

        </div>
      </div>

      <div class="case-table">
        <el-table :data="tableData" height="340">
          <el-table-column type="index" label="序号" width="55"></el-table-column>
          <!-- <el-table-column prop="pkgName" label="案件包名称" show-overflow-tooltip ></el-table-column> -->
          <el-table-column prop="caseNo" label="案件编号" width="200" show-overflow-tooltip></el-table-column>
          <el-table-column label="短信模板" show-overflow-tooltip >
              <template slot-scope="scope">
                  <span >{{ scope.row.templateId.desc != undefined ?scope.row.templateId.desc :'' }}</span>
              </template>
          </el-table-column>
          <el-table-column prop="contact" label="短信接收人" show-overflow-tooltip></el-table-column>
          <el-table-column prop="phone" label="电话号码" show-overflow-tooltip></el-table-column>
          <el-table-column prop="sendObjectType" label="短信接收对象" show-overflow-tooltip></el-table-column>
          <el-table-column prop="status" label="发送状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.status.desc }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="failureReason" label="失败原因" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createByName" label="操作人" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作时间" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.updateAt | formaDate("yyyy-MM-dd hh:mm:ss") }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <div class="flex-row align-items-center">
                <el-button size="mini" @click="handleRetry(scope.row)" v-if="scope.row.status.code !== 4 && scope.row.status.code !== 2">重试</el-button>
                <el-button size="mini" @click="handleCancel(scope.row)" v-if="scope.row.status.code !== 4 && scope.row.status.code !== 2">取消</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="text-center pt-16">
        <el-pagination
            @size-change="getList"
            @current-change="getList"
            :current-page="queryParam.current"
            :page-size="queryParam.size"
            layout="total, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import api from "@/services/eventTracingApi";
import caseManagement from "@/services/caseManagement";
export default {
  components: {},
  props: {
    DialogDetail: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      queryDate: '',
      queryParam: {
        contact:'',
        traceId: '',
        phone: '',
        beginTime: '',
        endTime: '',
        current: 1,
        size: 10
      },
      tableData: [],
      total: 0,
    };
  },
  mounted() {
    // console.log(this.DialogDetail, 'this.DialogDetail.caseNo')
    
    this.initializeQuery()
    
    this.getList(1)
  },
  methods: {
    initializeQuery(){
      if (this.DialogDetail.caseNo != undefined && this.DialogDetail.caseNo != '') {
        this.queryParam.caseNo = this.DialogDetail.caseNo
      }
      if (this.DialogDetail.caseId != undefined && this.DialogDetail.caseId != '') {
        this.queryParam.caseId = this.DialogDetail.caseId
      }
      this.queryParam.traceId = this.DialogDetail.id
    },
    handleChangeDate() {
      this.activeUsage = ''
      if (this.queryDate === null) {
        this.queryParam.beginTime = ''
        this.queryParam.endTime = ''
        this.queryDate = ''
      } else {
        this.queryParam.beginTime = this.$util.getDayStart(this.queryDate[0])
        this.queryParam.endTime = this.$util.getDayEnd(this.queryDate[1])
      }
    },
    // 重置
    hanldeReset() {
      for (let key in this.queryParam) {
        this.queryParam[key] = ''
      }
      this.queryParam.current = 1
      this.queryParam.size = 10
      this.activeUsage = ''
      this.queryDate = ''
      this.initializeQuery()
    },
    handleSearch() {
      this.queryParam.size = 10;
      this.queryParam.current = 1;
      this.getList(1)
    },
    // 列表数据
    getList(val) {
      this.queryParam.startTime = this.queryParam.beginTime
      // this.queryParam.planBegin = this.queryParam.beginTime
      // this.queryParam.planEnd = this.queryParam.endTime
      this.queryParam.current = val
      api.posttrace_sms_detail(this.queryParam).then(res => {
        if (!res.code) {
          this.tableData = res.records;
          this.total = res.total;
        }
      })
    },
    handleRetry(row) {
      this.$confirm("确定重试?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if(!this.$clickThrottle()) { return }//防止重复点击
        caseManagement.smsRetry({id: row.id}).then(res => {
          this.getList(1);
          this.$message.success("成功");
        })
      }).catch(() => {});
    },
    handleCancel(row) {
      this.$confirm("确定取消?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if(!this.$clickThrottle()) { return }//防止重复点击
        caseManagement.smsCancel({id: row.id}).then(res => {
          this.getList(1);
          this.$message.success("成功");
        })
      }).catch(() => {});
    },
  }
};
</script>
<style scoped lang="scss">
.dialog-content {
  padding: 16px 24px;
  max-height: 500px
}

.department-wrap {
  padding: 16px 24px;
  max-height: 250px;

  .department-wrap-list {
    margin-bottom: 32px;
  }

  .department-wrap-list:last-child {
    margin-bottom: 0;
  }
}

.min-height350 {
  min-height: 350px;
}

.department-dept ::v-deep .el-checkbox__label {
  color: $color-000000
}
</style>