// 人员布控名单审核
<template>
  <div class="app-container">
    <uploadModal
      ref="uploadModal"
      @finish="updateModalSuccess(false)"
      :type="'dialog'"
    />
    <div v-if="isShowUpdate" class="container">
      <addModal ref="addModel" @success="modalSuccess" @back="back" />
    </div>
    <div v-if="!isShowUpdate">
      <el-form :inline="true" :model="page.data">
        <el-form-item class="margin-left">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addOrUpdate('添加布控')"
            >添加布控审核</el-button
          >
        </el-form-item>
        <el-form-item class="margin-left">
          <el-button type="primary" icon="el-icon-download" @click="download"
            >下载模板</el-button
          >
        </el-form-item>
        <el-form-item class="margin-left">
          <el-button type="primary" plain @click="batchImport"
            >批量导入审核</el-button
          >
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-select
            v-model="page.data.warningGrade"
            clearable
            filterable
            placeholder="请选择预警等级"
          >
            <el-option
              v-for="item in warning_list"
              :key="item.code"
              :label="item.desc"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-input
            clearable
            v-model="page.data.name"
            placeholder="请输入布控人员姓名"
          ></el-input>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-select
            v-model="page.data.documentType"
            clearable
            filterable
            placeholder="请选择证件类型"
          >
            <el-option label="护照" :value="1"></el-option>
            <el-option label="身份证号" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-input
            clearable
            v-model="page.data.idNumber"
            placeholder="请输入布控人员证件号"
          ></el-input>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-input
            clearable
            v-model="page.data.taskName"
            placeholder="请输入任务名称"
          ></el-input>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-select
            v-model="page.data.controlAdminId"
            clearable
            filterable
            placeholder="请选择布控单位"
          >
            <el-option
              v-for="item in logAdminList"
              :key="item.adminId"
              :label="item.username"
              :value="item.adminId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-select
            v-model="page.data.createAdminId"
            clearable
            filterable
            placeholder="请选择创建单位"
          >
            <el-option
              v-for="item in personCreateList"
              :key="item.adminId"
              :label="item.username"
              :value="item.adminId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-input
            clearable
            v-model="page.data.manageName"
            placeholder="请输入负责人姓名"
          ></el-input>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-input
            clearable
            v-model="page.data.managePhone"
            placeholder="请输入负责人手机号"
          ></el-input>
        </el-form-item>
        <el-form-item class="margin-left input">
          <el-select
            v-model="page.data.status"
            clearable
            filterable
            placeholder="请选择审核状态"
          >
            <el-option label="待审核" :value="1"></el-option>
            <el-option label="已同意" :value="2"></el-option>
            <el-option label="整改中" :value="3"></el-option>
            <el-option label="已驳回" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="margin-left">
          <el-button type="primary" @click="fetchData" icon="el-icon-search"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 主体表格 -->
      <el-table
        :data="tableData"
        v-loading="listLoading"
        border
        fit
        highlight-current-row
        class="table"
      >
        <el-table-column align="center" width="80" label="序号">
          <template slot-scope="scope">
            {{ indexMethod(scope.$index) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="warningGrade" label="预警等级">
          <template v-slot="{ row }">
            <el-tag
              class="tag-color"
              color="#B60ED1"
              v-if="row.warningGrade == 1"
              >Ⅰ级</el-tag
            >
            <el-tag
              class="tag-color"
              color="#FF5C66"
              v-if="row.warningGrade == 2"
              >Ⅱ级</el-tag
            >
            <el-tag
              class="tag-color"
              color="#FF8B49"
              v-if="row.warningGrade == 3"
              >Ⅲ级</el-tag
            >
            <el-tag
              class="tag-color"
              color="#FFC52F"
              v-if="row.warningGrade == 4"
              >Ⅳ级</el-tag
            >
            <el-tag
              class="tag-color"
              color="#78D1E5"
              v-if="row.warningGrade == 5"
              >Ⅴ级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" prop="orderNo" label="布控人员名单">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="getNameList(row.warningPersonCheckId)"
              >名单</el-button
            >
          </template>
        </el-table-column>
        <el-table-column align="center" prop="controlNum" label="布控人员数">
        </el-table-column>
        <el-table-column align="center" prop="taskName" label="任务名称">
        </el-table-column>
        <el-table-column align="center" prop="controlUsername" label="布控单位">
        </el-table-column>
        <el-table-column align="center" label="布控时间段">
          <template v-slot="{ row }">
            {{ row.controlStartTime }}至{{ row.controlEndTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createUsername" label="创建单位">
        </el-table-column>
        <el-table-column align="center" prop="manageName" label="负责人">
        </el-table-column>
        <el-table-column align="center" prop="managePhone" label="负责人手机号">
          <template v-slot="{ row }">
            {{ (row.managePhone || []).join(",") || "无" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status" label="审核状态">
          <template v-slot="{ row }">
            <el-tag type="info" v-if="row.status == 1">待审核</el-tag>
            <el-tag v-if="row.status == 2">已同意</el-tag>
            <el-tag type="warning" v-if="row.status == 3">整改中</el-tag>
            <el-tag type="danger" v-if="row.status == 4">已驳回</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="remark" label="备注">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" label="操作区" width="120">
          <template v-slot="{ row }" v-if="checkRole([2])">
            <span v-if="row.status == 3"> — </span>
            <el-button
              v-if="row.status == 2 || row.status == 4"
              type="primary"
              size="small"
              @click="detailReview(row)"
              >详情</el-button
            >
            <el-button
              type="primary"
              size="small"
              v-if="row.status == 1"
              @click="detailReview(row, 'review')"
              >审核</el-button
            >
          </template>
          <template v-else v-slot="{ row }">
            <el-button
              v-if="row.status == 3"
              type="primary"
              size="small"
              @click="handledUpdate('编辑布控', row)"
              >整改</el-button
            >
            <el-button
              v-if="row.status == 1 || row.status == 2 || row.status == 4"
              type="primary"
              size="small"
              @click="detailReview(row)"
              >详情</el-button
            >
            <!-- <el-button
              type="primary"
              size="small"
              v-if="row.status == 1"
              @click="detailReview(row, 'review')"
              >审核</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <!--分页栏-->
      <pagination
        :total="total"
        :page.sync="page.pageNum"
        :limit.sync="page.pageSize"
        @pagination="fetchData"
      />

      <!-- 添加/编辑 -->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="1100px"
        :close-on-click-modal="false"
      >
        <div v-if="isEdit" class="dialog-top">
          整改内容：{{ addOrUpdateForm.remark }}
        </div>
        <el-form
          :model="addOrUpdateForm"
          :rules="rules"
          ref="formRef"
          label-position="left"
          label-width="120px"
        >
          <div class="flex">
            <el-form-item
              label="预警级别"
              prop="warningGrade"
              style="flex: 0.5"
            >
              <el-select
                v-model="addOrUpdateForm.warningGrade"
                filterable
                clearable
                placeholder="请选择预警级别"
              >
                <el-option
                  v-for="item in warning_list"
                  :key="item.code"
                  :label="item.desc"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="人员姓名"
              prop="name"
              style="margin-left: 40px; flex: 0.5"
            >
              <el-input
                clearable
                placeholder="请输入人员姓名"
                v-model="addOrUpdateForm.name"
              />
            </el-form-item>
          </div>
          <div class="flex">
            <el-form-item
              label="证件类型"
              style="flex: 0.5"
              prop="documentType"
            >
              <el-radio-group v-model="addOrUpdateForm.documentType">
                <el-radio :label="1">护照</el-radio>
                <el-radio :label="2">身份证</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="证件号"
              style="margin-left: 40px; flex: 0.5"
              prop="idNumber"
            >
              <el-input
                clearable
                placeholder="请输入证件号"
                v-model="addOrUpdateForm.idNumber"
              />
            </el-form-item>
          </div>
          <div class="flex">
            <el-form-item
              label="布控人员类型"
              style="flex: 0.5"
              prop="warningType"
            >
              <el-input
                clearable
                placeholder="请输入布控人员类型"
                v-model="addOrUpdateForm.warningType"
              />
            </el-form-item>
            <el-form-item
              label="任务名称"
              style="margin-left: 40px; flex: 0.5"
              prop="taskName"
            >
              <el-input
                clearable
                placeholder="请输入任务名称"
                v-model="addOrUpdateForm.taskName"
              />
            </el-form-item>
          </div>
          <div class="flex">
            <el-form-item
              style="flex: 0.5"
              label="布控单位"
              prop="controlAdminId"
            >
              <el-select
                v-model="addOrUpdateForm.controlAdminId"
                filterable
                clearable
                placeholder="请选择布控单位"
              >
                <el-option
                  v-for="item in oneselfBelowList"
                  :key="item.adminId"
                  :label="item.username"
                  :value="item.adminId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="布控时间段"
              style="margin-left: 40px; flex: 0.5"
              prop="time"
            >
              <el-date-picker
                v-model="addOrUpdateForm.time"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="选择布控开始时间"
                end-placeholder="选择布控结束时间"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </div>
          <div class="flex">
            <el-form-item style="flex: 0.5" label="负责人" prop="manageName">
              <el-input
                placeholder="请输入负责人姓名"
                v-model.trim="addOrUpdateForm.manageName"
              />
            </el-form-item>
            <el-form-item
              label="负责人手机号"
              style="margin-left: 40px; flex: 0.5"
              prop="phone"
            >
              <el-input
                clearable
                placeholder="请输入负责人手机号，多个手机号用“，”隔开,最多10个"
                v-model.trim="addOrUpdateForm.phone"
              />
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 布控人员名单 -->
      <el-dialog
        title="布控人员名单"
        :visible.sync="dialogVisibleName"
        width="900px"
        :close-on-click-modal="false"
      >
        <!-- 表格 -->
        <el-table
          :data="nameTableData"
          v-loading="nameListLoading"
          border
          fit
          highlight-current-row
          class="table"
        >
          <el-table-column align="center" width="80" label="序号">
            <template slot-scope="scope">
              {{ indexNameMethod(scope.$index) }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" label="人员姓名">
          </el-table-column>
          <el-table-column align="center" prop="documentType" label="证件类型">
            <template v-slot="{ row }">
              <el-tag type="warning" effect="plain" v-if="row.documentType == 2"
                >身份证</el-tag
              >
              <el-tag effect="plain" v-else>护照</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="idNumber" label="证件号">
          </el-table-column>
          <el-table-column
            align="center"
            prop="warningType"
            label="布控人员类型"
          >
          </el-table-column>
        </el-table>
        <!--分页栏-->
        <pagination
          :total="total1"
          :page.sync="page1.pageNum"
          :limit.sync="page1.pageSize"
          @pagination="getNameList(page1.data.warningPersonCheckId)"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleName = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisibleName = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  getwarningPersonLogAdminList,
  getwarningPersonOneselfBelowList,
  getwarningPersonCreateList,
  getwarningPersonReviewPageList,
  getwarningPersonInsert,
  getwarningPersonRectify,
  getwarningPersonCheckPageList,
} from "@/api/personnelWarning";
import { checkRole } from "@/utils/checkRole";
import AddModal from "./modal/AddModal.vue";
import UploadModal from "./components/UploadModal.vue";
export default {
  components: { Pagination, AddModal, UploadModal },
  data() {
    return {
      dialogVisible: false, //新增编辑
      dialogTitle: "",
      dialogVisibleName: false, //人员名单信息
      isShowUpdate: false, //是否审核详情
      listLoading: false,
      nameListLoading: false,
      option: [],
      logAdminList: [],
      oneselfBelowList: [],
      personCreateList: [],
      isEdit: false, //判断是否是编辑
      // 预警级别
      warning_list: [
        { code: 1, desc: "Ⅰ级" },
        { code: 2, desc: "Ⅱ级" },
        { code: 3, desc: "Ⅲ级" },
        { code: 4, desc: "Ⅳ级" },
        { code: 5, desc: "Ⅴ级" },
      ],
      // 添加订单参数
      addOrUpdateForm: {
        warningGrade: "",
        name: "",
        documentType: "",
        idNumber: "",
        warningType: "",
        taskName: "",
        controlAdminId: "",
        controlStartTime: "",
        controlEndTime: "",
        manageName: "",
        phone: "",
        managePhone: [],
        time: "",
        remark: "",
      },
      tableData: [],
      nameTableData: [],
      total: 0,
      // 页面
      page: {
        // 搜索表单
        data: {
          warningGrade: "",
          name: "",
          documentType: "",
          idNumber: "",
          taskName: "",
          controlAdminId: "",
          createAdminId: "",
          manageName: "",
          managePhone: "",
          status: "",
        },
        pageNum: 1,
        pageSize: 10,
      },
      page1: {
        data: {
          documentType: "",
          idNumber: "",
          name: "",
          warningPersonCheckId: "",
          warningType: "",
        },
        pageNum: 0,
        pageSize: 10,
      },
      total1: 0,
      rules: {
        warningGrade: [
          {
            required: true,
            message: "请选择预警级别",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入人员姓名",
            trigger: "blur",
          },
        ],
        documentType: [
          {
            required: true,
            message: "请选择证件类型",
            trigger: "change",
          },
        ],
        idNumber: [
          {
            required: true,
            message: "请输入证件号",
            trigger: "blur",
          },
        ],
        warningType: [
          {
            required: true,
            message: "请输入布控人员类型",
            trigger: "blur",
          },
        ],
        taskName: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "blur",
          },
        ],
        controlAdminId: [
          {
            required: true,
            message: "请选择布控单位",
            trigger: "change",
          },
        ],
        time: [
          {
            required: true,
            message: "请选择布控时间",
            trigger: "change",
          },
        ],
        manageName: [
          {
            required: true,
            message: "请输入负责人姓名",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入负责人手机号,中文逗号隔开",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              const numbers = value.split("，");
              if (numbers.every((num) => /^[0-9]+$/.test(num.trim()))) {
                callback();
              } else {
                callback(new Error("请输入正确格式的电话号码或用中文逗号分隔"));
              }
            },
            trigger: "blur",
          },
        ],
      },
      rowData: null, // 传递的参数
    };
  },
  created() {
    this.fetchData();
    this.getLogAdminList();
    this.getOneselfBelowList();
    this.getCreateList();
  },
  mounted() {
    this.rowData = this.$route.params.rowData;
    if (this.rowData) {
      this.addOrUpdate("添加布控", { ...this.rowData, phone: "" });
    }
  },
  methods: {
    checkRole,
    fetchData() {
      this.listLoading = true;
      getwarningPersonReviewPageList(this.page).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    async getLogAdminList() {
      const res = await getwarningPersonLogAdminList();
      this.logAdminList = res.data;
    },
    async getOneselfBelowList() {
      const res = await getwarningPersonOneselfBelowList();
      this.oneselfBelowList = res.data;
    },
    async getCreateList() {
      const res = await getwarningPersonCreateList();
      this.personCreateList = res.data;
    },
    // 添加
    addOrUpdate(dialogTitle, rowData) {
      this.dialogVisible = true;
      this.dialogTitle = dialogTitle;
      this.$nextTick(async () => {
        this.$refs.formRef.resetFields();
        if (rowData) {
          // 布控传入的添加
          this.addOrUpdateForm = {
            ...this.addOrUpdateForm,
            ...this.rowData,
            documentType: 2,
          };
          this.isEdit = false;
        }
        // else if (this.rowData) {
        //   console.log(this.rowData, 8);
        //   this.addOrUpdateForm = this.rowData;
        //   this.isEdit = false;
        // }
        else {
          // 添加
          this.isEdit = false;
        }
      });
    },
    // 编辑
    async handledUpdate(dialogTitle, edit) {
      // 修改
      this.page1.data.warningPersonCheckId = edit.warningPersonCheckId;
      const res = await getwarningPersonCheckPageList(this.page1);
      const personnlData = res.data.records;
      this.total1 = res.data.total;
      if (this.total1 == 1) {
        // 单个添加后的编辑
        this.dialogVisible = true;
        this.dialogTitle = dialogTitle;
        this.$nextTick(() => {
          this.$refs.formRef.resetFields();
          this.addOrUpdateForm = {
            ...JSON.parse(JSON.stringify(edit)),
            ...personnlData[0],
            time: [edit.controlStartTime, edit.controlEndTime],
            phone: edit.managePhone.join("，"),
          };
          this.isEdit = true;
        });
      } else {
        // 批量编辑
        this.$refs.uploadModal.open(edit);
      }
    },
    // 确认按钮
    submit() {
      this.$refs.formRef.validate(async (pass) => {
        if (!pass) {
          return false;
        }
        this.addOrUpdateForm.controlStartTime = this.addOrUpdateForm.time[0];
        this.addOrUpdateForm.controlEndTime = this.addOrUpdateForm.time[1];
        this.addOrUpdateForm.managePhone = this.addOrUpdateForm.phone
          .split("，")
          .map((item) => item);
        this.addOrUpdateForm.warningPersonCheckListDtoList = [
          {
            documentType: this.addOrUpdateForm.documentType,
            idNumber: this.addOrUpdateForm.idNumber,
            name: this.addOrUpdateForm.name,
            warningType: this.addOrUpdateForm.warningType,
          },
        ];
        if (!this.isEdit) {
          await getwarningPersonInsert({
            ...this.addOrUpdateForm,
            warningPersonCheckId: this.isEdit
              ? this.addOrUpdateForm.warningPersonCheckId
              : undefined,
          });
        } else {
          await getwarningPersonRectify({
            ...this.addOrUpdateForm,
            warningPersonCheckId: this.isEdit
              ? this.addOrUpdateForm.warningPersonCheckId
              : undefined,
          });
        }
        await this.fetchData();
        this.$message.success(`${this.dialogTitle}成功`);
        this.dialogVisible = false;
      });
    },
    // 查看名单
    async getNameList(id) {
      this.dialogVisibleName = true;
      this.page1.data.warningPersonCheckId = id;
      this.nameListLoading = true;
      const res = await getwarningPersonCheckPageList(this.page1);
      this.nameTableData = res.data.records;
      this.total1 = res.data.total;
      this.nameListLoading = false;
    },
    // 详情审核
    detailReview(row, review) {
      this.isShowUpdate = true;
      this.$nextTick(() => {
        this.$refs.addModel.open(row, review);
      });
    },

    modalSuccess() {
      this.fetchData();
    },
    back() {
      this.isShowUpdate = false;
      this.fetchData();
    },
    // 下载模板
    download() {
      const baseURLStr = window.g.ApiUrl;
      window.location.href =
        // process.env.VUE_APP_BASE_API + "/excel/warningPerson.xlsx";
        baseURLStr + "/excel/warningPerson.xlsx";
    },
    // 批量导入审核
    batchImport() {
      this.$refs.uploadModal.open();
    },
    updateModalSuccess(isEdit) {
      if (!isEdit) {
        this.fetchData();
      }
      this.fetchData();
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
    //
    indexNameMethod(index) {
      return (this.page1.pageNum - 1) * this.page1.pageSize + index + 1;
    },
  },
};
</script>

<style lang="scss" scoped>
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 180px;
}
.flex {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.tag-color {
  color: #fff !important;
}
.container {
  padding-left: 16px;
}
.dialog-top {
  padding: 16px;
  background-color: #ff5c651a;
  color: #ff4747;
  font-size: 16px;
  font-weight: 600;
  border-radius: 10px;
  margin-bottom: 10px;
}
</style>
