<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="instructorName">
        <el-input
          v-model="queryParams.instructorName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾考车型 " prop="driveCardType">
        <el-select v-model="queryParams.driveCardType" placeholder="请选择驾考车型 " clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.Drive_card_type)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科目类型 " prop="subjectType">
        <el-select v-model="queryParams.subjectType" placeholder="科目类型 " clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.Drive_course_subject)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolCoachList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="instructorName" />
      <el-table-column label="照片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>

<!--      <el-table-column label="年龄" align="center" prop="age" />-->
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="联系电话" align="center" prop="phone" />
<!--      <el-table-column label="学员人数" align="center" prop="student" />-->
      <el-table-column label="车牌" align="center" prop="carId" />
<!--      <el-table-column label="学员人数" align="center" prop="indenty" />-->
      <el-table-column label="家庭住址" align="center" prop="address" />
<!--      <el-table-column label="平均星级" align="center" prop="averageStar" />-->
      <el-table-column label="驾龄" align="center" prop="experienceYear" />
      <el-table-column label="个人简介" align="center" prop="instructorDesc">
        <template slot-scope="scope">
          <el-button @click="viewContentFun(scope.row.instructorDesc)" type="text" >查看</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="驾驶年龄" align="center" prop="drivingYear" />-->
      <el-table-column label="驾考车型 " align="center" prop="driveCardType">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.Drive_card_type" :value="scope.row.driveCardType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="科目类型 " align="center" multiple prop="subjectTypeList">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.drive_course_subject" :value="scope.row.subjectTypeList"/>
        </template>
      </el-table-column>-->

<!--      <el-table-column label="科目类型" align="center" prop="subjectType" />-->
<!--      <el-table-column label="科目类型" align="center" >
        <template slot-scope="scope">
         <div>{{scope.row.subjectType}}</div>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改驾校教练对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="instructorName">
          <el-input v-model="form.instructorName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="照片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>

<!--        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>-->
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input type="number" v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="身份证号" prop="indenty">
          <el-input v-model="form.indenty" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="车牌" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车牌" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入家庭住址" />
        </el-form-item>
<!--        <el-form-item label="学员人数" prop="student">
          <el-input v-model="form.student" placeholder="输入学员人数" />
        </el-form-item>-->
<!--        <el-form-item label="平均星级" prop="averageStar">
          <el-input v-model="form.averageStar" placeholder="请输入平均星级" />
        </el-form-item>-->
        <el-form-item label="驾龄" prop="experienceYear">
          <el-input v-model="form.experienceYear" placeholder="请输入驾龄" />
        </el-form-item>
<!--        <el-form-item label="驾驶年龄" prop="drivingYear">
          <el-input v-model="form.drivingYear" placeholder="请输入驾驶年龄" />
        </el-form-item>-->
        <el-form-item label="驾考车型 " prop="driveCardType">
          <el-select v-model="form.driveCardType" placeholder="请选择驾考车型 ">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.Drive_card_type)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科目类型 " prop="subjectTypeList">
          <el-select v-model="form.subjectTypeList" multiple placeholder="请选择科目类型 ">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.Drive_course_subject)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="个人简介">
          <editor v-model="form.instructorDesc" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog  title="查看" :visible.sync="viewFlag" width="65%" append-to-body>
      <div style="box-sizing: border-box;padding: 15px;overflow: hidden" v-html="instructorDesc"></div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchoolCoach, getSchoolCoach, delSchoolCoach, addSchoolCoach, updateSchoolCoach } from "./api/schoolCoach";
//import CouponTemplate from "@/views/system/inspection/PartnerManagement/couponTemplate";
import editor from '@/components/Editor/index.vue'
export default {
  name: "SchoolCoach",
  // components: {CouponTemplate},
  components: {editor},
  data() {
    return {
      viewFlag:false,
      instructorDesc:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾校教练表格数据
      schoolCoachList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        subjectType:'',
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        image: null,
        instructorName: null,
        age: null,
        sex: null,
        averageStar: null,
        experienceYear: null,
        instructorDesc: null,
        drivingYear: null,
        driveCardType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        instructorName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        indenty: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        carId: [
          { required: true, message: "车牌不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "家庭住址不能为空", trigger: "blur" }
        ],
        riveCardType: [
          { required: true, message: "驾证类型不能为空", trigger: "blur" }
        ],
        driveCardType: [
          { required: true, message: "驾驶证类型不能为空", trigger: "blur" }
        ],
        subjectTypeList: [
          { required: true, message: "科目类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    viewContentFun(data){
      this.instructorDesc = data;
      this.viewFlag = true
    },
    /** 查询驾校教练列表 */
    getList() {
      this.loading = true;
      listSchoolCoach(this.queryParams).then(response => {
        this.schoolCoachList = response.data;
       // this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        image: null,
        instructorName: null,
        age: null,
        sex: null,
        averageStar: null,
        experienceYear: null,
        instructorDesc: null,
        drivingYear: null,
        driveCardType: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加驾校教练";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchoolCoach(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改驾校教练";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchoolCoach(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchoolCoach(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除驾校教练编号为"' + ids + '"的数据项？').then(function() {
        return delSchoolCoach(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drivingSchool/system/schoolCoach/export', {
        ...this.queryParams
      }, `schoolCoach_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
