<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
        v-model="page.data.qqqqq"
        filterable
        clearable
        placeholder="请选择所属部门"
        class="margin-left input"
      >
        <el-option
          v-for="item in department_list"
          :key="item.code"
          :label="item.desc"
          :value="item.code"
        />
      </el-select>
      <el-input
        v-model="page.data.yyyyy"
        placeholder="请输入单位全称"
        clearable
        class="margin-left input"
      />
      <el-input
        v-model="page.data.eeeee"
        placeholder="请输入姓名"
        clearable
        class="margin-left input"
      />
      <el-input
        v-model="page.data.ttttt"
        placeholder="请输入手机号"
        clearable
        class="margin-left input"
      />
      <el-input
        v-model="page.data.rrrrr"
        placeholder="请输入身份证号"
        clearable
        class="margin-left input"
      />
      <el-select
        v-model="page.data.qqqqq"
        filterable
        clearable
        placeholder="请选择实名状态"
        class="margin-left input"
      >
        <el-option
          v-for="item in type_list"
          :key="item.code"
          :label="item.desc"
          :value="item.code"
        />
      </el-select>
      <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-image
            v-if="scope.row.avatar"
            class="table-img"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          />
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="实名照片" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.photo"
            class="table-img"
            :src="scope.row.photo"
            :preview-src-list="[scope.row.photo]"
          />
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="实名状态 " align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
          <el-tag v-if="scope.row.content === 1" type="info">未实名</el-tag>
          <el-tag v-if="scope.row.content === 1" type="success">已实名</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </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.birthdayQuotesId)"
          >删除</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="80px"
        label-position="left"
      >
        <div class="flex">
          <el-form-item label="所属部门" class="form-item-style" prop="aaaaa">
            <el-select
              v-model="form.aaaaa"
              filterable
              clearable
              multiple
              collapse-tags
              placeholder="请选择所属部门"
              style="width: 100%"
            >
              <el-option
                v-for="item in department_list"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="姓名" class="form-item-style" prop="bbbbb">
            <el-input v-model.trim="form.bbbbb" placeholder="请输入姓名" />
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item label="身份证" class="form-item-style" prop="sssss">
            <el-input
              v-model.trim="form.sssss"
              placeholder="请输入人员身份证"
            />
          </el-form-item>
          <el-form-item label="手机号" class="form-item-style" prop="ddddd">
            <el-input
              v-model.trim="form.ddddd"
              maxlength="11"
              show-word-limit
              placeholder="请输入人员手机号"
            />
          </el-form-item>
        </div>
        <el-form-item label="证件照" prop="ggggg" class="form-item-style">
          <UploadImg :image="form.ggggg" type="ggggg" @getImg="getImg" />
        </el-form-item>
      </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 UploadImg from '@/components/UploadImg'
import {
  birthdayQuotesDel,
  birthdayQuotesEdit,
  birthdayQuotesList
} from '@/api/account'
export default {
  components: { Pagination, UploadImg },
  filters: {},
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          qqqqq: null,
          eeeee: null,
          rrrrr: null,
          ttttt: null,
          yyyyy: null
        },
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: '添加人员',
        update: '编辑人员'
      },
      dialogType: 'add',
      form_copy: null,
      // 添加角色参数
      form: {
        aaaaa: null,
        bbbbb: null,
        sssss: null,
        ddddd: null,
        ggggg: null
      },
      // 验证规则
      rules: {
        aaaaa: [{ required: true, message: '请选择', trigger: 'change' }],
        ggggg: [{ required: true, message: '请选择', trigger: 'change' }],
        bbbbb: [{ required: true, message: '请输入', trigger: 'change' }],
        sssss: [{ required: true, message: '请输入', trigger: 'change' }],
        ddddd: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      type_list: [
        { id: 1, name: '未实名' },
        { id: 2, name: '已实名' }
      ],
      department_list: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
    this.form_copy = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    // 获取列表数据
    fetchData() {
      // this.listLoading = true;
      // birthdayQuotesList(this.page).then((res) => {
      //   this.list = res.data.records;
      //   this.total = res.data.total;
      this.listLoading = false
      // });
    },
    // 打开添加角色对话框
    create(type, row) {
      if (type === 'add') {
        this.form = JSON.parse(JSON.stringify(this.form_copy))
      } else {
        this.form = Object.assign(this.form, row)
      }
      this.dialogType = type
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    // 确定添加/编辑
    addOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          birthdayQuotesEdit(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(() => {
        birthdayQuotesDel(id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    // 图片赋值
    getImg(val, index, type) {
      this.form[type] = val
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1
    }
  }
}
</script>
<style scoped lang="scss">
</style>
