<template>
  <lay-container fluid="true" style="padding: 20px">
    <lay-row :space="10">
      <!-- 搜索区域 -->
      <lay-col :md="24">
        <!-- <lay-card>
          <lay-form style="margin-top: 20px">
            <lay-row>
              <lay-col :md="5">
                <lay-form-item label="科室" label-width="80">
                  <lay-select v-model="searchQuery.group_id" placeholder="请选择类型" :allow-clear="true">
                    <lay-select-option value="服务态度" label="服务态度"></lay-select-option>
                    <lay-select-option value="医疗质量" label="医疗质量"></lay-select-option>
                    <lay-select-option value="收费问题" label="收费问题"></lay-select-option>
                    <lay-select-option value="其他" label="其他"></lay-select-option>
                  </lay-select>
                </lay-form-item>
              </lay-col>
              <lay-col :md="4">
                <lay-form-item label-width="0">
                  <lay-button type="primary" @click="toSearch">查询</lay-button>
                  <lay-button @click="toReset" style="margin-left: 10px">重置</lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-card> -->
      </lay-col>

      <!-- 列表区域 -->
      <lay-col :md="24">
        <lay-card>
          <div style="padding: 10px">
            <span style="font-size: 18px;vertical-align: center;margin-right: 20px">投诉记录列表</span>
          </div>
          <lay-table height="600px" :columns="columns" :data-source="dataSource" :page="page" @change="change">
            <template #status="{ row }">
              <span v-if="row.acceptance_status == 1" style="color: #FFB800">待处理</span>
              <span v-if="row.acceptance_status == 2" style="color: #00CC66">已回复</span>
            </template>
            <template v-slot:operator="{ row }">
              <lay-space size="lg">
                <span v-if="row.acceptance_status == 1" style="color: #00A394;cursor: pointer" @click="addUpdate(row)">处理</span>
                <span style="color: #00A394;cursor: pointer" @click="changeVisible11(row)">详情</span>
              </lay-space>
            </template>
          </lay-table>
        </lay-card>
      </lay-col>
    </lay-row>
    <lay-layer v-model="addShow" title="投诉处理" :shade="true" :area="['800px', '80%']" :btn="addButton">
      <lay-container fluid="true" style="padding: 20px">
        <lay-form :model="addData" label-position="top">
          <lay-form-item required label="投诉内容：" prop="politics_content">
            <div>{{ info.politics_content }}</div>
            <div v-if="info.politics_images != '' && info.politics_images != null" style="margin-top: 10px">
              <a :href="info.politics_images" target="_blank"><img :src="info.politics_images" style="width: 100px;"></a>
            </div>
          </lay-form-item>
          <lay-form-item required label="处理结果：" prop="acceptance_content">
            <lay-textarea placeholder="请输入处理结果" v-model="addData.acceptance_content"></lay-textarea>
          </lay-form-item>
        </lay-form>
      </lay-container>
    </lay-layer>
    <!-- 添加详情弹窗 -->
    <lay-layer v-model="detailShow" title="投诉建议详情" :type="4" :shade="true" :area="['700px', '100%']">
        <div class="detail-container">
          <div class="detail-content">
            <div class="detail-item">
              <div class="detail-label">姓名：</div>
              <div class="detail-value">{{ detailInfo.user_name }}</div>
            </div>
            <div class="detail-item">
              <div class="detail-label">科室：</div>
              <div class="detail-value">{{ detailInfo.user_group_name }}</div>
            </div>
            <div class="detail-item">
              <div class="detail-label">投诉时间：</div>
              <div class="detail-value">{{ detailInfo.createtime }}</div>
            </div>
            <div class="detail-item">
              <div class="detail-label">处理状态：</div>
              <div class="detail-value">
                <span v-if="detailInfo.acceptance_status == 1" class="status-pending">待处理</span>
                <span v-if="detailInfo.acceptance_status == 2" class="status-replied">已回复</span>
              </div>
            </div>
            <div class="detail-item detail-item-full">
              <div class="detail-label">投诉内容：</div>
              <div class="detail-value">{{ detailInfo.politics_content }}</div>
            </div>
            <div class="detail-item detail-item-full" v-if="detailInfo.politics_images && detailInfo.politics_images !== ''">
              <div class="detail-label">投诉图片：</div>
              <div class="detail-value">
                <a :href="detailInfo.politics_images" target="_blank">
                  <img :src="detailInfo.politics_images" class="complaint-image">
                </a>
              </div>
            </div>
            <div class="detail-item detail-item-full" v-if="detailInfo.acceptance_status == 2">
              <div class="detail-label">处理结果：</div>
              <div class="detail-value">{{ detailInfo.acceptance_content }}</div>
            </div>
            <div class="detail-item" v-if="detailInfo.acceptance_status == 2">
              <div class="detail-label">处理时间：</div>
              <div class="detail-value">{{ detailInfo.acceptance_time }}</div>
            </div>
          </div>
        </div>
    </lay-layer>
  </lay-container>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import {
  getPoliticsList,getPoliticsEdit
} from "@/api/module/home";
import { layer } from '@layui/layui-vue';

