// 人员账号管理
<template>
  <div class="app-container">
    <uploadModal
      ref="uploadModal"
      :option="option"
      @finish="modalSuccess(false)"
      :type="checkRole([0]) ? 'dialog' : 'button'"
    />
    <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 v-if="checkRole([0])" class="margin-left">
        <el-button type="primary" plain @click="batchImport"
          >批量导入账号</el-button
        >
      </el-form-item>
      <el-form-item v-else class="margin-left">
        <el-button type="primary" plain @click="batchImport1"
          >批量导入账号</el-button
        >
      </el-form-item>
      <el-form-item class="margin-left input" v-if="checkRole([0])">
        <el-select
          v-model="page.data.accountId"
          filterable
          clearable
          placeholder="请选择所属账号"
        >
          <el-option
            v-for="item in option"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-select
          v-model="page.data.userId"
          filterable
          clearable
          placeholder="请选择人员账号"
        >
          <el-option
            v-for="item in option1"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-input
          v-model="page.data.idName"
          placeholder="请输入姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-input
          v-model="page.data.idNumber"
          placeholder="请输入身份证号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-left input">
        <el-select
          v-model="page.data.freeDays"
          clearable
          multiple
          filterable
          placeholder="请选择休息日"
        >
          <el-option
            v-for="item in freeDaysList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></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="accountName" label="所属账号">
      </el-table-column>
      <el-table-column align="center" prop="username" label="人员账号">
      </el-table-column>
      <el-table-column align="center" prop="idName" label="姓名">
      </el-table-column>
      <el-table-column align="center" prop="idNumber" label="身份证号">
      </el-table-column>
      <el-table-column align="center" prop="freeDays" label="休息日">
        <template v-slot="{ row }">
          <template v-for="item in freeDaysList">
            <el-tag
              :type="
                !['Saturday', 'Sunday'].includes(item.id)
                  ? 'primary'
                  : 'success'
              "
              :key="item.id"
              v-if="row.freeDays.includes(item.id)"
              >{{ item.name }}</el-tag
            >
          </template>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column align="center" label="操作区" width="180">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="addOrUpdate('编辑巡逻点', row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="delOrder(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <!--分页栏-->
    </el-table>
    <pagination
      :total="total"
      :page.sync="page.pageNumber"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />

    <!-- 添加/编辑 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="870px"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addOrUpdateForm"
        :rules="rules"
        ref="formRef"
        label-position="left"
        label-width="120px"
      >
        <div class="display-config-layout">
          <el-form-item label="所属账号" prop="accountId" v-if="checkRole([0])">
            <el-select
              v-model="addOrUpdateForm.accountId"
              filterable
              clearable
              placeholder="请选择所属账号"
            >
              <el-option
                v-for="item in option"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="人员账号名称" prop="username">
            <el-input
              v-model="addOrUpdateForm.username"
              placeholder="请输入账号名称"
              :disabled="disabled"
            ></el-input>
          </el-form-item>
          <el-form-item label="人员账号密码" prop="password">
            <el-input
              v-model="addOrUpdateForm.password"
              placeholder="请输入账号密码"
            ></el-input>
          </el-form-item>
          <el-form-item label="人员账号类型" prop="userType">
            <el-radio-group v-model="addOrUpdateForm.userType">
              <el-radio :label="0">巡更员</el-radio>
              <el-radio :label="1">管理员</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户姓名" prop="idName">
            <el-input
              placeholder="请输入用户姓名"
              v-model.trim="addOrUpdateForm.idName"
              :disabled="disabled"
            />
          </el-form-item>
          <el-form-item label="用户身份证号" prop="idNumber">
            <el-input
              placeholder="请输入用户身份证号"
              v-model.trim="addOrUpdateForm.idNumber"
              maxlength="18"
              show-word-limit
              :disabled="disabled"
            />
          </el-form-item>
        </div>
        <el-form-item label="用户休息日" prop="freeDays">
          <el-checkbox-group v-model="addOrUpdateForm.freeDays">
            <el-checkbox label="Monday">周一</el-checkbox>
            <el-checkbox label="Tuesday">周二</el-checkbox>
            <el-checkbox label="Wednesday">周三</el-checkbox>
            <el-checkbox label="Thursday">周四</el-checkbox>
            <el-checkbox label="Friday">周五</el-checkbox>
            <el-checkbox label="Saturday">周六</el-checkbox>
            <el-checkbox label="Sunday">周日</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </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>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  doGetSubuserlist,
  doGetSubuser,
  doEditSubuser,
  doDeleteSubuser,
  doDetailSubuser,
} from "@/api/subuser";
import { getUserlist } from "@/api/user";
import { checkRole } from "@/utils/checkRole";
import UploadModal from "./components/UploadModal.vue";
export default {
  components: { Pagination, UploadModal },
  data() {
    return {
      dialogVisible: false, //新增编辑
      dialogTitle: "",
      listLoading: false,
      option: [], //账号列表
      option1: [], //人员账号
      submitSendLoading: false,
      isEdit: false, //判断是否是编辑
      disabled: false, //编辑时禁用
      // 添加参数
      addOrUpdateForm: {
        accountId: "",
        username: "", //账号名称
        password: "",
        userType: 0,
        idName: "",
        idNumber: "",
        freeDays: [],
      },
      tableData: [],
      total: 0,
      freeDaysList: [
        { id: "Monday", name: "周一" },
        { id: "Tuesday", name: "周二" },
        { id: "Wednesday", name: "周三" },
        { id: "Thursday", name: "周四" },
        { id: "Friday", name: "周五" },
        { id: "Saturday", name: "周六" },
        { id: "Sunday", name: "周日" },
      ],
      // 页面
      page: {
        // 搜索表单
        data: {
          accountId: "", //所属账号
          userId: "",
          idName: "", //用户姓名
          idNumber: "", //用户身份证号
          freeDays: [],
        },
        pageNumber: 1,
        pageSize: 10,
      },
      rules: {
        accountId: [
          {
            required: true,
            message: "请选择所属账号",
            trigger: "change",
          },
        ],
        username: [
          {
            required: true,
            message: "请输入账号名称",
            trigger: "blur",
          },
        ],
        userType: [
          {
            required: true,
            message: "请选择账号类型",
            trigger: "change",
          },
        ],
        idName: [
          {
            required: true,
            message: "请输入用户姓名",
            trigger: "blur",
          },
        ],
        idNumber: [
          {
            required: true,
            message: "请输入用户身份证号",
            trigger: "blur",
          },
          { min: 18, max: 18, message: "长度在 18 个字符", trigger: "blur" },
          {
            pattern:
              /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dxX]$/,
            message: "请输入正确的身份证号码",
            trigger: "blur",
          },
        ],
        // freeDays: [
        //   {
        //     required: true,
        //     message: "请勾选用户休息日",
        //     trigger: "change",
        //   },
        // ],
      },
    };
  },
  created() {
    this.fetchData();
    if (checkRole([0])) {
      this.getUserList();
    }
    this.getSubUser();
  },
  methods: {
    checkRole,
    fetchData() {
      this.listLoading = true;
      doGetSubuserlist(this.page).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    getUserList() {
      getUserlist().then((res) => {
        this.option = res.data;
      });
    },
    getSubUser() {
      doGetSubuser().then((res) => {
        this.option1 = res.data;
      });
    },
    // 添加编辑订单
    addOrUpdate(dialogTitle, edit) {
      this.dialogVisible = true;
      this.dialogTitle = dialogTitle;
      this.$nextTick(async () => {
        this.$refs.formRef.resetFields();
        if (edit) {
          this.disabled = true;
          // 修改
          const res1 = await doDetailSubuser(edit.id);
          this.addOrUpdateForm = JSON.parse(JSON.stringify(res1.data));
          this.isEdit = true;
        } else {
          // 添加
          this.disabled = false;
          this.isEdit = false;
        }
      });
    },
    // 确认按钮
    submit() {
      this.$refs.formRef.validate(async (pass) => {
        if (!pass) {
          return false;
        }
        await doEditSubuser({
          ...this.addOrUpdateForm,
          id: this.isEdit ? this.addOrUpdateForm.id : undefined,
        });
        await this.fetchData();
        this.$message.success(`${this.dialogTitle}成功`);
        this.submitLoading = false;
        this.dialogVisible = false;
      });
    },
    // 删除订单
    delOrder(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        doDeleteSubuser(id).then((res) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    // 下载模板
    download() {
      const baseURLStr = window.g.ApiUrl;
      // console.log(baseURLStr);
      window.location.href = baseURLStr + "/file/personnelListExcel.xlsx";
    },
    // download(href, title) {
    //   const a = document.createElement("a");
    //   a.setAttribute("href", href);
    //   a.setAttribute("download", title);
    //   a.click();
    // },

    // 批量导入
    batchImport() {
      this.$refs.uploadModal.open();
    },
    batchImport1() {
      this.$refs.uploadModal.$refs.batchUploadRef.$refs.referenceUpload.click();
      // console.log(this.$refs.uploadModal.$refs.batchUploadRef);
    },
    modalSuccess(isEdit) {
      if (!isEdit) {
        this.fetchData();
      }
      this.fetchData();
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNumber - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>

<style lang="scss" scoped>
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 200px;
}
.flex {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.el-tag {
  margin-right: 10px;
  margin-bottom: 5px;
}
.display-config-layout {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  & > div {
    width: calc(50% - 30px);
    flex: none;
    margin-left: 0;
    margin-right: 0;
  }
  & > div:nth-child(odd) {
    margin-right: 50px;
  }
}
</style>
