<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="4">
            <el-form-item label="关键字搜索" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="创建单位" prop="CORP_NAME">
              <el-input v-model="searchForm.CORP_NAME" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="施工相关方" prop="UNITS_ID">
              <el-select
                v-model="searchForm.UNITS_ID"
                filterable
                clearable
                placeholder="请选择相关方"
              >
                <el-option
                  v-for="item in data.unitsList"
                  :key="item.UNITS_ID"
                  :label="item.UNITS_NAME"
                  :value="item.UNITS_ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="CORP_NAME"
          label="创建单位"
        />
        <el-table-column prop="OUTSOURCED_NAME" label="重点工程名称" />
        <el-table-column prop="UNITS_NAME" label="施工相关方" />
        <el-table-column label="辖区单位">
          <template #default="{ row }">
            <span v-if="row.JURISDICTION_UNIT_NAME">
              {{ row.JURISDICTION_UNIT_NAME }}
            </span>
            <span v-else>{{ row.INVOLVING_CORPS_DEPART_NAME }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UNITS_NAME" label="主管部门">
          <template #default="{ row }">
            <span v-if="row.Q_COMPETENT_DEPT_NAME">
              {{ row.Q_COMPETENT_DEPT_NAME }}
            </span>
            <span v-else>{{ row.MANAGER_DEPARTMENT_NAME }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="DEPARTMENT_NAME" label="监管部门">
          <template #default="{ row }">
            <span v-if="row.Q_DEPARTMENT_NAME">
              {{ row.Q_DEPARTMENT_NAME }}
            </span>
            <span v-else>{{ row.DEPARTMENT_NAME }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="STATE" label="状态">
          <template #default="{ row }">
            <span v-if="row.STATE === '0'">未开工</span>
            <span v-if="row.STATE === '1'">进行中</span>
            <span v-if="row.STATE === '2'">已结束</span>
            <span v-if="row.STATE === '-1'">开工申请中</span>
            <span v-if="row.STATE === '-2'">结束申请中</span>
          </template>
        </el-table-column>
        <el-table-column prop="OUTSOURCED_CREATOR_NAME" label="创建人" />
        <el-table-column label="操作" align="center" width="230">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/keyprojects/outsourced/view',
                  query: {
                    outsourcedId: row.OUTSOURCED_ID,
                  },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="row.STATE === '-2'"
              type="primary"
              text
              link
              @click="fnHandleJie(row.OUTSOURCED_ID)"
            >
              结束审批
            </el-button>
            <el-button
              v-if="row.STATE === '-1'"
              type="primary"
              text
              link
              @click="fnHandleStart(row.OUTSOURCED_ID)"
            >
              开工审批
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/keyprojects/outsourced/add',
                  query: {
                    type: 'edit',
                    outsourcedId: row.OUTSOURCED_ID,
                  },
                })
              "
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnHandleDelete(row.OUTSOURCED_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="
              router.push({
                path: '/keyprojects/outsourced/add',
                query: {
                  type: 'add',
                },
              })
            "
          >
            新增
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import useListData from "@/assets/js/useListData";
import { serialNumber } from "@/assets/js/utils";
import {
  getOutsourcedList,
  getUnitsListAll,
  setOutsourcedDelete,
  getOutsourcedUpdateState,
} from "@/request/keyprojects.js";
import { reactive } from "vue";
import router from "@/router";
import { ElMessage, ElMessageBox } from "element-plus";

const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getOutsourcedList);

const data = reactive({
  unitsList: [],
});

const fnGetUnitsList = async () => {
  const resData = await getUnitsListAll({});
  data.unitsList = resData.varList;
};
fnGetUnitsList();

const fnResetPaginationTransfer = () => {
  fnGetData(searchForm);
};

const fnHandleDelete = async (OUTSOURCED_ID) => {
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  await setOutsourcedDelete({ OUTSOURCED_ID });
  ElMessage.success("删除成功");
  fnGetData();
};

const fnHandleJie = async (OUTSOURCED_ID) => {
  await ElMessageBox.confirm("确定要结束吗？", {
    type: "warning",
  });
  await getOutsourcedUpdateState({ OUTSOURCED_ID, STATE: "2" });
  ElMessage.success("已结束");
  fnGetData();
};

const fnHandleStart = async (OUTSOURCED_ID) => {
  await ElMessageBox.confirm("确定要开工吗？", {
    type: "warning",
  });
  await getOutsourcedUpdateState({ OUTSOURCED_ID, STATE: "1" });
  ElMessage.success("已开工");
  fnGetData();
};
</script>

<style scoped></style>
