<template>
  <div class="content-edit">
    <div class="edit-top">
      <div class="left-top">
        <img src="@/assets/images/back.png" @click="cancel()" />
        <div class="left-title">{{this.infoDialog.formData.id!=''?$t('page.business.study.nonTrial.xgsy'):$t('page.business.study.nonTrial.create')}}</div>
      </div>
      <div class="right-top">
        <el-button @click="cancel()">{{$t('page.business.study.nonTrial.cancel')}}</el-button>
        <el-button type="primary" @click="save(1)" plain v-if="!infoDialog.formData.status || infoDialog.formData.status===1">{{$t('page.business.study.nonTrial.save')}}</el-button>
        <el-button type="primary" @click="save(3)">{{$t('page.business.study.nonTrial.submit')}}</el-button>
      </div>
    </div>
    <div class="edit-content">
      <el-form ref="infoDialogForm" :model="infoDialog.formData" :rules="infoDialog.rules" label-width="150px" class="edit-form">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.nonTrial.name')+'：'" prop="name">
              <el-input v-model="infoDialog.formData.name" :placeholder="$t('form.placeholderInput')" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.nonTrial.sn')+'：'" prop="sn">
              <el-input v-model="infoDialog.formData.sn" :placeholder="$t('form.placeholderInput')" maxlength="100" :disabled="this.infoDialog.formData.id!=''" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.nonTrial.leader')+'：'" prop="leader">
              <SelectDeptUser @change="changeLeader" v-model="infoDialog.formData.leader" :name="infoDialog.formData.leaderName" :nameEn="infoDialog.formData.leaderNameEn"  />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('page.business.study.nonTrial.remark')+'：'" prop="remark">
              <el-input type="textarea" :rows="5" :placeholder="$t('form.placeholderInput')" v-model="infoDialog.formData.remark" maxlength="500" show-word-limit> </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <Sign ref="editSignRef" @callback="doSign" />
  </div>
</template>

<script>
import { nonTrial_info,nonTrial_save} from "@/api/business/form/nonTrial";
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser.vue';
import Sign from './sign.vue'
const EmptyDialogData = {
  id:'',
  name:'',
  sn:'',
  leader:'',
  leaderName:'',
  leaderNameEn:'',
  reamrk:'',
}
export default {
  name: "StudyEdit",
  props:{
  },
  components:{
    SelectDeptUser,
    Sign
  },
  computed: {
  },
  filters:{
  },
  data() {
    return {
      infoDialog:{
        visible:false,
        formData:{},
        rules:{
          name: [
            { required: true, message: this.$t('page.business.study.nonTrial.inputName'), trigger: "blur" }
          ],
          sn: [
            { required: true, message: this.$t('page.business.study.nonTrial.inputSn'), trigger: "blur" }
          ],
          leader: [
            { required: true, message: this.$t('page.business.study.nonTrial.inputLeader'), trigger: "change" }
          ],
        }
      },
      oldStatus:1
    };
  },
  created() {
  },
  methods: {
    changeLeader(val){
      this.infoDialog.formData.leader = val.id
      this.infoDialog.formData.leaderName = val.name
      this.infoDialog.formData.leaderNameEn = val.nameEn
      this.$refs.infoDialogForm.clearValidate('leader')
    },
    edit(row) {
      this.$refs['infoDialogForm'] && this.$refs['infoDialogForm'].resetFields()
      this.infoDialog.formData = _.merge({}, EmptyDialogData,{leader:this.$store.getters.id,leaderName:this.$store.getters.nickName,leaderNameEn:this.$store.getters.name})
      this.oldStatus = 1
      if(row && row.id){
        this.$modal.loading()
        nonTrial_info({id:row.id}).then(({data}) => {
          this.infoDialog.formData = data
          this.oldStatus = data.status
        }).finally(() => {
          this.$modal.closeLoading()
        })
      }else{
      }
    },
    cancel(){
      this.$emit('cancel')
    },
    save(status) {
      this.$refs['infoDialogForm'].validate(valid => {
        if (valid) {
          this.infoDialog.formData.status = status
          if(status === 3){
            this.$refs.editSignRef.show(this.oldStatus==1?this.$t('page.business.study.nonTrial.submit'):this.$t('page.business.study.nonTrial.xgsy'),this.oldStatus==1?this.$t('page.business.study.nonTrial.create'):this.$t('page.business.study.nonTrial.xgsy'))
          }else{
            this.doSave({})
          }
        }
      })
    },
    doSign(val){
      this.doSave(val)
    },
    doSave(sign) {
      this.$modal.loading()
      nonTrial_save({
        study:this.infoDialog.formData,
        sign:sign
      }).then(() => {
        this.$emit('save')
        this.$refs.editSignRef.cancel()
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
  }
};
</script>
<style lang="scss">
.content-edit{
  background: #f5f5f5;
  padding: 0;
  .edit-top{
    background: #fff;
    padding: 10px 20px;
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    .left-top{
      flex-shrink: 0;
      display: flex;
      flex-direction: row;
      align-items: center;
      img{
        height: 16px;
        margin-right: 20px;
        cursor: pointer;
      }
      .right-top{

      }

    }
  }
  .edit-content{
    background: #fff;
    padding: 20px;
    min-height: 500px;
    .edit-form{
      width: 800px;
      margin: 0 auto;
    }
  }
}
</style>