<!-- 计量计划-计划列表 -->
<script lang="ts" setup name="PlanTable">
import { ElMessage, ElMessageBox } from 'element-plus'
import selectPlan from './selelectPlan.vue'
import selectDevice from '@/views/tested/MeasurementPlan/plan/components/selectDevice.vue'
import selectDeviceSinge from '@/views/tested/device/group/components/selectDevice.vue'
import { useCheckList } from '@/utils/useCheckList'
const $props = defineProps({
  data: {
    type: Array,
    default: () => ([]),
  },
  status: {
    type: String,
    required: true,
  },
})
const $route = useRoute()
const dialogStatus = ref('update') // 对话框类型：create,update
// 表头显示标题
const columns = ref([
  {
    text: '统一编号',
    value: 'equipmentNo',
    required: true,
    isDate: false, // 是否下拉框
    isBtn: true,
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    required: true,
    isDate: false, // 是否下拉框
    isBtn: false,
  },
  {
    text: '规格型号',
    value: 'model',
    required: false,
    isDate: false, // 是否下拉框
    isBtn: false,
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    required: false,
    isDate: false, // 是否下拉框
    isBtn: false,
  },
  {
    text: '负责人',
    value: 'directorName',
    required: false,
    isDate: false, // 是否下拉框
    isBtn: false,
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    required: false,
    isDate: true, // 是否下拉框
    isBtn: false,
  },
  {
    text: $route.query.approvalType === '0' ? '提前送检时间' : '延迟送检时间',
    value: 'inspectionTime',
    required: true,
    isDate: true, // 是否下拉框
    isBtn: false,
  },
  // {
  //   text: '关联计划',
  //   value: 'planName',
  //   required: true,
  //   isDate: false, // 是否下拉框
  //   isBtn: true,
  // },
  {
    text: '备注',
    value: 'remarks',
    required: false,
    isSelect: false, // 是否下拉框
    isBtn: false,
  },
])
const list = ref<any[]>([])
// 检查数据列表
function checkCertificateList() {
  return useCheckList(list.value, columns.value, '设备信息')
}
// 将列表置为不可编辑状态
function setAllRowReadable() {
  for (const item of list.value) {
    item.editable = false
  }
}
// 双击行显示输入框
const dblclickRow = (row: any) => {
  if ($route.path.includes('detail')) {
    return
  }
  setAllRowReadable()
  row.editable = true
}
const SelectionList = ref()
// 表格选中
const handleSelectionChange = (e: any[]) => {
  SelectionList.value = e
}
// 添加行
const addRow = () => {
  // certificateRef.value.initDialog({ title: '添加' })
  if (checkCertificateList()) {
    setAllRowReadable()
    list.value.push({
      equipmentNo: '',
      equipmentName: '',
      model: '',
      directorName: '',
      meterIdentify: '',
      certificateValid: '',
      installLocation: '',
      editable: true,
    })
  }
}
// 删除行
const removeRow = () => {
  list.value = list.value.filter((item) => {
    return !SelectionList.value.includes(item)
  })
}
// 点击设备名称输入框
const deviceRef = ref()
const deviceSingeRef = ref()
const planRef = ref()
// 选中的行
const selectRow = ref()
const select = (text: string, index: any) => {
  if (text === '统一编号') {
    deviceSingeRef.value.initDialog()
    selectRow.value = index
  }
  if (text === '关联计划') {
    planRef.value.initDialog()
    selectRow.value = index
  }
}
// 选择设备
const confirm = (device: any) => {
  // 判断是单选还是多选
  if (Array.isArray(device)) {
    // 多选
    device.forEach((item) => {
      list.value.push({
        ...item,
        equipmentId: item.id,
        id: null,
        groupId: null,
        editable: true,
        planId: '',
        planName: '',
        planNo: '',
      })
    })
  }
  else {
    // 单选
    // 判断是否重复
    const row = {
      ...device,
      equipmentId: device.id,
      id: null,
      groupId: null,
      editable: true,
      planId: '',
      planName: '',
      planNo: '',
    }
    list.value[selectRow.value] = row
  }
}
// 选择计量计划
const confirmPlan = (plan: any) => {
  list.value[selectRow.value].planNo = plan.planNo
  list.value[selectRow.value].planName = plan.planName
  list.value[selectRow.value].planId = plan.id
}
const initDialog = () => {
  list.value = $props.data
  dialogStatus.value = $props.status
}
// 批量增加
const batch = () => {
  deviceRef.value.initDialog(true)
}
watch(() => $props.data, (newVal) => {
  if (newVal) {
    list.value = newVal
  }
}, {
  deep: true,
  // immediate: true,
})
initDialog()
defineExpose({
  list, checkCertificateList,
})
</script>

<template>
  <detail-block-switch :title="`${$route.query.approvalType === '0' ? '提前' : '延迟'}送检列表`">
    <select-device ref="deviceRef" @add="confirm" />
    <select-device-singe ref="deviceSingeRef" @add="confirm" />
    <select-plan ref="planRef" @add="confirmPlan" />
    <template v-if="!$route.path.includes('detail')" #btns>
      <el-button type="primary" @click="batch">
        批量增加
      </el-button>
      <el-button type="primary" @click="addRow">
        增加行
      </el-button>
      <el-button type="info" @click="removeRow">
        删除行
      </el-button>
    </template>
    <el-table
      ref="multipleTableRef" :data="list" style="width: 100%;" border @selection-change="handleSelectionChange"
      @row-dblclick="dblclickRow"
    >
      <el-table-column v-if="!$route.path.includes('detail')" type="selection" width="38" />
      <el-table-column align="center" label="序号" width="80" type="index" />
      <el-table-column
        v-for="item in columns" :key="item.value" :prop="item.value" :label="item.text"
        align="center"
      >
        <template #header>
          <span v-show="item.required && !$route.path.includes('detail')" style="color: red;">*</span><span>{{ item.text }}</span>
        </template>
        <template #default="scope">
          <span v-if="!scope.row.editable">{{ scope.row[item.value] }}</span>
          <el-input
            v-if="scope.row.editable && !item.isDate && !item.isBtn" v-model="scope.row[item.value]" :autofocus="true"
            :placeholder="`${item.text}`" class="input"
            @focus="select(item.text, scope.$index)"
          />
          <el-input
            v-if="scope.row.editable && !item.isDate && item.isBtn" v-model="scope.row[item.value]" :autofocus="true"
            :placeholder="`${item.text}`" class="input"
            @focus="select(item.text, scope.$index)"
          >
            <template #append>
              <span @click="select(item.text, scope.$index)">选择</span>
            </template>
          </el-input>
          <el-date-picker
            v-if="scope.row.editable && item.isDate && !item.isBtn" v-model="scope.row[item.value]" type="date"
            format="YYYY-MM-DD" value-format="YYYY-MM-DD" :placeholder="`${item.text}`"
          />
        </template>
      </el-table-column>
    </el-table>
  </detail-block-switch>
</template>

<style lang="scss" scoped>
.el-dialog {
  width: 700px;
}

.el-select {
  width: 100%;
}
</style>
