<template>
	<div class="business-discern-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
        @sort-change="sortChange"
			>
        <template #table-search>
          <el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent label-width="100px">
            <el-row>
              <el-col>
                <el-form-item label="快捷查询" prop="DataScope">
                  <el-segmented
                      :options="[
									{
										value: 0,
										label: '全部',
									},
									{
										value: 1,
										label: '我发起的',
									},
								]"
                      v-model="state.queryParams.DataScope"
                      @change="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                <el-form-item label="甄别状态" prop="Status">
                  <el-select v-model="state.queryParams.Status" placeholder="请选择甄别状态" @change="handleQuery" clearable>
                    <el-option v-for="item in state.screenStatus" :value="item.key" :key="item.key" :label="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                <el-form-item label="工单编码" prop="No">
                  <el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                <el-form-item label="工单标题" prop="Title">
                  <el-input v-model="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="省/市工单" prop="IsProvince">
                    <el-select v-model="state.queryParams.IsProvince" clearable placeholder="请选择省/市工单" @change="handleQuery">
                      <el-option  :value="false" label="市工单" />
                      <el-option  :value="true" label="省工单" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="受理类型" prop="AcceptType">
                    <el-select v-model="state.queryParams.AcceptType" placeholder="请选择受理类型" clearable class="w100" @change="handleQuery">
                      <el-option v-for="item in state.acceptTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="热点分类" prop="HotspotSpliceName">
                    <el-input v-model="state.queryParams.HotspotSpliceName" placeholder="热点分类名称" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="来源渠道" prop="SourceChannel">
                    <el-select v-model="state.queryParams.SourceChannel" placeholder="请选择来源渠道" clearable class="w100" @change="handleQuery">
                      <el-option v-for="item in state.sourceChannelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="被回访部门" prop="VisitOrgName">
                    <el-input v-model="state.queryParams.VisitOrgName" placeholder="被回访部门名称" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="申请部门" prop="CreatorOrgName">
                    <el-input v-model="state.queryParams.CreatorOrgName" placeholder="申请部门名称" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="申请人" prop="CreatorName">
                    <el-input v-model="state.queryParams.CreatorName" placeholder="申请人名称" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" v-show="!searchCol">
                  <el-form-item label="申请时间" prop="crTime">
                    <el-date-picker
                        v-model="state.queryParams.crTime"
                        type="datetimerange"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        :shortcuts="shortcuts"
                        @change="handleQuery"
                        value-format="YYYY-MM-DD[T]HH:mm:ss"
                        :default-time="defaultTimeStartEnd"
                    />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="联系电话" prop="Contact">
                    <el-input v-model="state.queryParams.Contact" placeholder="联系电话" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <transition name="el-zoom-in-top" v-show="!searchCol">
                <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                  <el-form-item label="来电号码" prop="FromPhone">
                    <el-input v-model="state.queryParams.FromPhone" placeholder="来电号码" clearable @keyup.enter="handleQuery" />
                  </el-form-item>
                </el-col>
              </transition>
              <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
                <el-form-item label=" ">
                  <div class="flex-end w100">
                    <el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
                    <el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
                      <SvgIcon name="ele-Refresh" class="mr5" />重置
                    </el-button>
                    <el-button link type="primary" @click="closeSearch" :loading="state.loading">
                      {{ searchCol ? '展开' : '收起' }}
                      <SvgIcon :class="{ 'is-reverse': searchCol }" name="ele-ArrowUp" class="mr5 arrow" size="18px" />
                    </el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="查看甄别详情"> 甄别详情 </el-button>
          <el-button link type="primary" @click="visitDetail(row)" title="查看回访明细">
            回访明细
          </el-button>
				</template>
			</ProTable>
		</div>
		<!-- 甄别详情 -->
		<discern-detail ref="discernDetailRef" @updateList="queryList" />
		<!-- 甄别修改 -->
		<discern-edit ref="discernEditRef" @updateList="queryList" />
    <!-- 回访详情 -->
    <visit-detail-com ref="visitDetailRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="businessDiscern">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { defaultTimeStartEnd, shortcuts } from "@/utils/constants";
import { screenBaseData, screenDetail, screenList } from '@/api/business/discern';
import Other from "@/utils/other";

