<!--导游信息-->
<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @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
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" type="index"/>
      <el-table-column label="姓名" prop="name"/>
      <el-table-column label="电话" prop="phone"/>
      <el-table-column label="级别" prop="level"/>
      <el-table-column label="语种" prop="language"/>
      <el-table-column label="费用" prop="cost"/>
      <el-table-column label="描述" prop="description"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleAppointmentAdd(scope.row.id)">预约</el-button>
          <el-button type="text" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button type="text" @click="handleRemove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"/>
    <el-dialog :title="title" :visible.sync="open" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="照片" prop="photo">
          <image-upload v-model="form.photo" :limit="1"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"/>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level"/>
        </el-form-item>
        <el-form-item label="语种" prop="language">
          <el-input v-model="form.language"/>
        </el-form-item>
        <el-form-item label="费用" prop="cost">
          <el-input v-model="form.cost"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea"/>
        </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="appointmentOpen" append-to-body width="600px">
      <el-form ref="appointmentForm" :model="appointmentForm" :rules="appointmentRules" label-width="80px">
        <el-form-item label="起止时间">
          <el-date-picker
            v-model="appointmentTime"
            end-placeholder="结束时间"
            range-separator="-"
            start-placeholder="开始时间"
            style="width: 100%"
            type="datetimerange"
            :default-time="['08:00:00', '12:00:00']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="appointmentForm.phone"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="appointmentAddSubmit">确 定</el-button>
        <el-button @click="appointmentOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {add, appointmentAdd, detail, edit, page, remove} from '@/api/liandao/tourGuide.js'

export default {
  name: "TourGuideInfo",
  created() {
    this.getList();
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
      },
      //表格数据
      tableData: [],
      // 总数
      total: 0,
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      open: false,
      rules: {
        name: [{required: true, message: "姓名不能为空", trigger: "blur"},],
        photo: [{required: true, message: "照片不能为空", trigger: "blur"},],
      },
      appointmentForm: {},
      appointmentOpen: false,
      appointmentTime:[],
      appointmentRules:{},
    }
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      page(this.queryParams).then(resp => {
          if (resp.code == 200) {
            this.tableData = resp.rows;
            this.total = resp.total;
            this.loading = false;
          }
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        photo: undefined,
        description: undefined,
        phone: undefined,
        level: undefined,
        language: undefined,
        cost: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleEdit(id) {
      this.reset();
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.open = true;
          this.title = "修改";
        }
      })
    },
    handleRemove(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id).then(resp => {
          if (resp.code === 200) {
            this.$message.success('删除成功！')
            this.resetQuery()
          }
        })
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            edit(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          } else {
            add(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAppointmentAdd(id) {
      this.appointmentForm = {
        tourGuideId: id,
        startTime: undefined,
        endTime: undefined,
        phone: undefined,
      };
      this.resetForm("appointmentForm");
      this.appointmentOpen = true
    },
    appointmentAddSubmit() {
      if(this.appointmentTime && this.appointmentTime.length > 0){
        this.appointmentForm.startTime = this.appointmentTime[0]
        this.appointmentForm.endTime = this.appointmentTime[1]
      }
      this.$refs["appointmentForm"].validate(valid => {
        if (valid) {
          appointmentAdd(this.appointmentForm).then(resp => {
            if (resp.code === 200) {
              this.$message.success("预约成功");
              this.appointmentOpen = false;
            } else {
              this.$message.error(resp.msg);
            }
          })
        }
      })
    }
  }
}
</script>
