<template>
  <section class="Add">
    <ai-detail class="add" v-if="isEdit == true || !id">
      <template slot="title">
        <ai-title :title="params.id ? '编辑行政处罚' : '新增行政处罚'" :isShowBack="true" :isShowBottomBorder="true" @onBackClick="cancel(false)" />
      </template>
      <template slot="content">
        <ai-card title="基本信息">
          <template #right v-if="params.id">
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="getDetail(params.id),isEdit=false">取消</span>
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="confirm()">保存</span>
          </template>
          <template #content>
            <div class="add-form">
              <el-form ref="forms" :model="forms" :rules="formRules" size="small"  label-width="150px">
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="处罚文书号" prop="sanctionCode">
                      <el-input v-model="forms.sanctionCode" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="处罚机关" prop="sanctionOrganization">
                      <el-input v-model="forms.sanctionOrganization" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="处罚决定日期" prop="sanctionDate">
                      <el-date-picker v-model="forms.sanctionDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" style="width:337px">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="处罚类型" prop="sanctionType">
                      <ai-select v-model="forms.sanctionType" placeholder="请选择文档类型" :selectList="$dict.getDict('administrativeSanctionType')"></ai-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="处罚内容" prop="sanctionContent">
                      <el-input v-model="forms.sanctionContent" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="处罚依据" prop="sanctionBasis">
                      <el-input v-model="forms.sanctionBasis" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="受罚主体" prop="enterpriseName">
                      <el-input v-model="forms.enterpriseName" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="统一信用代码" prop="unifiedCode">
                      <el-input v-model="forms.unifiedCode" placeholder="请输入" maxlength="18"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="违法事实" prop="lllegalFact">
                  <el-input v-model="forms.lllegalFact" placeholder="请输入违法事实" type="textarea" show-word-limit maxlength="500" :rows="5"></el-input>
                </el-form-item>
              </el-form>
            </div>
          </template>
        </ai-card>
      </template>
      
      <template slot="footer" class="footer">
        <el-button class="delete-btn footer-btn" @click="cancel(false)">取消</el-button>
        <el-button class="footer-btn" type="primary" @click="confirm()">保存</el-button>
      </template>
    </ai-detail>

    <ai-detail class="add" v-if="id && !isEdit">
      <template slot="title">
        <ai-title title="行政处罚信息详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
      </template>
      <template slot="content">
        <ai-card title="基本信息">
          <template #right>
             <span style="color:#2266FF;cursor: pointer;font-size: 12px;" class="iconfont iconEdit" v-if="isEdit==false" @click="edit">修改</span>
          </template>
          <template #content>
            <ai-wrapper>
              <ai-info-item label="处罚文书号" :value="data.sanctionCode"></ai-info-item>
              <ai-info-item label="处罚机关" :value="data.sanctionOrganization"></ai-info-item>
              <ai-info-item label="处罚决定日期" :value="data.sanctionDate"></ai-info-item>
              <ai-info-item label="处罚类型">{{ dict.getLabel('administrativeSanctionType',data.sanctionType) }}</ai-info-item>
              <ai-info-item label="处罚内容" :value="data.sanctionContent" isLine></ai-info-item>
              <ai-info-item label="处罚依据" isLine :value="data.sanctionBasis"></ai-info-item>
              <ai-info-item label="受罚主体" isLine :value="data.enterpriseName"></ai-info-item>
              <ai-info-item label="统一信用代码" isLine :value="data.unifiedCode"></ai-info-item>
              <ai-info-item label="违法事实" isLine :value="data.lllegalFact"></ai-info-item>
            </ai-wrapper>
          </template>
        </ai-card>
      </template>
    </ai-detail>
  </section>
</template>

<script>
export default {
  name: "Add",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
    params: Object,
  },
  computed: {
    formRules() {
      return {
        sanctionCode: [{required: true,message:"请输入处罚文书号",trigger: "blur"}],
        sanctionOrganization: [{required: true,message:"请输入处罚机关",trigger: "blur"}],
        sanctionDate: [{required: true,message:"请输入处罚决定日期",trigger: "blur"}],
        sanctionType: [{required: true,message:"请输入处罚类型",trigger: "blur"}],
        sanctionContent: [{required: true,message:"请输入处罚内容",trigger: "blur"}],
        sanctionBasis: [{required: true,message:"请输入处罚依据",trigger: "blur"}],
        enterpriseName: [{required: true,message:"请输入受罚主体",trigger: "blur"}],
        unifiedCode: [{required: true,message:"请输入统一信用代码",trigger: "blur"},
        { min: 18, max: 18, message: '长度为18个字符', trigger: 'blur' }],
        lllegalFact: [{required: true,message:"请输入违法事实",trigger: "blur"}],
      }
    }
  },
  data() {
    return {
      forms: {
        sanctionCode: '',
        sanctionOrganization: '',
        sanctionDate: '',
        sanctionType: '',
        sanctionContent: '',
        sanctionBasis: '',
        enterpriseName: '',
        unifiedCode: '',
        lllegalFact: '',
      },
      isEdit: false,
      id: '',
      data: {},
    }
  },
  created() {
    this.$dict.load('administrativeSanctionType')
    if (this.params && this.params.id) {
      this.id = this.params.id
      this.getDetail(this.params.id)
    }
  },
  methods: {
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh,
      })
    },
    edit(){
      this.isEdit=true
      this.forms = JSON.parse(JSON.stringify(this.data))
    },
    getDetail(id) {
      this.instance.post(`/appcreditadministrativesanction/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm() {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          this.instance.post(`/appcreditadministrativesanction/addOrUpdate`, {
            ...this.forms,
            id: this.params.id || '',
          }).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功')
              setTimeout(() => {
                this.cancel(true)
              }, 600)
            }
          })
        }
      })
    },
  },

}
</script>

<style lang="scss" scoped>
.Add {
  height: 100%;
  overflow: hidden;
  background: #f2f4f6 !important;

  .add-form__item {
    display: flex;
    align-items: center;
  }

  ::v-deep .el-form-item__label {
    padding-right: 40px;
  }

  ::v-deep .ai-detail__footer {
    background: #fff !important;
  }

  ::v-deep .ai-detail__content--active {
    padding: 20px;

    .ai-detail__content--wrapper {
      width: 100%;
    }

    .aibar {
      padding: 0 16px;
    }

    .el-form {
      padding: 0 96px 20px 50px;
    }

    .add-form {
      background: #fff;
    }
  }

  ::v-deep .ai-wrapper {
    align-items: inherit !important;
  }

  .user-wrapper {
    display: flex;
    justify-content: space-between;
  }

  .avatar {
    width: 100px;
    height: 100px;
    object-fit: contain;
    border-radius: 10px;
  }

  .footer-btn {
    width: 130px;
  }

  .el-form {
    padding-bottom: 80px;
  }
}
</style>