<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:suozaiduiwu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>所在队伍：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.suozaiduiwu" style="width:300px" @change="changeTeam" placeholder="请选择">
                <el-option
                  v-for="item in listTeam"
                  :key="item.id"
                  :label="item.mingcheng"
                  :value="item.mingcheng">
                </el-option>
              </el-select>
          </div>
        </div>
      </template>
      <template v-slot:xingming>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>姓名：
          </div>
          <div class="xy-table-item-content">
             <el-input style="width:300px" v-model="form.xingming"></el-input>

          </div>
        </div>
      </template>
      <template v-slot:lianxidianhua>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>联系电话：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.lianxidianhua"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:gongzhong>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>工种：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.gongzhong"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:beizhu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>备注：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" type="textarea" rows="8" v-model="form.beizhu"></el-input>
          </div>
        </div>
      </template>
    </xy-dialog>

  </div>
</template>

<script>
  import {
    save,
    show,
    index
  } from "@/api/system/baseForm.js"
  import {
    Message
  } from 'element-ui'
  export default {

    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        tableName:'books',
        listTeam:[],
        form: {
          team_id:'',
          suozaiduiwu:'',
          xingming:'',
          lianxidianhua:'',
          gongzhong:'',
          beizhu:''
        },
        rules: {
          suozaiduiwu:[{
            required:true,
            message:'请选择队伍'
          }]
        },

      }
    },
    created() {

    },
    methods: {

      async getDetail() {
        const res = await show({
          id: this.id,
          table_name: this.tableName
        })
        this.$integrateData(this.form,res)
      },
      setlistTeam(e){
        this.listTeam = e
      },
      async getTeamList(){
        if(this.listTeam.length>0){

          return
        }
        const res = await index({
          page: 1,
          page_size: 10,
          table_name: 'teams'
        })
        this.listTeam = res.data
      },
      changeTeam(e){
        console.log(e)
        if(e){
          this.listTeam.map(item=>{
            if(item.mingcheng===e){
              this.form.team_id = item.id
            }
          })
        }else{
          this.form.team_id = ''
        }
      },
      submit() {
        if (this.type === 'add') {
          save({
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '新增成功'
            })
            this.$emit('refresh')
            this.isShow = false

          })
          return
        }
        if (this.type === 'editor') {
          save({
            id: this.id,
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '编辑成功'
            })
            this.$emit('refresh')
            this.isShow = false
          })
        }
      },

    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .xy-table-item-label{
    min-width:160px!important
  }
</style>
