<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"
      @sort-change="sortChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column label="客户姓名" align="center" prop="customerName" width="100" />
      <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>
          <span v-if="scope.row.status == 'DRAFT'"
            ><span style="color: #86909c" class="iconfont icon-genjinjilu"></span> 草稿
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="流程编号"
        align="center"
        prop="fnaNo"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        label="预约编号"
        align="center"
        prop="appointmentNo"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column label="新单编号" align="center" prop="policyId" />
      <el-table-column label="保单号" align="center" prop="policyNo" width="150" />

      <el-table-column label="创建时间" sortable align="center" prop="createTime" width="200">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="250"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button type="primary" link @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="success" link @click="handleCopy(scope.row)">生成副本</el-button>
          <el-button v-if="!scope.row.policyNo" type="danger" link @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 { getFnaList, deleteFna, subProcess } 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 { csf_fna_status } = proxy.useDict('csf_fna_status')

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

const { queryParams, form, rules } = toRefs(data)
const handleCopy = row => {
  // { fnaBizId: row.fnaBizId }
  subProcess({ fnaBizId: row.fnaBizId }).then(response => {
    if (response.code === 200) {
      getList()
      proxy.$modal.msgSuccess('生成副本成功')
    }
  })
  // proxy.$modal
  //   .confirm('是否确认复用' + row.customerName + '的FNA流程？')
  //   .then(function () {
  //     return subProcess({ fnaBizId: row.fnaBizId })
  //   })
  //   .then(() => {
  //     getList()
  //     proxy.$modal.msgSuccess('生成副本成功')
  //   })
  //   .catch(() => {})
}
const sortChange = ({ prop, order }) => {
  if (order == 'ascending') {
    queryParams.value.sortOrder = 'ascend'
  } else {
    queryParams.value.sortOrder = 'descend'
  }
  if (prop) {
    queryParams.value.sortField = prop
  }
  getList()
}
/** 多选框选中数据 */
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 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', status: row.status }
  })
}

getList()
</script>
<style lang="scss" scoped>
.bottomBtn {
  width: 100%;
  text-align: right;
  margin-top: 20px;
}
</style>