const addData = reactive({
  acceptance_content: '',
  id:0
})

// 添加详情弹窗相关变量和方法
const detailShow = ref(false)
const detailInfo = ref({})
const changeVisible11 = (row: any) => {
  detailInfo.value = row
  detailShow.value = true
}

onMounted(() => {
  getList();
})

const addShow = ref(false)
const info = ref({});
const addUpdate = (row: any) => {
  info.value = row;
  addData.id = row.id;
  addShow.value = true
}
const dataSource = ref([])
const getList = () => {
  getPoliticsList({ size: page.limit, page: page.current, ...searchQuery.value }).then((res) => {
    console.log(res)
    if (res.code == 1) {
      dataSource.value = res.data.list;
      page.total = res.data.total;
    } else {
      dataSource.value = [];
      layer.msg(res.msg, { icon: 2 })
    }
  })
}

const searchQuery = ref({
  group_id: '',
})
function toReset() {
  searchQuery.value = {
    group_id: '',
  }
}

function toSearch() {
  page.current = 1
  change(page)
}

const loading = ref(false)
const page = reactive({ current: 1, limit: 10, total: 100 })
const columns = ref([
  { title: '编号', width: '90px', key: 'id', align: 'center' },
  { title: '姓名', width: '120px', key: 'user_name', align: 'center' },
  { title: '投诉内容', width: '300px', key: 'politics_content', align: 'center' },
  { title: '科室', width: '120px', key: 'user_group_name', align: 'center' },
  { title: '投诉时间', width: '160px', key: 'createtime', align: 'center' },
  { title: '处理状态', width: '120px', key: 'status', align: 'center', customSlot: 'status' },
  { title: '处理结果', width: '300px', key: 'acceptance_content', align: 'center' },
  { title: '处理时间', width: '200px', key: 'acceptance_time', align: 'center' },
  {
    title: '操作',
    width: '150px',
    customSlot: 'operator',
    key: 'operator',
    fixed: 'right',
    align: 'center'
  }
])
const change = (p: any) => {
  loading.value = true
  page.current = p.current;
  page.limit = p.limit;
  getList();
}
const addButton = ref([
  {
    text: "确认",
    callback: async () => {
      console.log(addData);
      if (addData.acceptance_content == '') {
        layer.msg('处理结果不能为空！', { icon: 2 })
        return;
      }
      var res = await getPoliticsEdit(addData);
      console.log(res)
      if (res.code == 1) {
        layer.msg('处理成功！', { icon: 1 })
        addShow.value = false;
        getList();
      } else {
        layer.msg(res.msg, { icon: 2 })
      }
    }
  },
    {
    text: "取消",
    callback: () => {
      addShow.value = false;
    }
  }
])
</script>

<style>
.layui-table-header .layui-table-cell {
  background-color: #ECF8FA !important;
}

.search-input {
  display: inline-block;
  width: 98%;
  margin-right: 10px;
}
.detail-header {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
}

.detail-header h3 {
  margin: 0;
  color: #333;
  font-size: 18px;
  font-weight: 500;
}

.detail-content {
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
}

.detail-item {
  width: 50%;
  margin-bottom: 20px;
  display: flex;
}

.detail-item-full {
  width: 100%;
}

.detail-label {
  width: 100px;
  color: #606266;
  font-weight: 500;
  text-align: right;
  padding-right: 12px;
  line-height: 24px;
}

.detail-value {
  flex: 1;
  color: #333;
  line-height: 24px;
  word-break: break-all;
}

.complaint-image {
  max-width: 300px;
  max-height: 300px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.complaint-image:hover {
  transform: scale(1.02);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.status-pending {
  color: #1E9FFF;
  font-weight: 500;
}

.status-replied {
  color: #FFB800;
  font-weight: 500;
}
</style>