<template>
  <div class="app-container">
    <!--操作区-->
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="margin-bottom"
      @click="create('add')"
    >添加账号</el-button>
    <!--搜索区-->
    <div class="float">
      <!-- <el-select
        filterable
        clearable
        v-model="page.data.adminType"
        placeholder="请选择账号类型"
        class="margin-left input m-b-10"
      >
        <el-option
          v-for="item in type_list"
          :key="item.code"
          :label="item.desc"
          :value="item.code"
        >
        </el-option>
      </el-select>
      <el-select
        filterable
        clearable
        v-model="page.data.wwwww"
        placeholder="请选择账号"
        class="margin-left input m-b-10"
      >
        <el-option
          v-for="item in account_list"
          :key="item.code"
          :label="item.desc"
          :value="item.code"
        >
        </el-option>
      </el-select> -->
      <el-input
        v-model="page.data.manageName"
        placeholder="请输入姓名"
        clearable
        class="margin-left input"
      />
      <el-input
        v-model="page.data.managePhone"
        placeholder="请输入手机号"
        clearable
        class="margin-left input"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        class="margin-left"
        @click="fetchData"
      >搜索</el-button>
    </div>
    <!--主表格-->
    <el-table
      ref="table_form"
      v-loading="listLoading"
      class="table"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="账号类型" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.adminType === 1"
            type="primary"
          >机场公司</el-tag>
          <el-tag
            v-if="scope.row.adminType === 2"
            type="success"
          >机场公安局</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="账号名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="管理员姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.manageName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员手机号 " align="center">
        <template slot-scope="scope">
          {{ scope.row.managePhone }}
        </template>
      </el-table-column>
      <el-table-column label="关联机场公安局" align="center">
        <template slot-scope="scope">
          {{ scope.row.linkName }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="200">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteFun(scope.row.adminId)"
          >删除</el-button>
          <el-button
            type="primary"
            size="small"
            @click="create('update', scope.row)"
          >编辑</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[dialogType]"
      :visible.sync="dialogVisible"
      width="900px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        label-position="left"
      >
        <div class="flex">
          <el-form-item
            label="账号类型"
            class="form-item-style"
            prop="adminType"
          >
            <el-select
              v-model="form.adminType"
              :disabled="dialogType == 'update'"
              filterable
              clearable
              placeholder="请选择账号类型"
              style="width: 100%"
            >
              <el-option
                v-for="item in type_list"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="账号名称"
            class="form-item-style"
            prop="username"
          >
            <el-input
              v-model.trim="form.username"
              :disabled="dialogType === 'update'"
              placeholder="请输入账号名称"
            />
          </el-form-item>
        </div>
        <el-form-item label="账号密码" class="form-item-style" prop="password">
          <el-input
            v-model.trim="form.password"
            placeholder="请输入账号密码"
            show-password
          />
        </el-form-item>
        <el-form-item
          label="管理员姓名"
          class="form-item-style"
          prop="manageName"
        >
          <el-input
            v-model.trim="form.manageName"
            maxlength="20"
            show-word-limit
            placeholder="请输入管理员姓名"
          />
        </el-form-item>
        <div class="flex">
          <el-form-item
            label="管理员手机号"
            class="form-item-style"
            prop="managePhone"
          >
            <el-input
              v-model.trim="form.managePhone"
              maxlength="11"
              show-word-limit
              placeholder="请输入管理员手机号"
            />
          </el-form-item>
          <el-form-item
            v-if="form.adminType == 1"
            label="关联账号"
            class="form-item-style"
            prop="linkId"
          >
            <el-select
              v-model="form.linkId"
              filterable
              clearable
              placeholder="请选择关联的机场公安局"
              style="width: 100%"
            >
              <el-option
                v-for="item in linkList"
                :key="item.adminId"
                :label="item.username"
                :value="item.adminId"
              />
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {
  adminDelete,
  adminEdit,
  getPageList,
  linkList
} from '@/api/user_manage'
export default {
  components: { Pagination },
  filters: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          // adminType: null,
          manageName: null,
          managePhone: null
        },
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: '添加账号',
        update: '编辑账号'
      },
      dialogType: 'add',
      form_copy: null,
      // 添加角色参数
      form: {
        adminType: null,
        username: null,
        password: null,
        manageName: null,
        managePhone: null,
        linkId: null
      },
      // 验证规则
      rules: {
        adminType: [{ required: true, message: '请选择', trigger: 'change' }],
        linkId: [{ required: true, message: '请选择', trigger: 'change' }],
        username: [{ required: true, message: '请输入', trigger: 'change' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern:
              /^(((?=.*\d)(?=.*[a-z])(?=.*[A-Z]))|((?=.*\d)(?=.*[a-z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))).{10,32}$/,
            message:
              '必须大小写字母、数字、特殊字符,4种组合中至少满足3种，且密码长度在10-32位之间'
          }
        ],
        manageName: [{ required: true, message: '请输入', trigger: 'change' }],
        managePhone: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      type_list: [
        { id: 1, name: '机场公司' },
        { id: 2, name: '机场公安局' }
      ],
      linkList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
    this.getLinkList()
    this.form_copy = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true
      getPageList(this.page).then((res) => {
        this.list = res.data.records
        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 可关联账号列表
    getLinkList() {
      linkList(this.page).then((res) => {
        this.linkList = res.data
      })
    },
    // 打开添加角色对话框
    create(type, row) {
      if (type === 'add') {
        this.form = JSON.parse(JSON.stringify(this.form_copy))
      } else {
        this.form = Object.assign(this.form, row)
        this.form.password = ''
      }
      this.dialogType = type
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          adminEdit(this.form).then((res) => {
            this.dialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          })
        }
      })
    },
    // 删除角色
    deleteFun(id) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        adminDelete(id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1
    }
  }
}
</script>
<style scoped lang="scss">
</style>
