<template>
    <div class="container">
        <header>
            <el-form :label-width="114">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="登记时间">
                            <date-time-range-picker-ext
                                v-model="query.discoveryTime"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="录入时间">
                            <date-time-range-picker-ext
                                v-model="query.createTime"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="信件编号">
                            <el-input
                                placeholder="请输入"
                                v-model="query.originId"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="信访人">
                            <div class="flex gap">
                                <el-select
                                    v-model="query.responderKey"
                                    style="width: 160px"
                                    @change="delete query.responderValue"
                                >
                                    <el-option value="name" label="姓名" />
                                    <el-option value="phone" label="电话" />
                                </el-select>
                                <el-input
                                    placeholder="请输入"
                                    v-model="query.responderValue"
                                    clearable
                                />
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="具体内容">
                            <el-input
                                placeholder="请输入"
                                v-model="query.thingDesc"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="被投诉机构">
                            <depart-tree-select v-model="query.departId" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="初重信访">
                            <el-select
                                clearable
                                v-model="query.initialPetition"
                            >
                                <el-option
                                    v-for="item in dict.initialPetition"
                                    :key="item.id"
                                    :label="item.dictLabel"
                                    :value="item.dictValue"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="状态">
                            <el-select
                                v-model="query.distributionState"
                                clearable
                            >
                                <el-option label="未分发" value="0"></el-option>
                                <el-option label="已分发" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                  <el-col :span="6">
                    <el-form-item label="涉企涉冻">
                      <el-select v-model="query.involvedIssue" clearable>
                        <el-option label="涉企问题" value="涉企问题" ></el-option>
                        <el-option label="涉冻问题" value="涉冻问题" ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
            </el-form>
            <div class="mb-25 flex between">
                <div>
                    <el-button type="primary" @click="show = true"
                        >数据导入</el-button
                    >
                  <el-button type="primary" @click="handleShowImportReplenish">集访导入</el-button>
                  <el-button type="primary" @click="handleShowldjfReplenish">领导接访导入</el-button>
                  <el-button type="primary" @click="handleInvolvedIssue" >涉企涉冻导入</el-button>
                  <el-button type="primary" @click="handleExport">数据导出</el-button>
                    <el-badge
                        :value="distributeList.length"
                        class="ml-10"
                        v-if="distributeList.length"
                    >
                        <el-button type="primary" @click="distributeShow = true"
                            >问题下发</el-button
                        >
                    </el-badge>
                </div>
                <div>
                    <el-button type="primary" @click="getList">
                        <template #icon>
                            <icon name="el-icon-Search" />
                        </template>
                        查询</el-button
                    >
                    <el-button @click="reset">重置</el-button>
                </div>
            </div>
        </header>
        <div class="table-container">
            <el-table :data="list">
                <el-table-column
                    label="信件编号"
                    prop="originId"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="投诉渠道"
                    prop="channelForFilingComplaints"
                    width="90"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="登记时间"
                    prop="discoveryTime"
                    show-overflow-tooltip
                    width="150"
                />
                <el-table-column
                    label="信访人"
                    prop="responderName"
                    width="90"
                    show-overflow-tooltip
                />
                <el-table-column label="电话" prop="responderPhone" width="116" />
              <el-table-column
                  label="涉企涉冻"
                  prop="involvedIssue"
                  width="100"
              ></el-table-column>
                <!-- <el-table-column label="身份证号码" prop="responderIdCode" width="180" /> -->
                <el-table-column label="初重信访" align="center" width="60">
                    <template #default="{ row }">
                        <span>{{
                            getDictLable(
                                dict.initialPetition,
                                row.initialPetition
                            )
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="群众集访" width="60" align="center">
                    <template #default="{ row }">
                        <span v-if="row.massVisits === true">是</span>
                        <span v-if="row.massVisits === false">否</span>
                    </template>
                </el-table-column>
                <el-table-column label="被投诉机构" show-overflow-tooltip>
                    <template #default="{ row }">
                        <span>{{ row.secondDepartName }}</span>
                        <span>{{ row.thirdDepartName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="具体内容"
                    prop="thingDesc"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="办理情况"
                    prop="petitionProcessingStatus"
                    width="90"
                />
                <el-table-column label="状态" width="100">
                    <template #default="{ row }">
                        <el-tag>{{
                            getDictLable(
                                dict.distributionState,
                                row.distributionState
                            )
                        }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200"  fixed="right">
                    <template #default="{ row }">
                        <template
                            v-if="
                                row.distributionState ===
                                DistributionState.UNDISTRIBUTED
                            "
                        >
                            <el-button
                                type="primary"
                                link
                                @click="handleAddDistribute(row)"
                                v-if="
                                    distributeList.filter(
                                        (item) => item.originId === row.originId
                                    ).length === 0
                                "
                                >加入问题下发</el-button
                            >
                            <el-button
                                type="info"
                                link
                                v-else
                                @click="handleRemoveDistribute(row)"
                                >移除</el-button
                            >
                        </template>
                        <el-button
                            v-if="
                                row.negativeId && row.distributionState === '1'
                            "
                            type="primary"
                            link
                            @click="handleAction(row)"
                            >问题详情
                        </el-button>
                        <el-button
                            type="primary"
                            link
                            @click="handleDetail(row)"
                            >详情</el-button
                        >
                        <el-button type="danger" link @click="handleDel(row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="flex end mt-8">
            <el-pagination
                @size-change="getList"
                @current-change="getList"
                :page-sizes="[10, 20, 50]"
                v-model:page-size="query.size"
                v-model:current-page="query.current"
                layout="total, sizes, prev, pager, next"
                :total="total"
            >
            </el-pagination>
        </div>
    </div>

    <data-gj-import v-model="show" @close="show = false" @update="getList" />
  <!--   集访、领导接访数据补充-->
  <data-gj-replenish v-model="replenishShow"
                      :replenishType="replenishType"
                      @close="replenishShow = false" @success="successFun"/>
  <data-gj-replenish-lead
      v-model="replenishShow2"
      :replenishType="replenishType2"
      @close="replenishShow2 = false" @success="successFun"
  />


    <data-distrbute
        v-model:show="distributeShow"
        v-model:data="distributeList"
        @update="getList"
    />

    <negative-dialog
        v-model="negativeShow"
        :id="activeNegativeId"
        @close="negativeShow = false"
    />

    <el-dialog title="详情" v-model="detailShow" width="60vw" :lock-scroll="false">
        <div style="min-height: 50vh">
            <div class="row" style="margin: 0 60px">
                <div class="col col-12">
                    <label>信件编号</label>
                    <span>{{ activeRow.originId }}</span>
                </div>
                <div class="col col-12">
                    <label>投诉渠道</label>
                    <span>{{
                        activeRow.channelForFilingComplaints || "/"
                    }}</span>
                </div>
                <div class="col col-12">
                    <label>信访方式</label>
                    <span>{{
                        activeRow.petitionType || "/"
                    }}</span>
                </div>
                <div class="col col-12">
                    <label>登记时间</label>
                    <span>{{ activeRow.discoveryTime }}</span>
                </div>
                <div class="col col-12">
                    <label>信访人</label>
                    <span>{{ activeRow.responderName }}</span>
                </div>
                <div class="col col-12">
                    <label>信访人联系方式</label>
                    <span>{{ activeRow.responderPhone }}</span>
                </div>
                <div class="col col-12">
                    <label>信访人身份证</label>
                    <span>{{ activeRow.responderIdCode || '/' }}</span>
                </div>
                <div class="col col-12">
                    <label>初重信访</label>
                    <span>{{
                        getDictLable(
                            dict.initialPetition,
                            activeRow.initialPetition
                        ) || "/"
                    }}</span>
                </div>
                <div class="col col-12">
                    <label>群众集访</label>
                    <span
                        ><span v-if="activeRow.massVisits === true">是</span>
                        <span v-else-if="activeRow.massVisits === false"
                            >否</span
                        >
                        <span v-else
                            >/</span
                        >
                        </span
                    >
                </div>
                <div class="col col-12">
                    <label>被投诉机构</label>
                    <span
                        ><span>{{ activeRow.secondDepartName }}</span>
                        <span>{{ activeRow.thirdDepartName }}</span></span
                    >
                </div>
                <div class="col col-12">
                    <label>办理情况</label>
                    <span>{{ activeRow.petitionProcessingStatus || '/' }}</span>
                </div>
                <div class="col col-12">
                    <label>接访领导</label>
                    <span>{{ activeRow.receivingLeaderName || '/' }}</span>
                </div>
                <div class="col col-12">
                    <label>化解情况</label>
                    <span>{{ '/' }}</span>
                </div>
                <div class="col col-12">
                    <label>当前状态</label>
                    <span>{{ '/' }}</span>
                </div>
                <div class="col col-24">
                    <label>具体内容</label>
                    <span class="content">{{ activeRow.thingDesc }}</span>
                </div>
            </div>
        </div>
    </el-dialog>

    <el-dialog title="涉及问题导入" v-model="involvedIssueShow" width="60vw" :lock-scroll="false">
      <div v-loading="involvedIssueLoading">
        <el-upload
            drag
            :multiple="false"
            :auto-upload="false"
            :show-file-list="false"
            v-model:file-list="fileList"
            accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            class="mt-20"
        >
          <template v-if="fileList.length == 0">
            <el-icon class="el-icon--upload">
              <upload-filled/>
            </el-icon>
            <div class="el-upload__text">
              <p>点击或拖拽文件到此区域上传</p>
            </div>
          </template>
          <template v-else>
            <el-icon class="el-icon--upload"
            ><Select
            /></el-icon>
            <div class="el-upload__text">
              已选择文件：{{
                fileList[fileList.length - 1].name
              }}
            </div>
          </template>
        </el-upload>
        <div class="mt-20">
          <span>文件模板</span>
          <a
              class="link"
              :href="`${BASE_PATH}/templates/涉企涉冻导入模板.xlsx`"
              target="__blank"
          >涉企涉冻导入模板.xlsx 下载</a
          >
        </div>
        <footer class="flex end mt-20 v-center">
          <el-button  type="primary"
                      size="large"
                      @click="handleInvolvedIssueNext"
                      :disabled="involvedIssueLoading">
            确认
          </el-button>
        </footer>
      </div>
    </el-dialog>
</template>
<script setup>
import {
    listPetitionComplaint,
    delPetitionComplaint,
    replenishInvolvedIssueFun
} from "@/api/data/petitionComplaint";
import { ProblemSources, DistributionState } from "@/enums/dictEnums";
import feedback from "@/utils/feedback";
import { getDictLable ,getYearTime} from "@/utils/util";
import useCatchStore from "@/stores/modules/catch";
import {BASE_PATH} from "@/api/request";

const catchStore = useCatchStore();
const replenishShow = ref(false)
const replenishShow2 =ref(false)
const replenishType =ref("1")
const replenishType2 =ref("2")
const dict = catchStore.getDicts(["distributionState", "initialPetition"]);
const route = useRoute()
const query = ref({
    size: 10,
    current: 1,
    responderKey: "name",
    problemSourcesCode: ProblemSources.GJXFPT,
});
const involvedIssueShow = ref(false)
const involvedIssueLoading = ref(false)
const fileList= ref([])

const list = ref([]);
const total = ref(0);
function getList() {

    listPetitionComplaint(query.value).then((data) => {
        list.value = data.records;
        total.value = data.total;
    });
}
/**
 * 集访数据导入
 * */
function handleShowImportReplenish(){
  replenishType.value="1"
  replenishShow.value=true
}
/**
 * 领导接访数据导入
 * */
function handleShowldjfReplenish(){
  replenishType2.value='2'
  replenishShow2.value=true
}
/**
 * 涉及问题导入
 * */
function  handleInvolvedIssue(){
  involvedIssueShow.value=true;
}
const handleInvolvedIssueNext =async ()=>{
  const formData = new FormData();
  formData.append("file", fileList.value[fileList.value.length - 1].raw);
  involvedIssueLoading.value = true;
  try {
    await replenishInvolvedIssueFun(formData);
    feedback.msgSuccess("操作成功");
    involvedIssueShow.value=false;
    involvedIssueLoading.value=false;
  } catch (e) {
    involvedIssueLoading.value = false;
    involvedIssueShow.value=false;
    throw e;
  }
}

/**
 * 导出数据
 * */
function handleExport() {
  window.open(`${BASE_PATH}/data/petitionComplaint/export/xf/gjxf?` + new URLSearchParams(query.value).toString())
}
//检测跳转来源界面是否为大屏
watch(()=>route.query.toString(),
    ()=>{
      if(route.query.open){
        query.value.createTime=getYearTime()
      }
      getList()
    },{immediate:true,deep:true})


function reset() {
    query.value = {
        size: 10,
        current: 1,
        responderKey: "name",
        problemSourcesCode: ProblemSources.GJXFPT,
    };
    getList();
}

getList();

/**
 * 补充成功的事件
 * 补充的数据：初重访、群众集访
 * */
function successFun(){

  replenishShow.value=false
  getList()
}

const show = ref(false);
async function handleDel(row) {
    await feedback.confirm("确定要删除该数据？");
    await delPetitionComplaint(row.originId);
    getList();
}

const distributeList = ref([]);
const distributeShow = ref(false);

function handleAddDistribute(row) {
    distributeList.value.push(row);
}

function handleRemoveDistribute(row) {
    distributeList.value.splice(
        distributeList.value.findIndex(
            (item) => item.originId === row.originId
        ),
        1
    );
}

const activeNegativeId = ref("");
const negativeShow = ref(false);
function handleAction(row) {
    negativeShow.value = true;
    activeNegativeId.value = row.negativeId;
}

const detailShow = ref(false);
const activeRow = ref({});

function handleDetail(row) {
    activeRow.value = row;
    detailShow.value = true;
}
</script>
<style lang="scss" scoped>
</style>
