<template>
  <section class="Add">
    <ai-detail class="add" v-if="isEdit == true || !id">
      <template #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">
              <!-- <ai-bar :title="params.id ? '编辑行政许可信息' : '新增行政许可信息'"></ai-bar> -->
              <el-form ref="forms" :model="forms" :rules="formRules" size="small"  label-width="150px">
                <el-form-item label="许可文书名称" prop="licenseName">
                  <el-input v-model="forms.licenseName" placeholder="请输入"></el-input>
                </el-form-item>
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="许可文书号" prop="licenseCode">
                      <el-input v-model="forms.licenseCode" placeholder="请输入"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="许可决定日期" prop="decisionDate">
                      <el-date-picker v-model="forms.decisionDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" style="width:338px">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row type="flex">
                  <el-col :span="20">
                    <el-form-item label="许可有效期自" prop="startDate">
                      <el-date-picker v-model="forms.startDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" style="width:338px">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="许可有效期至" prop="endDate">
                      <el-date-picker v-model="forms.endDate" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" style="width:338px">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="许可机关" prop="licenseOrganization">
                  <el-input v-model="forms.licenseOrganization" placeholder="请输入"></el-input>
                </el-form-item>
                <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="licenseContent">
                  <el-input v-model="forms.licenseContent" 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 v-if="isEdit == false">
            <ai-wrapper>
              <ai-info-item label="许可文书名称" :value="data.licenseName"></ai-info-item>
              <ai-info-item label="许可文书号" :value="data.licenseCode"></ai-info-item>
              <ai-info-item label="许可决定日期" :value="data.decisionDate"></ai-info-item>
              <ai-info-item label="许可有效期自" :value="data.startDate"></ai-info-item>
              <ai-info-item label="许可有效期至" :value="data.endDate"></ai-info-item>
              <ai-info-item label="许可机关" isLine :value="data.licenseOrganization"></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.licenseContent"></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 {
        licenseName: [{required: true,message:"请输入许可文书名称",trigger: "blur"}],
        licenseCode: [{required: true,message:"请输入许可文书号",trigger: "blur"}],
        decisionDate: [{required: true,message:"请选择许可决定日期",trigger: "blur"}],
        startDate: [{required: true,message:"请选择许可有效期自",trigger: "blur"}],
        endDate: [{required: true,message:"请选择许可有效期至",trigger: "blur"}],
        licenseOrganization: [{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' }],
        licenseContent: [{required: true,message:"请输入许可内容",trigger: "blur"}],
      }
    }
  },
  data() {
    return {
      forms: {
        licenseName: '',
        licenseCode: '',
        decisionDate: '',
        startDate: '',
        endDate: '',
        licenseOrganization: '',
        enterpriseName: '',
        unifiedCode: '',
        licenseContent: '',
      },
      isEdit: false,
      data: {},
      id: '',
    }
  },
  created() {
    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(`/appcreditadminstrativelicense/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm() {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          this.instance.post(`/appcreditadminstrativelicense/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!important;
    }
  }

  ::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>