<template>
  <el-dialog v-model.trim="addShow" :title="title" width="1200px" draggable :close-on-click-modal="false">
    <el-form :model="form" ref="ruleFormRef" :inline="true" label-width="106px" :rules="formRules"
      style="padding: 0 0px 0px 0;">
      <el-form-item label="企业名称：" prop="enterprise">
        {{ form.companyName }}
      </el-form-item>
      <el-form-item label="租赁范围：" prop="isEnable">
        {{ form.leaseScope }}
      </el-form-item>
      <el-form-item label="出场时间：" prop="enterprise">
        {{ form.productionDate }}
      </el-form-item>

      <el-form-item label="装备名称：" prop="enterprise">
        {{ form.devName }}
      </el-form-item>
      <el-form-item label="装备编号：" prop="isEnable">
        {{ form.code }}
      </el-form-item>
      <el-form-item label="装备类别：" prop="enterprise">
        {{ form.parentDevType }}
      </el-form-item>
      <el-form-item label="品牌：" prop="isEnable">
        {{ form.brand }}
      </el-form-item>
      <el-form-item label="装备规格：" prop="enterprise">
        {{ form.companyName }}
      </el-form-item>
      <el-form-item label="装备位置：" prop="isEnable">
        {{ form.specification }}
      </el-form-item>
      <el-form-item label="装备组别：" prop="enterprise">
        {{ form.devTypeName }}
      </el-form-item>
      <el-form-item label="租金：" prop="isEnable">
        {{ form.monthLeasePrice }}
      </el-form-item>
      <el-form-item label="是否配备机手：" prop="enterprise">
        {{ form.isOperator }}
      </el-form-item>
      <el-form-item label="检验信息：" prop="isEnable">
        <el-button type="text" @click="attachFn(form.inspectImageUrl)">{{ form.inspectImageName }}</el-button>
      </el-form-item>
      <el-form-item label="保险信息：" prop="enterprise">
        <el-button type="text" @click="attachFn(form.insureImageUrl)">{{ form.insureImageName }}</el-button>
      </el-form-item>
      <el-form-item label="工作时长：" prop="enterprise">
        {{ form.workingHours }}
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer" v-if="isAudit">
        <el-button type="primary" @click="approveFn">通过</el-button>
        <el-button type="danger" @click="rejectFn">
          驳回
        </el-button>
      </span>
      <span class="dialog-footer" v-else>
        <el-button type="primary" @click="addShow = false">关闭</el-button>

      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>

import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

import { apiSelectInfo, apiOff } from "http/api/equip"

import { ElMessageBoxOpert } from 'utils/elementCom'
const emits = defineEmits(['send'])
const addShow = ref(false)
let title = ref("审批详情")
const ruleFormRef: any = ref()
const isAudit = ref(false)
const form: any = reactive({

})
// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  enterprise: [
    { required: true, message: '请输入企业类型', trigger: 'blur' },
  ],


})

const rejectCallBack = async (row: any, value: any) => {
  console.log('editCallBack', row, value)
  const params = [{
      maId: form.maId,
      id: form.creator,
      applyCompany: form.companyName,
      type: "2",
      status: '2'
    }
    ]

  const res = await apiOff(params)
  console.log("apiOff", res)
  ElMessage({
    type: 'success',
    message: "驳回成功"
  })
  addShow.value=false
  emits("send")
}



const rejectFn = () => {
  ElMessageBoxOpert(
    '驳回原因',
    '填写驳回原因',
    '确定',
    '取消',
    (val: any) => {
      if (!val) {
        return '请填写类型模板'
      }
    },
    '请填写类型模板',
    rejectCallBack,
    "row",
    'textarea',
    'dangerC'
  )
}

const approveFn = async () => {
  const params =  [
      {
        maId: form.maId,
        id: form.creator,
        applyCompany: form.companyName,
        type: "1",
        status: '1'
      }
    ]

 
  const res = await apiOff(params)
  console.log("apiOff", res)
  ElMessage({
    type: 'success',
    message: "审批成功"
  })
  addShow.value=false
  emits("send")
}

const initData = async (val: any) => {
  const params = {
    ids: val.maId
  }
  const res: any = await apiSelectInfo(params)
  console.log("res=apiSelectInfo", res)
  Object.assign(form, res.data[0])
}

const open = (val: any) => {
  console.log("openopenopen", val)
  addShow.value = true
  isAudit.value = false
  initData(val)
}

const edit = (val: any) => {
  isAudit.value = true
  addShow.value = true
  initData(val)
}
const attachFn = (url: any) => {
  if (!url) {
    return
  }
  window.open(url, "_blank")
}
defineExpose({
  open,
  edit
})

</script>

<style scoped lang="scss">
:deep(.el-form-item__content) {
  width: 246px;
  white-space: wrap;
}
</style>
<style>
.dangerC {
  background-color: #F56C6C !important;
  border: #F56C6C;
}
</style>
