<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="主客户号" prop="customerNo" label-width="100px">
              <el-input v-model="queryParams.customerNo" placeholder="请输主客户号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="子客户号" prop="subCustomerNo" label-width="100px">
              <el-input v-model="queryParams.subCustomerNo" placeholder="请输入子客户号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="name" label-width="100px">
              <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="证件类型" prop="identificationType" label-width="100px">
              <el-select v-model="queryParams.identificationType" clearable placeholder="请输入证件类型">
                <el-option v-for="dict in sys_id_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="证件号码" prop="identificationNumber" label-width="100px">
              <el-input v-model="queryParams.identificationNumber" placeholder="请输入证件号码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="变更日期" clearable label-width="100px" style="width: 343px">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 0, 0, 0)]"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="变更来源" prop="fromSystem" label-width="100px">
              <el-select v-model="queryParams.fromSystem" clearable placeholder="请输入变更来源">
                <el-option
                  v-for="dict in sys_from_system.filter((item) => item.value !== 'CMS' && item.value !== 'Shared')"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="处理状态" prop="status" label-width="100px">
              <el-select v-model="queryParams.status" clearable placeholder="请输入处理状态">
                <el-option v-for="dict in cus_change_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-row>
              <el-form-item>
                <el-button type="primary" icon="Search" style="margin-left: 40px" @click="handleQuery">查询</el-button>
                <el-button icon="Refresh" style="margin-left: 50px" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="customerChangeList">
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column label="发起系统" align="center" prop="fromSystem">
          <template #default="scope">
            <dict-tag :options="sys_from_system" :value="scope.row.fromSystem" />
          </template>
        </el-table-column>
        <el-table-column label="发起时间" align="center" prop="operationDate" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.operationDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="主客户号" align="center" prop="customerNo" width="150" />
        <el-table-column label="发起子客户号" align="center" prop="subCustomerNo" width="150" />
        <el-table-column label="姓名" align="center" prop="newName" />
        <el-table-column label="性别" align="center" prop="newGender">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.newGender" />
          </template>
        </el-table-column>
        <el-table-column label="出生日期" align="center" prop="newBirthday" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.newBirthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证件类型" align="center" prop="newIdType">
          <template #default="scope">
            <dict-tag :options="sys_id_type" :value="scope.row.newIdType" />
          </template>
        </el-table-column>
        <el-table-column label="证件号码" align="center" prop="newIdNo" width="160" show-overflow-tooltip />
        <el-table-column label="待修改系统" align="center" prop="pendingSystem" width="160">
          <template #default="scope">
            <dict-tag :options="sys_from_system" :value="scope.row.pendingSystem" />
          </template>
        </el-table-column>
        <el-table-column label="变更内容" align="center" prop="changeMsg" show-overflow-tooltip />
        <el-table-column label="处理状态" align="center" prop="changeStatus">
          <template #default="scope">
            <dict-tag :options="cus_change_status" :value="scope.row.changeStatus" />
          </template>
        </el-table-column>
        <el-table-column label="完成时间" align="center" prop="completionDate" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.completionDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改人" align="center" prop="fromOperate" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
      <el-row style="margin-top: 20px">
        <el-button v-hasPermi="['customer:customerChange:export']" type="warning" plain icon="Download" @click="handleExport">下 载</el-button>
      </el-row>
    </el-card>
    <view-dialog ref="viewDialogRef" :detail-visible="showDialog" :change-id="changeId" @update:detail-visible="updateDetailVisible" />
  </div>
</template>
<script setup="CustomerChangeInventory" lang="ts">
import { listChangeInventory } from '@/api/customer/customerChange';
import { parseTime } from '../../../utils/ruoyi';
import ViewDialog from '@/views/customer/customerChange/viewDialog.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_from_system } = toRefs<any>(proxy?.useDict('sys_from_system'));
const { sys_id_type } = toRefs<any>(proxy?.useDict('sys_id_type'));
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));
const { cus_change_status, merge_status } = toRefs<any>(proxy?.useDict('cus_change_status', 'merge_status'));
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const customerChangeList = ref([]);
const changeId = ref('');
const showDialog = ref(false);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerNo: '',
    subCustomerNo: '',
    name: '',
    dictType: '',
    identificationType: '',
    identificationNumber: '',
    fromSystem: '',
    status: ''
  },
  rules: {}
});
const { queryParams, rules } = toRefs(data);

/** 查询按钮 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  customerChangeList.value = '';
  total.value = 0;
};

/** 查询变更清单列表 */
const getList = () => {
  loading.value = true;
  listChangeInventory(proxy?.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    customerChangeList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'customer/customerChangeInventory/export',
    {
      ...queryParams.value
    },
    `客户重要资料变更追踪清单_${new Date().toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
      fractionalSecondDigits: 3,
      hour12: false
    })}.xlsx`
  );
};

/**
 * 查看合并轨迹
 */
const handleView = (row) => {
  changeId.value = row.changeId;
  showDialog.value = true;
};

/**
 * 关闭详情弹窗
 */
const updateDetailVisible = (val) => {
  if (!val) {
    showDialog.value = false;
    changeId.value = '';
  }
};

// onMounted(() => {
//   getList();
// });
</script>

<style scoped lang="scss"></style>
