<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item  label="课程类型" prop="type">
        <el-select v-model="queryParams.name" placeholder="请选择课程类型" clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.Cource_type)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程名字" prop="automatic">
        <el-input
          v-model="queryParams.automatic"
          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-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="driveSchoolCourseList" @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="name">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.Cource_type" :value="scope.row.name"/>
        </template>
      </el-table-column>

<!--      <el-table-column label="课程类型" align="center" prop="name" />-->
      <el-table-column label="课程名字" align="center" prop="automatic" />
<!--      <el-table-column label="课程开始时间" align="center" prop="dayStart" />
      <el-table-column label="课程结束时间" align="center" prop="dayEnd" />-->
<!--      <el-table-column label="价格" align="center" :prop="price" />-->
      <el-table-column label="价格" align="center" >
        <template #default="data">
          <div>{{data.row.price}}￥</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="定金" align="center" prop="reserveMoney" />-->
      <el-table-column label="定金" align="center" >
        <template #default="data">
          <div>{{data.row.reserveMoney}}￥</div>
        </template>
      </el-table-column>

      <el-table-column label="交付定金的优惠：" align="center" >
        <template #default="data">
          <div>{{data.row.favour}}￥</div>
        </template>
      </el-table-column>

      <el-table-column label="封面图" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="课程简介" align="center" prop="describ">
      <template slot-scope="scope">
        <el-button @click="viewContentFun(scope.row.describ)" type="text" slot="reference">查看</el-button>
      </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="750" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">


        <el-form-item label="课程类型" prop="name">
          <el-radio-group v-model="form.name">
            <el-radio
              v-for="dict in this.getDictDatas(DICT_TYPE.Cource_type)"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="课程名字" prop="automatic">
          <el-input v-model="form.automatic" placeholder="请输入课程名字" />
        </el-form-item>

<!--        <el-form-item label="上课时间" prop="weekDay">
          <el-checkbox-group :max="2"  v-model="form.weekDay">
            <el-checkbox label="星期一">星期一</el-checkbox>
            <el-checkbox label="星期二">星期二</el-checkbox>
            <el-checkbox label="星期三">星期三</el-checkbox>
            <el-checkbox label="星期四">星期四</el-checkbox>
            <el-checkbox label="星期五">星期五</el-checkbox>
            <el-checkbox label="星期六">星期六</el-checkbox>
            <el-checkbox label="星期日">星期日</el-checkbox>
          </el-checkbox-group>
        </el-form-item>-->
<!--        <el-form-item label="课程开始时间" prop="dayStart">
          <el-input v-model="form.dayStart" placeholder="请输入课程开始时间" />
        </el-form-item>
        <el-form-item label="课程结束时间" prop="dayEnd">
          <el-input v-model="form.dayEnd" placeholder="请输入课程结束时间" />
        </el-form-item>-->
        <el-form-item label="价格（元）" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
<!--        <el-form-item label="驾照类型" prop="automatic">
          <el-input v-model="form.automatic" placeholder="驾照类型" />
        </el-form-item>-->
        <el-form-item label="驾校优势" prop="license">
          <el-input v-model="form.license" placeholder="驾校优势" />
        </el-form-item>

        <el-form-item label="定金（元）" prop="reserveMoney">
          <el-input v-model="form.reserveMoney" placeholder="请输入定金" />
        </el-form-item>

        <el-form-item label="交付定金优惠（元）:" prop="favour">
          <el-input v-model="form.favour" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="封面图" prop="photo">
          <image-upload :limit="1" v-model="form.photo"/>
        </el-form-item>
        <el-form-item label="课程简介">
          <editor v-model="form.describ" :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="viewContent"></div>
    </el-dialog>
  </div>
</template>

<script>
import { listDriveSchoolCourse, getDriveSchoolCourse, delDriveSchoolCourse, addDriveSchoolCourse, updateDriveSchoolCourse } from "./api/driveSchoolCourse";
import editor from '@/components/Editor/index.vue'
export default {
  name: "DriveSchoolCourse",
  components: {editor},
  data() {
    return {
      viewFlag:false,
      viewContent:"",
      weekDay:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾校课程分类表格数据
      driveSchoolCourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        type: null,
        name: null,
        dayStart: null,
        dayEnd: null,
        price: null,
        automatic: null,
        license: null,
        photo: null,
        describ: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "驾驶证类型不能为空", trigger: "blur" }
        ],
        automatic: [
          { required: true, message: "课程名字不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "课程名字不能为空", trigger: "blur" }
        ],
        price: [
          { required: true,  trigger: 'blur', message:'金额不能为空'},
        ],
        reserveMoney: [
          { required: true, trigger: 'blur', message:'合同金额不能为空'},
        ],
        photo: [
          { required: true, message: "封面图不能为空", trigger: "blur" }
        ],
        license: [
          { required: true, message: "封面图不能为空", trigger: "blur", size: 5 }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    viewContentFun(data){
      this.viewContent =data;
      this.viewFlag = true;
    },
    /** 查询驾校课程分类列表 */
    getList() {
      this.loading = true;
      listDriveSchoolCourse(this.queryParams).then(response => {
        this.driveSchoolCourseList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        type: null,
        name: null,
        dayStart: null,
        dayEnd: null,
        price: null,
        automatic: null,
        license: null,
        reserveMoney: null,
        weekDay:[],
        photo: null,
        describ: 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.weekDay = []
      this.reset();
      this.open = true;
      this.title = "添加驾校课程分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDriveSchoolCourse(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) {
            updateDriveSchoolCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDriveSchoolCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
        if (response.data.weekDay){
          this.weekDay = response.data.weekDay.split(",")
        }else {
          this.weekDay = []
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除驾校课程为"' + row.name + '"的数据项？').then(function() {
        return delDriveSchoolCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drivingSchool/system/driveSchoolCourse/export', {
        ...this.queryParams
      }, `driveSchoolCourse_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
