<template>
  <el-dialog
    width="960px"
    :visible.sync="open"
    :title="title"
    @close="handleClose"
  >
    <el-form ref="queryForm" inline :model="queryForm">
      <!--<el-form-item label="工作编号" prop="teacherCode">-->
      <!--  <el-input-->
      <!--    v-model="queryForm.teacherCode"-->
      <!--    placeholder="请输入工作编号"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->

      <el-form-item label="教师姓名" prop="teacherName">
        <el-input
          v-model="queryForm.teacherName"
          placeholder="请输入老师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师电话" prop="teacherTel">
        <el-input
          v-model="queryForm.teacherTel"
          placeholder="请输入老师电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="teacherTable"
      v-loading="loading"
      stripe
      height="380px"
      width="100%"
      :data="teacherList"
      @selection-change="handleSelectChange"
      @row-click="handleRowClick"
    >
      <el-table-column type="selection" align="center"></el-table-column>
      <!--<el-table-column type="index" label="序号" width="55" align="center"></el-table-column>-->
      <!--<el-table-column label="工作编号" align="center" prop="teacherCode"/>-->
      <el-table-column label="头像" width="100">
        <template slot-scope="scope">
          <!--          <ListImage :image-url="baseUrl + scope.row.avatar"></ListImage>-->
          <ListImage :image-url="isEmptyTeacherUrl(scope.row.picUrl, scope.row.sex)"></ListImage>
        </template>
      </el-table-column>
      <el-table-column label="老师名称" align="center" prop="teacherName" width="120" show-overflow-tooltip/>
      <el-table-column label="性别" align="center" prop="sex" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="老师电话" width="160" align="center" prop="teacherTel"/>
      <el-table-column label="家庭住址" align="center" prop="homeAddress" show-overflow-tooltip/>
      <!--<el-table-column label="身份证号" width="180" align="center" prop="idCard"/>-->
    </el-table>
    <ExPagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      @pagination="getList"
    ></ExPagination>
    <div slot="footer">
      <el-button
        type="primary"
        size="small"
        :disabled="!isSimple"
        @click="handleSelect">确 定
      </el-button>
      <el-button
        plain
        size="small"
        @click="handleClose">取 消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listInform} from "@/api/smartSchool/teacherManage/teacherInformation.js";
import ListImage from "@/components/ListImage";
import {Gender} from "../../../../enums/common";

export default {
  name: "TeacherSelect",
  dicts: ['sys_user_sex'],
  components: {
    ListImage
  },
  props: {
    // 打开对话框
    openSelect: {
      required: true,
      type: Boolean,
      default: false
    },
    // 标题
    title: {
      required: false,
      type: String,
      default: '教师选择'
    },
    //
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        teacherTel: undefined,
        teacherName: '',
        teacherCode: undefined
      },
      total: 0,
      // 老师数据列表
      teacherList: [],
      // 是否为单个
      isSimple: false,
      // 选中项
      selectedItem: {},
      loading: false
    }
  },
  computed: {
    open: {
      get() {
        return this.openSelect;
      },
      set(value) {
        this.$emit('update:openSelect', value);
      }
    }
  },
  created() {
    this.resetQuery();
  },

  methods: {
    /** 列表查询 */
    getList() {
      this.loading = true;
      // listInform(this.queryParams).then(response => {
      //   this.teacherList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      this.$emit('getTeacherList', this.queryForm, (rows = [], total = 0) => {
        console.log('this.rows', this.rows)
        this.teacherList = rows;
        this.total = total;
        this.loading = false;
      });
    },

    /** 模糊查询 */
    handleQuery() {
      this.throttle(() => {
        this.getList();
      })
    },

    /** 重置表单 */
    resetQuery() {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        teacherTel: undefined,
        teacherName: '',
        teacherCode: undefined
      };
      this.isSimple = false;
      this.handleQuery();
    },

    /** 处理列表选中数据 */
    handleSelectChange(selections) {
      this.selectedItem = selections[0];
      this.isSimple = selections.length === 1;
    },

    /** 单击行选中 */
    handleRowClick(row, column, event) {
      this.$refs.teacherTable.clearSelection();
      this.$refs.teacherTable.toggleRowSelection(row, true);
    },
    /** 确定 */
    handleSelect() {
      this.$emit('setTeacherInfo', this.selectedItem);
      this.handleClose();
    },

    /** 关闭 */
    handleClose() {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        teacherTel: undefined,
        teacherName: '',
        teacherCode: undefined
      };
      this.open = false;
      this.isSimple = true;
      this.selectedItem = {};
    },
    /** 判断老师头像是否为空 */
    isEmptyTeacherUrl(imageUrl, sex) {
      if (!imageUrl) {
        let url = null;
        if (Gender.MALE === sex) {
          url = 'https://eos-wuxi-1.cmecloud.cn/dy-pro/image/2022/08/09/8a6c1334494d418d8a53cc7bf5abd668.jpg';
        } else {
          url = 'https://eos-wuxi-1.cmecloud.cn/dy-pro/image/2022/08/09/b076ed42bfca4a8cae062dfffd5f3618.jpg\'';
        }
        return url
      } else
        return this.baseUrl + imageUrl;
    }
  }
}
</script>

