<template>
  <div>
    <xy-dialog ref="dialog" :width="40" :is-show.sync="isShow" :type="'form'"
      :title="type === 'add' ? '新增教师信息' : '编辑教师信息'" :form="form" :rules='rules' @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>姓名：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" placeholder="请输入姓名" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:sex>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>性别：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.sex" placeholder="请选择" clearable style="width: 100%;">
              <el-option v-for="item in sex_options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:mobile>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>联系方式：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.mobile" placeholder="请输入联系方式" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:introduce>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>介绍：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.introduce" type="textarea" placeholder="请输入介绍" clearable
              style="width: 100%;"></el-input>

          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/info/teachers.js"
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        action: '',
        id: '',
        form: {
          name: '',
          sex: '',
          mobile:'',
          introduce: ''
        },
        sex_options:[{
          label:'男',
          value:'男'
        },{
          label:'女',
          value:'女'
        }],
        rules: {
          name:[{
            required:true,
            message:'请输入姓名'
          }],
          sex:[{
            required:true,
            message:'请选择性别'
          }],
          mobile:[{
            required:true,
            message:'请输入联系方式'
          }]
        }
      }
    },
    created() {},
    methods: {
      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        save(this.form).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增教师成功' : '编辑教师成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.requestToForm(res,this.form)
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .name,
  ::v-deep .sex,
  ::v-deep .introduce {
    flex-basis: 100%;
  }
</style>