// 引入组件
const DiscernDetail = defineAsyncComponent(() => import('@/views/business/discern/components/Discern-detail.vue')); // 甄别详情
const DiscernEdit = defineAsyncComponent(() => import('@/views/business/discern/components/Discern-edit.vue')); //  甄别修改
const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const VisitDetailCom = defineAsyncComponent(() => import('@/views/business/visit/component/Visit-detail.vue')); // 回访

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'statusText', label: '甄别状态', minWidth: 100 },
  { prop: 'workflow.actualHandleStepName', label: '当前审核节点', minWidth: 110 },
  { prop: 'order.no', label: '工单编码', minWidth: 140 },
	{ prop: 'order.isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'order.title', label: '工单标题', minWidth: 200 },
	{ prop: 'order.acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'order.hotspotName', label: '热点分类', minWidth: 150 },
  { prop: 'order.sourceChannel', label: '来源渠道', minWidth: 100 },
  { prop: 'visitDetail.visitOrgName', label: '被回访部门', minWidth: 140 },
  { prop: 'creatorName', label: '申请人', minWidth: 120 },
  { prop: 'creatorOrgName', label: '申请部门', minWidth: 140 },
	{
		prop: 'creationTime',
		label: '申请时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
  { prop: 'timeConsuminText', label: '发起甄别耗时(工作日)', minWidth: 160 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 160, align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		source: '2', // 甄别列表 2
    Status:null,// 甄别状态
    DataScope:0, // 快捷查询
    No:null, // 工单编码
    Title:null, // 工单标题
    IsProvince: null, // 省/市工单
    IsHomePage: null, // 是否首页进入
    AcceptType:null, // 受理类型
    HotspotSpliceName:null, // 热点分类
    SourceChannel:null, // 来源渠道
    VisitOrgName: null, // 被回访部门
    CreatorOrgName:null, // 申请部门
    CreatorName:null, // 申请人
    crTime:[],// 申请时间
    CreationTimeStart:null,
    CreationTimeEnd:null,
    SortField:null, // 排序字段
    SortRule:null, // 排序字段
    Contact:null, // 联系电话
    FromPhone:null, // 来电电话
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	screenStatus: [], // 甄别状态
	screenType: [], // 甄别类型
  acceptTypeOptions:[], // 受理类型
  sourceChannelOptions:[] , // 来源渠道
});
const searchCol = ref(true); // 展开/收起
// 展开/收起
const closeSearch = () => {
  searchCol.value = !searchCol.value;
};
// 获取查询条件基础信息
const getBaseData = async () => {
	const {result} = await screenBaseData();
  state.screenStatus = result?.screenStatus ?? [];
  state.screenType = result?.screenType ?? [];
  state.acceptTypeOptions = result?.acceptType ?? [];
  state.sourceChannelOptions = result?.sourceChannel ?? [];
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref({});
const queryList = () => {
  requestParams.value = Other.deepClone(state.queryParams);
  requestParams.value.CreationTimeStart = state.queryParams.crTime === null ? null : state.queryParams.crTime[0]; // 受理时间
  requestParams.value.CreationTimeEnd = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
  Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
	state.loading = true;
	screenList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 排序
const sortChange = (val: any) => {
  state.queryParams.SortField = val.order ? val.prop : null;
  // 0 升序 1 降序
  state.queryParams.SortRule = val.order ? (val.order == 'descending' ? 1 : 0) : null;
  queryList();
};
// 查看详情
const discernDetailRef = ref<RefType>(); // 甄别详情ref
const discernEditRef = ref<RefType>(); // 甄别修改ref
const onDetail = async (row: any) => {
	try {
		const { result } = await screenDetail(row.id);
		if (result.handle) {
			// 退回到了开始 需要重新打开编辑页面在发起流程
			discernEditRef.value.openDialog(row);
		} else {
			discernDetailRef.value.openDialog(row);
		}
	} catch (e) {
		console.log(e);
	}
};
// 查看回访明细
const visitDetailRef = ref<RefType>();
const visitDetail = (row:any)=>{
  visitDetailRef.value.openDialog(row.visit, '回访明细');
}
onMounted(async () => {
	await getBaseData();
	queryList();
});
</script>
<style scoped lang="scss">
.business-discern-container {
  .arrow {
    transition: transform var(--el-transition-duration);
    cursor: pointer;
  }
  .arrow.is-reverse {
    transform: rotateZ(-180deg);
  }
}
</style>
