<template>
  <el-dialog 
    :visible.sync="showCK" 
    width="80%" 
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false" 
    custom-class="travel"
  >
    <div slot="title" class="dialog-title">
      <div class="dialog-title-text">{{ title }}</div>
      <i @click="close" class="el-icon-close"></i>
    </div>
    <div class="audit-generalize">
      <el-row>
        <el-col :span="8">
          <div class="grid-content" v-if="auditStatus == 1">
            <div class="grid-title">概括名称：</div>
            <div class="grid-value-btn">
              <el-tooltip class="item" effect="light" :content="audioInfo.title" placement="top">
                <!--  @blur="titleBlur" -->
                <el-input class="grid-value-value" v-loading="InputLoading" v-model="audioInfo.title" placeholder="概括名称" :disabled="disabled"></el-input>
              </el-tooltip>
              <el-button size="mini" @click="changeTitle"><img src="@/assets/images/changeTitle.png" alt=""> 修改标题</el-button>
            </div>
          </div>
          <div class="lookTitle" v-else>
            <div class="grid-content">
              <div class="grid-title">概括名称：</div>
              <el-tooltip class="item" effect="light" :content="audioInfo.title" placement="top">
                <div class="grid-value">{{ audioInfo.title }}</div>
              </el-tooltip>
            </div>
            <el-button size="mini" v-if="audioInfo.titleBefore" @click="lookTitleBefore"><img src="@/assets/images/changeTitle.png" alt=""> 查看原标题</el-button>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <div class="grid-title">预警时间：</div>
            <div class="grid-value">{{ audioInfo.firstWarnTimeDate || audioInfo.caseDate }}</div>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <div class="grid-title">事件数量：</div>
            <div class="grid-value">{{ audioInfo.nums }}条</div>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <div class="grid-content">
            <div class="grid-title">预警因素：</div>
            <el-tooltip class="item" effect="light" :content="audioInfo.warnFactor" placement="top">
              <div class="grid-value">{{ audioInfo.warnFactor }}</div>
            </el-tooltip>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content">
            <div class="grid-title">预警类别：</div>
            <div class="grid-value warning-type">{{ audioInfo.msgType == '1' ? "群体事件" : audioInfo.msgType == '2' ? "同人同诉" : audioInfo.msgType == '3' ? "紧急事件" : audioInfo.msgType == '4' ? "重点人员" : audioInfo.msgType == '5' ? "一人多诉" : "" }}</div>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="audit-table">
      <section class="table-box">
        <div class="table-item">
          <el-table v-loading="loading" :data="tableData" :cell-class-name="addStyle" border>
            <el-table-column
              type="index"
              label="序号"
              width="55"
              align="center"
            />
            <el-table-column label="工单编号" align="center" prop="caseSerial">
            </el-table-column>
            <el-table-column label="案件描述" width="180" align="center" prop="caseContent">
              <template slot-scope="scope">
                <el-tooltip class="item" effect="light" placement="top">
                  <template slot="content">
                    <p style="width: 400px;">{{ scope.row.caseContent }}</p>
                  </template>
                  <div class="anjianmiaoshuTwo"> {{ scope.row.caseContent }}</div>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column label="地点" align="center" prop="caseAddress">
            </el-table-column>
            <el-table-column label="上报时间" align="center" prop="caseDate">
            </el-table-column>
            <el-table-column label="审核时间" v-if="auditStatus == 2" align="center" prop="auditTime">
            </el-table-column>
            <el-table-column label="审核结果" v-if="auditStatus == 2" align="center" prop="isReport">
              <template slot-scope="scope">
                <span class="sign" v-if="scope.row.isReport == 1 || scope.row.isReport == 2">不符合</span>
                <span class="unmark" v-else>符合</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="operation-box" >
              <template slot-scope="scope">
                <div class="operation">
                  <el-radio-group v-model="scope.row.radio" @change.native="changRadio(scope.row)" v-if="auditStatus == 1">
                    <el-radio v-removeAriaHiddent :label="1">符合</el-radio>
                    <el-radio v-removeAriaHiddent :label="2" value="13212">不符合</el-radio>
                  </el-radio-group>
                  <el-button @click="toDetail(scope.row)" type="text" style="margin-left: 20px;" class="sqbtn sqbtn1">
                    查看详情
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </section>
      <div class="my-pagination">
        <pagination
          :total="total"
          :page="pages.current"
          :limit="pages.size"
          @pagination="getPagination"
          :current-page.sync="pages.current"
        ></pagination>
      </div>
    </div>
    <div slot="footer" class="dialog-footer" v-if="auditStatus == 1">
      <el-button type="primary" @click="reported(1)">上报驾驶舱</el-button>
      <!-- <el-button type="info" plain>不上报</el-button> -->
      <el-button type="danger" @click="reported(2)">退回重训练</el-button>
      <!-- <el-button type="success" @click="reported(3)">审核通过</el-button> -->
    </div>
    <el-dialog
      width="30%"
      title="查看标题"
      :visible.sync="innerVisible"
      append-to-body>
      <el-descriptions :column="1">
        <el-descriptions-item label="当前标题">{{ audioInfo.title }}</el-descriptions-item>
        <el-descriptions-item label="历史标题">{{ audioInfo.titleBefore }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <urgency-audit title="工单详情" ref="urgencyAudit" :auditStatus="auditStatusTwo"></urgency-audit>
  </el-dialog>
</template>
<script>
import { auditList, eventChangeIsReport, editTitle } from "@/api/eventPage"
import urgencyAudit from '@/views/components/urgencyAudit.vue'
export default {
  components:{
    urgencyAudit
  },
  data() {
    return {
      showCK:false,
      audioInfo:{},
      backupsInfo:{},
      loading:false,
      InputLoading:false,
      tableData:[],
      total:0,
      pages: {
        current: 1,
        size: 10
      },
      disabled:true,
      multipleSelection: [],
      innerVisible:false,
      auditStatusTwo: 1,
    }
  },
  props:{
    title:{
      type:String,
      default:""
    },
    auditStatus:{
      type:Number,
      required: true
    }
  },
  methods:{
    // 弹出弹框
    open(row){
      this.audioInfo = {...row};
      this.backupsInfo = {...row};
      this.showCK = true;
      this.getList(row.innerEventId);
    },
    // 关闭弹框
    close(){
      this.showCK = false;
      this.InputLoading = false;
      this.loading = false;
      this.multipleSelection = [];
      this.pages = {
        current: 1,
        size: 10
      }
    },
    getList(id){
      this.loading = true;
      auditList(id,{...this.pages}).then(res=>{
        const deepCopy = JSON.parse(JSON.stringify(res.data.records));
        this.tableData = deepCopy;
        if(this.multipleSelection.length > 0) {
          this.tableData.forEach(item=>{
            this.$set(item, 'radio', 1);
            this.multipleSelection.forEach(itemTwo=>{
              if(itemTwo == item.caseSerial) {
                this.$set(item, 'radio', 2);
              }
            })
          })
        } else {
          this.tableData.map(item=>{
            this.$set(item, 'radio', 1);
          })
        }
        this.total = res.data.total;
        this.loading = false;
      }).catch(err=>{
        this.loading = false;
      })
    },
    // 查看详情
    toDetail(item){
      // console.log(item);
      this.auditStatusTwo = 2;
      this.$refs.urgencyAudit.open(this.audioInfo,item);
    },
    changRadio(row){
      if(row.radio == 2) {
        this.multipleSelection.push(row.caseSerial);
      } else if(row.radio == 1){
        this.multipleSelection = this.multipleSelection.filter((item)=> item !== row.caseSerial)
      }
    },
    // 上报驾驶舱/退回重训练
    reported(id){
      this.$confirm(`确定要${id == 1 ? '重新上报驾驶舱' : id == 2 ? '退回重训练' : id == 3 ? '审核通过':''}吗?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if(id !== 3) {
          // if(this.multipleSelection.length > 0 || this.backupsInfo.title !== this.audioInfo.title) {
            if(this.backupsInfo.title !== this.audioInfo.title) {
              eventChangeIsReport({isReport:id,caseSerial:this.multipleSelection,innerEventId:this.audioInfo.innerEventId,newTitle:this.audioInfo.title,oldTitle:this.backupsInfo.title}).then(res=>{
                // console.log(res);
                this.multipleSelection = [];
                this.$message({
                  type: "success",
                  message: id == 1 ? '已上报驾驶舱' : '已退回重训练' + "!",
                });
                this.$emit('closeDialog')
                this.close();
              })
            } else {
              eventChangeIsReport({isReport:id,caseSerial:this.multipleSelection,innerEventId:this.audioInfo.innerEventId}).then(res=>{
                // console.log(res);
                this.multipleSelection = [];
                this.$message({
                  type: "success",
                  message: id == 1 ? '已上报驾驶舱' : '已退回重训练' + "!",
                });
                this.$emit('closeDialog')
                this.close();
              })
            }
          // } else {
          //   this.$message.error(`您未做任何操作，请修改后再${id == 1 ? '上报驾驶仓' : id == 2 ? '退回重训练' : ''}`);
          // }
        } else {
          if(this.backupsInfo.title !== this.audioInfo.title) {
            this.$message.error(`您已修改标题，请上报驾驶舱或退回重训练`);
          } else if(this.multipleSelection.length > 0) {
            this.$message.error(`您已标记工单为不符合，请上报驾驶舱或退回重训练`);
          } else {
            eventChangeIsReport({isReport:id,innerEventId:this.audioInfo.innerEventId}).then(res=>{
              // console.log(res);
              this.$message({
                type: "success",
                message: "审核完成",
              });
              this.$emit('closeDialog')
              this.close();
            })
          }
        }
      })
    },
    // 查看标题按钮
    lookTitleBefore(){
      this.innerVisible = true;
    },
    // 修改标题按钮
    changeTitle(){
      this.disabled = false;
    },
    // 标题失去焦点
    titleBlur(){
      this.disabled = true;
      if(this.backupsInfo.title !== this.audioInfo.title) {
        // this.InputLoading = true;
        // editTitle({title:this.audioInfo.title, innerEventId:this.audioInfo.innerEventId}).then(res=>{
        //   if(res.code == 200) {
        //     this.$message({
        //       type: "success",
        //       message: "标题修改成功!",
        //     });
        //     this.InputLoading = false;
        //   }
        // }).catch(err=>{
        //   this.InputLoading = false;
        // })
      }
    },
    // 获取页码
    getPagination(pages) {
      this.pages.current = pages.page;
      this.pages.size = pages.limit;
      this.getList(this.audioInfo.innerEventId);
    },
    // 修改第二列样式
    addStyle({row, column, rowIndex, columnIndex}) {
      if(columnIndex === 2) {
        return "columnFactor"
      }
    },
  },
}
</script>
