<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.study.xgsy'):$t('page.business.study.study.create')}}</div>
      </div>
      <div class="right-top">
        <el-button @click="cancel()">{{$t('page.business.study.study.cancel')}}</el-button>
        <el-button type="primary" @click="save(1)" plain v-if="!infoDialog.formData.status || infoDialog.formData.status===1">{{$t('page.business.study.study.save')}}</el-button>
        <el-button type="primary" @click="save(3)">{{$t('page.business.study.study.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.study.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.study.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.study.leader')+'：'" prop="leader">
              <SelectDeptUser :readOnly="!checkRole(['TFM'])" @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.study.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 { checkRole } from "@/utils/permission";
import { study_info,study_save} from "@/api/business/study/study";
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.study.inputName'), trigger: "blur" }
          ],
          sn: [
            { required: true, message: this.$t('page.business.study.study.inputSn'), trigger: "blur" }
          ],
          leader: [
            { required: true, message: this.$t('page.business.study.study.inputLeader'), trigger: "change" }
          ],
        }
      },
      oldStatus:1
    };
  },
  created() {
  },
  methods: {
    checkRole,
    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()
        study_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.study.submit'):this.$t('page.business.study.study.xgsy'),this.oldStatus==1?this.$t('page.business.study.study.create'):this.$t('page.business.study.study.xgsy'),false,null,this.oldStatus!=1,this.oldStatus!=1)
          }else{
            this.doSave({})
          }
        }
      })
    },
    doSign(val){
      this.doSave(val)
    },
    doSave(sign) {
      this.$modal.loading()
      study_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>