<template>
  <div>
    <el-button size="mini" type="primary" @click="dialogFormVisible = true">转 工 单</el-button>
    <el-drawer append-to-body :show-close="false" :visible.sync="dialogFormVisible" size="500px">
      <div slot="title" style="font-size: 15px">
        <span style="margin-right:16px">转工单</span>
        <workProgress class="inline-16" />
      </div>
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="right" size="mini">
            <!-- <el-col :span="24">
              <el-form-item label="工单类型" prop="type">
                <el-select class="inline-24" v-model="form.type" placeholder="请选择模板" size="small" clearable>
                  <el-option v-for="(item,index) in serveClassList" :key="index" :label="item.value" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item label="工单模板" prop="sa_workorder_templateid">
                <el-select class="inline-24" @change="modelChange" v-model="form.sa_workorder_templateid" placeholder="请选择模板" size="small" clearable>
                  <el-option v-for="(item,index) in serveModelList" :key="index" :label="item.name" :value="item.sa_workorder_templateid"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="工单类型" prop="type">
                <el-input size="mini" type="text" placeholder="工单类型" :disabled="true" v-model="form.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="负责人" prop="projectlearders">
                <el-select class="inline-24" v-model="form.projectlearders" placeholder="请选择负责人" size="small" clearable>
                  <el-option v-for="(item,index) in leaderList" :key="index" :label="item.name" :value="item.userid"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import workProgress from './workProgress'
export default {
  name: "add",
  components:{workProgress},
  data(){
    return {
      dialogFormVisible:false,
      serveModelList:[],
      serveClassList:[],
      leaderList:[],
      form:{
        type:'',
        sa_workorder_templateid:'',
        projectlearders:'',
        sa_serviceorderid:''
      },
      rules:{
        sa_workorder_templateid:[
          { required: true, message: '请选择服务模板', trigger: 'blur'},
        ],
        projectlearders:[
          { required: true, message: '请选择负责人', trigger: 'blur'},
        ],
        type:[
          { required: true, message: '请选择服务类型', trigger: 'blur'},
        ],
      }
    }
  },
  created () {
    this.getServeModel()
    this.getLeaderList()
  },
  methods:{
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        
        this.form.sa_serviceorderid = this.$route.query.id
        typeof this.form.projectlearders == 'number' ? this.form.projectlearders = [this.form.projectlearders] : ''
        const res = await this.$api.requested({
          "id": "20230207091003",
          "version":1,
          "content": this.form
        })
        console.log(res);
        
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
          if (this.$route.path !== '/workorderDetail') {
            this.oldRoute = {path:this.$route.path,query:this.$route.query}
            this.$store.dispatch('setHistoryRouter',this.oldRoute)
          }
          this.$router.push({
            path:'/workorderDetail',
            query:{
              id:res.data,
            }
          })
        })
      })
    },
    modelChange (data) {
      this.form.type = this.serveModelList.find(item => item.sa_workorder_templateid == data).type   
    },
    async getServeModel () {
      let res = await this.$api.requested({
        "id":20230206155803,
        "content": {
        }
      })
      this.serveModelList = res.data
      console.log(this.serveModelList);
    },
    async getLeaderList () {
      let res = await this.$api.requested({
        "id":20230213143003,
        "content": {
          isleader:0
        }
      })
      this.leaderList = res.data
      console.log(this.leaderList);
      
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  margin-top: 0;
}
.el-select {
  width:100% !important;
}
</style>