<template>
  <div class="ResidentMainBox p-2 w-full">
    <PageHeader title="投诉详情"></PageHeader>
    <div class="ResidentMainBody">
      <!-- 投诉信息 -->
      <div class="mb-10">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>投诉信息</span>
            </div>
          </template>
          <div>
            <div class="carInfoBox">
              <div class="carInfo_item">
                <div class="label">投诉类型</div>
                <div class="main">{{ form.type }}</div>
              </div>

              <div class="carInfo_item">
                <div class="label">投诉人姓名</div>
                <div class="main">{{ form.complaintName }}</div>
              </div>

              <div class="carInfo_item">
                <div class="label">手机号码</div>
                <div class="main">{{ form.phone }}</div>
              </div>

              <div class="carInfo_item">
                <div class="label">投诉时间</div>
                <div class="main">{{ form.createTime }}</div>
              </div>

              <div class="carInfo_item">
                <div class="label">处理状态</div>
                <div class="main">
                  <DictTag :options="com_complaint_status" :value="form.status"></DictTag>
                </div>
              </div>
            </div>
            <div class="carInfoBox signLe">
              <div class="carInfo_item">
                <div class="label">问题描述</div>
                <div class="main">{{ form.problemDes }}</div>
              </div>
            </div>
            <div class="carInfoBox signLe">
              <div class="carInfo_item">
                <div class="label">问题照片</div>
                <div class="main">
                  <el-image
                    v-for="(item, index) in problemImageUrlList"
                    :key="index"
                    :src="item"
                    :preview-teleported="true"
                    :preview-src-list="problemImageUrlList"
                    style="width: 120px; height: 120px; margin-right: 10px"
                  ></el-image>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </div>
      <!-- 处理情况 -->
      <div class="mb-10">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>处理情况</span>
            </div>
          </template>
          <div class="formBox">
            <el-form ref="formRef" :rules="rules" class="m-auto" :inline="false" label-width="100px" :model="form">
              <el-form-item label="处理状态" prop="status">
                <el-radio-group v-model="form.status">
                  <!-- <el-radio v-for="(item, index) in com_complaint_status" :key="index" :value="item.value">{{ item.label }}</el-radio> -->
                  <el-radio value="1">处理完成</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="处理人员" prop="handleId">
                <repairUser
                  :is-multiple="false"
                  :username="form.handleName"
                  :userid="form.handleId ?? ''"
                  returnvalue="*"
                  @change="handleChange"
                ></repairUser>
              </el-form-item>
              <el-form-item label="处理描述" prop="handleContent">
                <el-input type="textarea" :rows="5" v-model="form.handleContent"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="handleBack">返回</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import DictTag from '@/components/DictTag/index.vue';
import { ref } from 'vue';
import repairUser from '@/components/Holder/repairUser.vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { getComplaint, updateComplaint } from '@/api/system/Complaint';
import { listComplaintType } from '@/api/system/ComplainType';
import { ResidentVO } from '@/api/system/resident/type';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_complaint_status } = toRefs<any>(proxy?.useDict('com_complaint_status'));

const route = useRoute();
const router = useRouter();

const form = ref({
  'id': null,
  'type': '',
  'typeId': null,
  'problemDes': '',
  'complaintName': '',
  'phone': '',
  'problemImageUrl': '',
  'status': '1', // 投诉状态 0待处理 1已处理
  'handleId': '',
  'handleName': '',
  'handleContent': '',
  'createTime': null
});
const rules = {
  status: [{ required: true, message: '选择投诉处理状态', trigger: 'blur' }],
  handleContent: [{ required: true, message: '投诉处理内容不能为空', trigger: 'blur' }]
};
const userid = ref();
const problemImageUrlList = ref([]);
//  ! 投诉 ====================================================================================================
async function init() {
  const res2 = await listComplaintType();
  getComplaint(userid.value).then((res) => {
    form.value = {
      ...form.value,
      ...res.data
    };
    problemImageUrlList.value = res.data.problemImageUrl ? res.data.problemImageUrl.split(',') : [];
    form.value.status = '1';
    const find = res2.rows.find((item) => item.id === form.value.typeId);
    if (find) {
      form.value.type = find.name;
    }
  });
}

if (route.query.id) {
  userid.value = route.query.id;
  init();
}

function handleChange(pop: ResidentVO) {
  form.value.handleId = pop.userId;
  form.value.handleName = pop.nickName;
}

// 提交

const formRef = ref();
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (userid.value) {
        updateComplaint(form.value).then(() => {
          ElMessage.success('编辑成功');
          handleBack();
        });
      }
    }
  });
};

//  返回
function handleBack() {
  router.push({
    path: '/repair/complaint'
  });
}
</script>

<style scoped lang="scss">
.ResidentMainBox {
  display: flex;
  flex-direction: column;
  .ResidentMainBody {
    margin-top: 20px;
    flex: 1;
    font-size: 14px;

    .carInfoBox {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(2, 1fr);
      gap: 20px;
      margin-bottom: 20px;

      .carInfo_item {
        display: flex;
        align-items: center;
        .label {
          margin-right: 20px;
          min-width: 60px;
        }
      }

      &.signLe {
        grid-template-columns: repeat(1, 1fr);
        grid-template-rows: repeat(1, 1fr);
      }
    }

    .formBox {
      width: 500px;
      margin: 0 auto;
    }
    &:deep(div.el-step__head.is-process) {
      & > div.el-step__line {
        width: 0;
        border: 1px dashed gray;
        background: transparent;
      }
    }
    &:deep(.el-step.is-vertical .el-step__line) {
      top: 20px;
    }
    &:deep(div.el-step__head.is-finish) {
      & > div.el-step__line {
        width: 0;
        border: 1px solid var(--el-color-primary);
        background: transparent;
      }
    }
  }
}
</style>
