<template>
  <div>
    <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="fnaNo" width="200" />
      <el-table-column label="预约编号" align="center" prop="appointmentNo" width="200" />
      <el-table-column label="新单编号" align="center" prop="policyId" width="150" />
      <el-table-column label="保单号" align="center" prop="policyNo" width="150" />

      <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="创建时间" 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="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link @click="handleLian">重新关联</el-button>
          <el-button type="danger" link @click="handleUnlink(scope.row)">解除关联</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="bottomBtn">
      <el-button type="primary" @click="handleLian">关联FNA</el-button>
    </div>
    <el-dialog title="FNA列表" v-model="dialogVisible" width="70%">
      <!-- 条件查询 -->
      <el-row>
        <el-col :span="24">
          <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
            label-width="70px"
          >
            <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>
              <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="fnaList"
        @selection-change="tableSelect"
        @sort-change="sortChange"
        @row-click="rowClick"
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column type="index" width="50" />
        <el-table-column label="流程编号" align="center" prop="fnaNo" width="200" />
        <el-table-column label="预约编号" align="center" prop="appointmentNo" />
        <el-table-column label="新单编号" align="center" prop="policyId" />
        <el-table-column label="保单号" align="center" prop="policyNo" width="150" />

        <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="创建时间" sortable align="center" prop="createTime" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </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"
      />
    </el-dialog>
  </div>
</template>

<script setup name="FnaList">
import { watch } from 'vue'
import { getFnaList, deleteFna, subProcess } from '@/api/sign/fna'
import { editFna, unlinkFna } from '@/api/sign/appointment'
import useUserStore from '@/store/modules/user'
const emit = defineEmits(['handleSuccessEdit'])
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 props = defineProps({
  activeName: { type: String, default: '' }, //tab名称
  formStatus: { type: String, default: '' }, //父组件状态，新增、修改
  idsObj: { type: Object, default: () => ({}) } //父组件传递过来的id对象
})

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

const { queryParams, form, rules } = toRefs(data)
const handleLian = () => {
  dialogVisible.value = true
  queryParams.value = {
    pageNo: 1,
    pageSize: 5,
    startTime: '',
    endTime: '',
    sortField: '',
    sortOrder: ''
  }
  getList()
}

const rowClick = row => {
  let data = {
    fnaBizId: row.fnaBizId,
    appointmentBizId: props.idsObj.appointmentBizId,
    fnaNo: row.fnaNo
  }
  editFna(data).then(response => {
    if (response.code === 200) {
      queryParams.value.fnaBizId = row.fnaBizId
      queryParams.value.pageNo = 1
      getList()
      emit('handleSuccessEdit')
      dialogVisible.value = false
    }
  })
}
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)
}

/** 查询租户列表 */
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 => {
      if (dialogVisible.value) {
        total.value = response.data.total
        fnaList.value = response.data.records
      } else {
        tenantList.value = response.data.records
      }

      loading.value = false
    })
  } catch (error) {
    tenantList.value = []
    fnaList.value = []
  } finally {
    loading.value = false
  }
}

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

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  queryParams.value = {
    pageNo: 1,
    pageSize: 5,
    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' } })
}
/** 查看按钮操作 */
function handleView(row) {
  router.push({ path: '/sign/FnaList/edit', query: { fnaBizId: row.fnaBizId, type: 'edit' } })
}
/** 解除关联按钮操作 */
function handleUnlink(row) {
  let data = {
    appointmentBizId: props.idsObj.appointmentBizId
  }
  proxy.$modal
    .confirm('是否确认解除关联？')
    .then(function () {
      return unlinkFna(data)
    })
    .then(response => {
      if (response.code === 200) {
        tenantList.value = []
        emit('handleSuccessEdit')
      }
      proxy.$modal.msgSuccess('已解除关联')
    })
    .catch(() => {})
}

watch(
  () => props.activeName,
  newVal => {
    if (newVal === 'fna') {
      queryParams.value.fnaBizId = props.idsObj.fnaBizId
      getList()
    }
  }
)
</script>
<style lang="scss" scoped>
.bottomBtn {
  width: 100%;
  text-align: center;
  margin-top: 20px;
}
</style>
