<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 考试端账号管理 </el-col>
          <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="姓名">
                    <el-input v-model="searchFrom.name"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系电话">
                    <el-input v-model="searchFrom.phone"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8" class="btn">
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                  <el-button type="primary" size="mini" @click="show = true">添加</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="24" class="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @edit="toEdit"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog title="账号信息管理" :visible.sync="show" width="50%" :before-close="close" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="直属单位编号" prop="department_code">
          <el-select v-model="form.department_code" clearable placeholder="请选择" style="width: 100%" @change="deptChange" filterable>
            <el-option v-for="(item, index) in deptlsit" :key="index" :label="item.name" :value="item.num"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮件"></el-input>
        </el-form-item>
        <el-form-item label="工作单位" prop="company">
          <el-input v-model="form.company" placeholder="请输入工作单位"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
        </el-form-item>
        <el-form-item>
          <p style="color: #ff0000">账号初始密码为：123456；请用户登录系统自行修改；</p>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: police_department } = createNamespacedHelpers('police_department');
const { mapActions: exam_account } = createNamespacedHelpers('exam_account');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '直属单位编号', model: 'department_code' },
        { label: '直属单位名称', model: 'department_name' },
        { label: '姓名', model: 'name' },
        { label: '联系电话', model: 'phone' },
        { label: '电子邮件', model: 'email' },
        { label: '工作单位', model: 'company' },
      ],
      opera: [{ label: '维护', method: 'edit' }],
      list: [],
      total: 0,
      // 添加角色
      show: false,
      form: {},
      rules: {
        department_code: [{ required: true, message: '请选择', trigger: 'change' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        email: [{ required: false, message: '请输入电子邮件', trigger: 'blur' }],
        company: [{ required: false, message: '请输入工作单位', trigger: 'blur' }],
      },
      // 部门列表
      deptlsit: [],
      roleList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...police_department({ depatQuery: 'query' }),
    ...exam_account(['query', 'create', 'update']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 修改信息
    toEdit({ data }) {
      this.$set(this, `form`, data);
      this.show = true;
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          if (data.id) {
            let res = await this.update(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `修改信息成功` });
              this.search();
              this.close();
            }
          } else {
            data.password = '123456';
            let res = await this.create(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `创建信息成功` });
              this.search();
              this.close();
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 选择部门
    deptChange(value) {
      let p1 = this.deptlsit.find((i) => i.num == value);
      if (p1) this.$set(this.form, `department_name`, p1.name);
    },
    // 取消添加
    close() {
      this.form = {};
      this.show = false;
      this.search();
    },
    async searchOther() {
      let res = await this.depatQuery();
      if (this.$checkRes(res)) {
        this.$set(this, `deptlsit`, res.data);
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_1 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
/deep/.el-dialog__body {
  padding: 10px;
}
/deep/.el-form-item {
  margin: 0 0 10px 0;
}
</style>
