<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(),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>
            <el-form ref="forms" :model="forms" :rules="formRules" size="small"  label-width="180px">
              <el-form-item label="欠费金额(万元)" prop="arrearsAmount">
                <el-input v-model="forms.arrearsAmount" placeholder="请输入"></el-input>
              </el-form-item>
              <el-form-item label="欠费类型名称" prop="arrearsType">
                <ai-select v-model="forms.arrearsType" placeholder="请选择欠费类型" :selectList="$dict.getDict('arrearsType')"></ai-select>
              </el-form-item>
              <el-form-item label="欠费主体" prop="arrearsSubject">
                <el-input v-model="forms.arrearsSubject" placeholder="请输入"></el-input>
              </el-form-item>
              <el-form-item label="失信严重程度" prop="dishonestySeverity">
                <ai-select v-model="forms.dishonestySeverity" placeholder="请选择失信严重程度" :selectList="$dict.getDict('dishonestySeverity')"></ai-select>
              </el-form-item>
              <el-form-item label="统一信用代码" prop="unifiedCode">
                <el-input v-model="forms.unifiedCode" placeholder="请输入" maxlength="18"></el-input>
              </el-form-item>
              <el-form-item label="认定日期" prop="cognizanceDate">
                <el-date-picker v-model="forms.cognizanceDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width:307px">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="失信行为有效期" prop="validityPeriodOfDishonesty">
                <el-date-picker v-model="forms.validityPeriodOfDishonesty" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width:307px">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="欠费统计截止日期" prop="deadlineForArrearsStatistics">
                <el-date-picker v-model="forms.deadlineForArrearsStatistics" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width:307px">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="数据来源机构" prop="sourceOrganization">
                <el-input v-model="forms.sourceOrganization" placeholder="请输入"></el-input>
              </el-form-item>
            </el-form>
          </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="isEdit=true,forms=data">修改</span>
            </template>
          <template #content>
            <ai-wrapper>
              <ai-info-item label="欠费金额(万元)" :value="data.arrearsAmount"></ai-info-item>
              <ai-info-item label="欠费类型名称">{{  dict.getLabel('arrearsType',data.arrearsType) }}</ai-info-item>
              <ai-info-item label="欠费主体" :value="data.arrearsSubject"></ai-info-item>
              <ai-info-item label="失信严重程度">{{  dict.getLabel('dishonestySeverity',data.dishonestySeverity) }}</ai-info-item>
              <ai-info-item label="统一信用代码" :value="data.unifiedCode"></ai-info-item>
              <ai-info-item label="认定日期" :value="data.cognizanceDate"></ai-info-item>
              <ai-info-item label="失信行为有效期" :value="data.validityPeriodOfDishonesty"></ai-info-item>
              <ai-info-item label="欠费统计截止日期" :value="data.deadlineForArrearsStatistics"></ai-info-item>
              <ai-info-item label="数据来源机构" :value="data.sourceOrganization"></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 {
        arrearsAmount: [{required: true,message:"请输入欠费金额",trigger: "blur"}],
        arrearsType: [{required: true,message:"请选择欠费类型",trigger: "blur"}],
        arrearsSubject: [{required: true,message:"请输入欠费主体",trigger: "blur"}],
        dishonestySeverity: [{required: true,message:"请选择失信严重程度",trigger: "blur"}],
        unifiedCode: [{required: true,message:"请输入统一信用代码",trigger: "blur"},
        { min: 18, max: 18, message: '长度为18个字符', trigger: 'blur' }],
        cognizanceDate: [{required: true,message:"请选择认定日期",trigger: "blur"}],
        validityPeriodOfDishonesty: [{required: true,message:"请选择失信行为有效期",trigger: "blur"}],
        deadlineForArrearsStatistics: [{required: true,message:"请选择失信行为有效期",trigger: "blur"}],
        validityPeriodOfDishonesty: [{required: true,message:"请选择欠费统计截止日期",trigger: "blur"}],
        sourceOrganization: [{required: true,message:"请输入数据来源机构",trigger: "blur"}],
      }
    }
  },
  created() {
    this.$dict.load('arrearsType','dishonestySeverity')
    if (this.params && this.params.id) {
      this.id = this.params.id
      this.getDetail(this.params.id)
    }
  },
  data() {
    return {
      forms: {
        arrearsAmount:'',
        arrearsType: '',
        arrearsSubject: '',
        dishonestySeverity: '',
        unifiedCode: '',
        cognizanceDate: '',
        validityPeriodOfDishonesty: '',
        deadlineForArrearsStatistics: '',
        validityPeriodOfDishonesty: '',
        sourceOrganization: '',
      },
      data: {},
      isEdit: false,
      id: '',
    }
  },
  methods: {
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh,
      })
    },
    getDetail(id) {
      this.instance.post(`/appcreditpublicutilitiesarrears/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm( ) {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          this.instance.post(`/appcreditpublicutilitiesarrears/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-info-item label {
    width: 100px;
  }

  ::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;
  }
  ::v-deep .el-form-item {
    width: 50%;
    float: left;
  }
}
</style>