<template>
  <el-card class="hl-card">
    <template #header>
      <span>生产计划</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="120px">
        <el-form-item label="生产计划编号" prop="planNo">
          <el-input v-model="queryParams.planNo" placeholder="生产计划编号" clearable @keyup.enter="handleQuery" class="!w-250px" />
        </el-form-item>
        <el-form-item label="项目编号" prop="projectCode">
          <el-input v-model="queryParams.projectCode" placeholder="项目编号" clearable @keyup.enter="handleQuery" class="!w-250px" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="客户名称" clearable @keyup.enter="handleQuery" class="!w-250px" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="queryParams.projectName" placeholder="项目名称" clearable @keyup.enter="handleQuery" class="!w-250px" />
        </el-form-item>
        <el-form-item label="所属业务线" prop="businessLine">
          <el-select class="!w-250px" v-model="queryParams.businessLine" placeholder="下拉选择">
            <el-option v-for="dict in getStrDictOptions(DICT_TYPE.HELI_BUSINESS_LINE)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="性质" prop="property">
          <el-select class="!w-250px" v-model="queryParams.property" placeholder="下拉选择">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PROPERTY)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="生产状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="生产状态" clearable class="!w-250px">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PLAN_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目负责人" prop="projectOwner">
          <el-input v-model="queryParams.projectOwner" placeholder="项目负责人" clearable @keyup.enter="handleQuery" class="!w-250px" />
        </el-form-item>
        <el-form-item style="margin-left:15px">
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> 重置
          </el-button>
          <el-button type="primary" plain @click="openAdd()" v-if="false">
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
        <el-table-column type="index" width="100" fixed label="序号" align="center" />
        <el-table-column fixed label="计划单号" align="center" prop="planNo" width="150">
          <template #default="scope">
            <el-button text type="primary" @click="handleCommand('detail',scope.row.id)">
              {{scope.row.planNo}}
            </el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column fixed label="项目编号" align="center" prop="projectCode" width="220" /> -->
        <el-table-column fixed label="项目变更次数" align="center" prop="changeNum" width="150" />
        <el-table-column label="客户名称" align="center" prop="customerName" width="150" />
        <el-table-column label="项目名称" align="center" prop="projectName" width="150" />
        <!-- <el-table-column label="业务员" align="center" prop="businessManName" width="150" /> -->
        <el-table-column label="起止时间" align="center" prop="projectStartTime" width="260">
          <template #default="scope">
            {{
              formatDate(scope.row.projectStartTime, 'YYYY-MM-DD') +
              '~' +
              formatDate(scope.row.projectEndTime, 'YYYY-MM-DD')
            }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="所属业务线" align="center" prop="businessLine" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_BUSINESS_LINE" :value="scope.row.businessLine" />
          </template>
        </el-table-column> -->
        <el-table-column label="性质" align="center" prop="property" width="120">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_PROJECT_PROPERTY" :value="scope.row.property" />
          </template>
        </el-table-column>
        <el-table-column label="是否紧急" align="center" prop="isUrgency" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.isUrgency" />
          </template>
        </el-table-column>
        <el-table-column label="是否已变更" align="center" prop="hasAlter" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.hasAlter" />
          </template>
        </el-table-column>
        <el-table-column label="是否要工艺" align="center" prop="hasCraft" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.hasCraft" />
          </template>
        </el-table-column>
        <el-table-column label="工艺起止时间" align="center" prop="craftStartDate" width="260">
          <template #default="scope">
            {{
              formatDate(scope.row.craftStartDate, 'YYYY-MM-DD') +
              '~' +
              formatDate(scope.row.craftEndDate, 'YYYY-MM-DD')
            }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="220px" />
        <el-table-column label="备注" align="center" prop="description" width="150" />
        <el-table-column fixed="right" label="项目负责人" align="center" prop="projectOwner" width="150">
          <template #default="scope">
            {{ userInit.find((user) => user.id == scope.row.projectOwner)?.nickname }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="工艺负责人" align="center" prop="craftOwner" width="150">
          <template #default="scope">
            {{ userInit.find((user) => user.id == scope.row.craftOwner)?.nickname }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="生产状态" align="center" prop="status" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_PROJECT_PLAN_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" align="center" width="190">
          <template #default="scope">
            <el-row>
              <el-col>
                <el-button link type="primary" @click="openForm(scope.row.id)" v-if="scope.row.status!='3'">
                  编辑
                </el-button>
                <el-button link type="primary" @click="complete(scope.row.id)" v-if="scope.row.status!='3'&& scope.row.status!='4'">
                  生产完成
                </el-button>
                <el-dropdown @command="(command) => handleCommand(command, scope.row.id, scope.row.code)">
                  <span class="el-dropdown-link">更多
                    <el-icon class="el-icon--right">
                      <arrow-down />
                    </el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="detail">查看详情</el-dropdown-item>
                      <el-dropdown-item command="printProjectPlan">
                        打印生产计划单
                      </el-dropdown-item>
                      <!--                      <el-dropdown-item command="A3printProjectPlan">
                                              打印子项目跟踪表
                                            </el-dropdown-item>-->
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </el-col>
            </el-row>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </ContentWrap>

  </el-card>
  <!-- 【打印启动单】 - 弹框-->
  <Print ref="printRef" />
  <A3sonPrint ref="A3printRef" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PlanApi from '@/api/heli/plan'
import Print from './productionPrint.vue' // 引入打印弹框
import A3sonPrint from './A3sonPrint.vue' // 引入A3打印弹框
import * as UserApi from '@/api/system/user'
import {disposeCoordSysRecordIfNeeded} from "echarts/types/src/component/dataZoom/roams";

defineOptions({ name: 'Plan' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planNo: undefined,
  projectCode: undefined,
  customerName: undefined,
  projectName: undefined,
  businessManName: undefined,
  businessLine: undefined,
  property: undefined,
  projectOwner: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PlanApi.getPlanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const handleCommand = async (command, id, code) => {
  switch (command) {
    case 'detail':
      openDetail(id)
      break
    case 'printProjectPlan':
      printHandle(id)
      break
    case 'A3printProjectPlan':
      A3printHandle(id)
      break
  }
}

// 打印启动单
const printRef = ref() // 打印弹框
// 打印启动单
const printHandle = (id) => {
  printRef.value.open(id)
}
// 打印启动单
const A3printRef = ref() // 打印弹框
// 打印启动单
const A3printHandle = (id) => {
  A3printRef.value.open(id)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id: number) => {
  router.push({ path: '/plan/planedit', query: { id: id } })
}
const complete=async(id:number)=>{
  ElMessageBox.confirm(
    '是否生产完成','确认',
    {
      cancelButtonText: '否',
      confirmButtonText: '是',
      type: 'warning',
    } 
  ).then(async () => {
    const data =await PlanApi.complete(id)
    if (data){
      message.success("操作成功")
      getList()
    }else {
      message.error("生产任务单报工或装配报工未完成,请确认")
    }
  }).catch(()=>{})

}
const openAdd = (id: number) => {
  router.push({ path: '/plan/planadd' })
}

/** 添加/修改操作 */
const openDetail = (id: number) => {
  router.push({ path: '/plan/plandetail', query: { id: id } })
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PlanApi.exportPlan(queryParams)
    download.excel(data, '生产计划.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const userInit = ref()
/** 初始化 **/
onMounted(async () => {
  //用户列表数据
  userInit.value = await UserApi.getSimpleUserList()
  await getList()
})
</script>
<style scoped>
.el-dropdown-link {
  cursor: pointer;
  margin: 5px;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>
