<template>
    <div v-show="showPlatform" >
        <el-card style="min-height: calc(100vh - 102px);">
            <el-form :model="searchForm" label-width="auto" :inline="true">
                <backPage @click="goBack"></backPage>
                <el-form-item label="姓名/医院：">
                    <el-input style="width:230px" clearable v-model="searchForm.keyword"
                        placeholder="请输入姓名或者医院"></el-input>
                </el-form-item>
                <el-form-item label="优质留言：">
                    <el-select style="width:230px" v-model="searchForm.is_high_quality" placeholder="请选择是否是优质留言" clearable>
                        <el-option label="是"  :value="1" />
                        <el-option label="否" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item label="平台：">
                    <el-select style="width:230px" v-model="searchForm.platform_id
" placeholder="请选择平台" clearable>
                        <el-option label="肝胆相照"  value="1" />
                        <el-option label="佳动例" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                <el-button type="primary" @click="search">查询</el-button>
                <!-- <el-button type="default" @click="onSubmit">重置</el-button> -->
            </el-form-item>
            </el-form>
            <el-divider />
            <!-- <el-row>
                <el-button type="success" @click="add">新增</el-button>
            </el-row> -->
            <el-table :data="tableData" border style="width: 100%;margin-top:20px">
                <el-table-column prop="user_name" label="姓名" width="120">
                </el-table-column>
                <el-table-column prop="content" label="评论内容"  width="200">
                </el-table-column>
                <el-table-column prop="content_word" label="含敏感词时评论内容"  width="180">
                    <template #default="scope">
                       <span>{{scope.row.content_word?scope.row.content_word:''}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="area" label="省份"  width="100">
                </el-table-column>
                <el-table-column prop="title" label="职称"  width="100">
                </el-table-column>
                <el-table-column prop="department_name" label="科室"  width="100">
                </el-table-column>
                <el-table-column prop="hospital_name" label="医院"  width="200">
                </el-table-column>
                <el-table-column prop="like_num" label="点赞数量"  width="90">
                </el-table-column>
                
                <el-table-column prop="is_high_quality" label="是否优质留言"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_high_quality==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_sensitive" label="是否存在敏感词"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_sensitive==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="评论状态"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.status==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="is_grant_high_quality" label="优质留言积分是否已发放"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.is_grant_high_quality==1?'是':'否'}}</span>
                    </template>
                </el-table-column>
                
                <el-table-column prop="level" label="评论类型"  width="100">
                    <template #default="scope">
                       <span>{{scope.row.level==1?'留言':scope.row.level==2?'回复':scope.row.level==3?'评论':''}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="level" label="评论等级"  width="100">
                </el-table-column>
                <el-table-column prop="created_at" label="提交时间"  width="165" />
                <el-table-column label="操作" width="280" fixed="right">
                    <template #default="scope">
                        <!-- <el-button size="small"
                           type="primary"
                            @click="openDetail(scope.row.comment_id,scope.row.content)">修改</el-button> -->
                          <el-button type="default" size="small" @click="handleUpdateStatus(scope.row.comment_id,scope.row.status)">{{ scope.row.status==1?'隐藏':'启用' }}</el-button> 
                          <el-button type="default" size="small" @click="handleUpdateQualityStatus(scope.row.comment_id,scope.row.is_high_quality)" :disabled="scope.row.is_high_quality==1">{{ scope.row.is_high_quality==1?'优质留言':'设置优质留言' }}</el-button> 
                          <el-tag type="danger" v-if="scope.row.is_sensitive==1" class="tag">风险提醒</el-tag>
                          <el-tag type="success" class="tag" v-else>风险提醒</el-tag>
                         </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top:20px" :current-page="pageFrom.page" :page-size="pageFrom.page_size"
                :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </el-card>
        <platformDetail ref="platformDetailRef"  @refreshList="getList" :project_id="project_id"></platformDetail> 
    </div>
     <whiteHospital ref="whiteHospitalRef" v-if="!showPlatform && whiteType==1" ></whiteHospital>
     <whiteDoctor ref="whiteDoctorRef" v-else-if="!showPlatform && whiteType==2" ></whiteDoctor>
     <dynamic ref="dynamicRef" v-else-if="!showPlatform && whiteType==3" ></dynamic>
     <el-dialog v-model="dialogFormVisible" title="修改评论" width="400" id="maincomemnt">
     <el-form :inline="true" :rules="rules"  :model="msgform" class="demo-form-inline" ref="ruleFormRef">
      <el-form-item label="" prop="comment">
        <el-input v-model="msgform.comment" :autosize="{ minRows:6}"  type="textarea" placeholder="请输入评论" style="width:360px;" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitContent('ruleFormRef')">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import userApi from "../api/user";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
import { formatDate } from "../utils/format";
const router = useRouter();
const platformDetailRef = ref(null);
const showPlatform=ref(true)
const whiteHospitalRef = ref(null)
const whiteDoctorRef = ref(null)
const dynamicRef = ref(null)
const ruleFormRef=ref(null);
const msgform = reactive({
     comment:'23',
  });
const comment_id=ref('');
const project_id=ref('')


const whiteType=ref(null);
const emits = defineEmits(['goBack'])
const dialogFormVisible = ref(false)
// Dom 挂载之后

// 用户数据
let tableData = ref([]);
let total = ref(0);
// 搜索条件

const pageFrom = reactive({
    page: 1,
    page_size: 10,
})
const searchForm = reactive({
})
const rules = reactive({
  comment:[
    {
      required: true,
      message: '请输入评论',
      trigger: 'blur',
    }
  ]
})
const goBack=()=>{
    emits('goBack')
}
const add = (id) => {
    platformDetailRef.value.init('view','')
}
const openWhite=(id,type)=>{
    whiteType.value=type
    showPlatform.value=false
    if(type==1){
        nextTick(()=>{
           whiteHospitalRef.value.init(id)
        })
    }else if(type==2){
        nextTick(()=>{
           whiteDoctorRef.value.init(id)
        })
    }else if(type==3){
        nextTick(()=>{
            dynamicRef.value.init(id)
        })
    }
   
}

// 获取用户列表
const getList = async () => {
    if(searchForm.is_high_quality===''){
        delete searchForm.is_high_quality
    }
    const res = await userApi.getCommentList({ ...searchForm, ...pageFrom });
    console.log(res.data);
    tableData.value = res.data.data;
    total.value = res.data.total;
}

const handleUpdateStatus=(id,status)=>{
    userApi.updateCommentStatus(id,{
        status:status==1?0:1
    }).then(res=>{
        let {code}=res;
        if(code==200){
            getList()
            ElMessage.success("操作成功")
        }
})
}
const handleUpdateQualityStatus=(id,status)=>{
    userApi.updateCommentQualityStatus(id,{
        is_high_quality:status==1?0:1
    }).then(res=>{
        console.log(res);
        let {code}=res;
        if(code==200){
            getList()
            ElMessage.success("操作成功")
        }
    })
}
const updateComment=(id,value)=>{
    userApi.updateComment(id,{
            content:value
        }).then(res=>{
           if(res.code==200){
                getList()
                msgform.comment='';
                ElMessage.success("操作成功")
           }
    })
}
const submitContent = async(form) => {
   
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
        dialogFormVisible.value=false
        updateComment(comment_id.value,msgform.comment)
    } else {
      console.log('error submit!', fields)
    }
 
  });
}
const openDetail = (id,value) => {
   
    dialogFormVisible.value=true
    msgform.comment=value;
    comment_id.value=id

//     ElMessageBox.prompt('', '修改评论', {
//     confirmButtonText: '确定',
//     cancelButtonText: '取消',
//     inputValue:value,
//     message:'<el-input v-model="textarea1" style="width: 240px" autosize type="textarea"placeholder="Please input" />',
//     dangerouslyUseHTMLString: true,
//     inputPattern:/\S/,
//     inputErrorMessage: '输入不能为空',

//   })
//     .then(({ value }) => {
//         updateComment(id,value)
//     })
//     .catch(() => {
     
//     })

}
const handleGocase=()=>{

}
const handleGoWhite=()=>{
    
}
const handleGoPlatform=()=>{
    
}
const handleSizeChange = (size) => {
    pageFrom.page_size = size;
    getList();
}
const handleCurrentChange = (current) => {
    pageFrom.page = current;
    getList();
}
const search = () => {
    pageFrom.page =1;
    tableData.value = [];
    getList();
}
const init = (id) => {
    searchForm.case_id = id;
    getList()
}
defineExpose({
    init
})
</script>

<style lang="scss" scoped>
.tag{
    margin-left: 10px;
}

</style>