<template>
  <div class="app-container">
    <!-- 步骤条 -->
    <el-row style="margin-bottom: 30px">
      <el-col :span="24">
        <Step :stepList="stepList"></Step>
      </el-col>
    </el-row>
    <!-- 条件查询 -->
    <el-row>
      <el-col :span="24">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="70px"
        >
          <el-form-item
            ><el-button type="primary" icon="Plus" @click="handleAdd"
              >新建流程</el-button
            ></el-form-item
          >

          <el-form-item label="创建时间" style="width: 300px">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="流程编号" prop="fnaNo">
            <el-input
              v-model="queryParams.fnaNo"
              placeholder="请输入流程编号"
              clearable
              style="width: 150px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="客户姓名" prop="customerName">
            <el-input
              v-model="queryParams.customerName"
              placeholder="请输入姓名"
              clearable
              style="width: 140px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择"
              clearable
              style="width: 110px"
            >
              <el-option
                v-for="dict in csf_fna_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tenantList"
      @selection-change="tableSelect"
      :default-sort="{ prop: 'createTime', order: 'descending' }"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="流程编号" align="center" prop="fnaNo" width="200" />
      <el-table-column label="预约编号" align="center" prop="appointmentNo" width="150" />
      <el-table-column label="新单编号" align="center" prop="policyId" />
      <el-table-column label="保单号" align="center" prop="policyNo" />

      <el-table-column label="客户姓名" align="center" prop="customerName" />

      <el-table-column label="状态" align="center" width="150">
        <template #default="scope">
          <span v-if="scope.row.status == 'UNCOMPLETED'">
            <span style="color: #ff7d00" class="iconfont icon-yanqiweiwancheng"></span> 未完成
          </span>
          <span v-if="scope.row.status == 'COMPLETED'"
            ><span style="color: #43cf7c" class="iconfont icon-yiwancheng"></span> 已完成
          </span>
        </template>
      </el-table-column>
      <!-- sortable 后端未做 -->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <!-- v-if="scope.row.status == 'COMPLETED'" -->
          <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total >= 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 下一步到预约 -->
    <div class="bottomBtn">
      <el-button type="primary" @click="goToAppointment">下一步</el-button>
    </div>
  </div>
</template>

<script setup name="FnaList">
import Step from '@/views/components/moduleStep'
import useAppStore from '@/store/modules/app'
import {
  changeUserStatus,
  listTenant,
  resetUserPwd,
  delUser,
  getTenant,
  updateTenant,
  addTenant,
  changeTenantStatus
} from '@/api/system/tenant'
import { getFnaList, deleteFna } from '@/api/sign/fna'
import useUserStore from '@/store/modules/user'

const stepList = ref([
  { title: 'FNA', id: 1, finish: false, todo: true, unFinish: false },
  { title: '预约', id: 2, todo: false, finish: false, unFinish: true },
  { title: '新单跟进', id: 3, finish: false, todo: false, unFinish: true }
])
const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()
const { sys_status, csf_fna_status, sys_no_yes } = proxy.useDict(
  'sys_status',
  'csf_fna_status',
  'sys_no_yes'
)

const tenantList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])
const selectIdsList = ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 8,
    startTime: '',
    endTime: ''
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 多选框选中数据 */
function tableSelect(selection) {
  selectIdsList.value = selection.map(item => item.projectBizId)
}
const goToAppointment = () => {
  router.push({ path: '/sign/appointment' })
}
/** 查询租户列表 */
function getList() {
  if (dateRange.value.length > 0) {
    queryParams.value.startTime = `${dateRange.value[0]} 00:00:00`
    queryParams.value.endTime = `${dateRange.value[1]} 23:59:59`
  }
  loading.value = true
  try {
    getFnaList(queryParams.value).then(response => {
      tenantList.value = response.data.records
      total.value = response.data.total
      loading.value = false
    })
  } catch (error) {
    tenantList.value = []
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  queryParams.value = {
    pageNo: 1,
    pageSize: 8,
    startTime: '',
    endTime: ''
  }
  handleQuery()
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除' + row.customerName + '的FNA流程？')
    .then(function () {
      return deleteFna({ fnaBizId: row.fnaBizId })
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 租户状态修改  */
function handleStatusChange(row, event) {
  let text = row.status === 0 ? '停用' : '启用'
  const tenantName = row.tenantName
  proxy.$modal
    .confirm(`确认要${text}"${tenantName}"租户吗?`)
    .then(() => changeTenantStatus({ tenantBizId: row.tenantBizId, status: row.status }))
    .then(() => proxy.$modal.msgSuccess(`${text}成功`))
    .catch(() => {
      // 操作取消时恢复原状态
      row.status = row.status === 0 ? 1 : 0
    })
}

/** 重置操作表单 */
function reset() {
  form.value = {
    pageNo: 1,
    pageSize: 8
  }
  proxy.resetForm('tenantRef')
}

/** 新增按钮操作 */
function handleAdd() {
  router.push('/sign/FnaList/edit?type=add')
}

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({ path: '/sign/FnaList/edit', query: { fnaBizId: row.fnaBizId, type: 'edit' } })
}

getList()
</script>
<style lang="scss" scoped>
/* ::v-deep .el-step__head.is-success {
  color: none !important;
  background-color: #e8f3ff !important;
  border: none !important;
} */
/* ::v-deep .el-step__icon {
 background-color: #e8f3ff !important;
} */
.bottomBtn {
  width: 100%;
  text-align: right;
  margin-top: 20px;
}
</style>
