<!-- 添加/编辑 -->
<template>
  <el-dialog :title="title" :visible.sync="isShow" :close-on-click-modal="false" class="drawer-box" width="700px" @close="handleCancel" append-to-body>
    <el-form ref="form" :model="form" label-width="150px" label-position="top">
      <el-row>
        <el-col :span="12">
          <el-form-item label="规则类型" prop="code">
            <el-select v-model="form.date" class="wd100">
              <el-option label="普通流转路径" value="normal" />
              <el-option label="默认流转路径" value="default" />
              <el-option label="条件流转路径" value="condition" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款账号" prop="name">
            <el-input-number size="small" :controls="false" class="wd100 align-left" v-model="form.total" placeholder="请输入"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="对方户名" prop="appletId">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="对方账号" prop="name">
            <el-input-number size="small" :controls="false" class="wd100 align-left" v-model="form.total" placeholder="请输入"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收款银行" prop="name">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="code">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="name">
            <el-select v-model="form.date" class="wd100">
              <el-option label="普通流转路径" value="normal" />
              <el-option label="默认流转路径" value="default" />
              <el-option label="条件流转路径" value="condition" />
            </el-select> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="认款人" prop="code">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="认款部门" prop="code">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="归属客户" prop="code">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="操作员" prop="code">
            <el-input size="small" v-model="form.code" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="操作时间" prop="code">
            <el-date-picker v-model="form.code" class="wd100" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="name">
            <el-input size="small" type="textarea" :rows="2" v-model="form.total" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { msgDetailApi } from '@/api/subscription/msg'
export default {
  name: '',
  props: {
    id: [String, Number],
    isOpen: Boolean,
    disabled: Boolean,
  },
  watch: {
    fileList: {
      handler(list) {
        if (list && list.length) {
          this.$nextTick(() => {
            this.$set(this.form, 'logo', 1)
            this.$refs.form.clearValidate(['logo'])
          })
        } else {
          this.$set(this.form, 'logo', null)
        }
      },
      deep: true
    },
    isOpen: {
      handler(val) {
        this.isShow = val
        this.title = '认款详情'
        console.log('251512513');
        if (val) {
          // this.handleInitForm()
          // this.handleInitList(this.id)
        }
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      selectId: 3,
      payShow: false,
      btnLoading: false,
      dialogVisible: false,
      isShow: false,
      form: {},
    }
  },

  methods: {
    handlePayConfig() {
      if (!this.form.total) {
        this.$fm('请输入正确得合同总金额')
      } else {
        this.payShow = true
      }
    },
    handlePayCancel() {
      this.payShow = false
    },
    handlePayConfirm(obj) {
      const { id } = obj
      this.selectId = id
      if (id === 3) {
        this.$set(this.form, 'planList', [])
      } else {
        this.$set(this.form, 'planList', [
          { date: null, percent: '10%', money: 10 },
          { date: null, percent: '80%', money: 10 }
        ])
        this.$nextTick(() => {
          // planList.${index}.date
          this.form.planList.forEach((n, index) => {
            this.$refs.form.clearValidate([`planList.${index}.date`])
          })
        })
      }
      this.payShow = false
    },
    handleBeginScan() {
      if (!(this.fileList && this.fileList.length)) {
        return this.$fm('请上传营业执照！')
      }
    },
    handleDel() {
      delSchoolListApi(this.id).then(() => {
        this.$sm('删除成功!')
        this.$emit('onConfirm')
      })
    },
    // id 不存在 新增 否则编辑
    handleInitForm(id = this.id) {
      // 初始化参数
      if (!id) {
        this.form = {
          mobile: null,
          date: '',
          appletId: '',
          code: '',
          name: '',
          type: null,
          isCooperativeSchool: 0,
          region: '',
          note: '',
          sortId: 99,
          state: true,
          logoId: null,
          planList: []
        }
        this.fileList = []
        this.$nextTick(() => {
          this.$refs.form && this.$refs.form.resetFields()
          this.$refs.form && this.$refs.form.clearValidate()
        })
      } else {
        schoolDetailListApi(id).then(res => {
          this.form = {
            ...(res.data || {}),
            id,
            state: res.data.state ? true : false,
          }
          this.fileList = [{ url: process.env.VUE_APP_BASE_API + (res.data || {}).logoUrl }]
          this.$nextTick(() => {
            this.$refs.form && this.$refs.form.clearValidate()
          })
        })
      }
    },
    // 0 新增 1 编辑
    handleInitList(id) {
      queryEnrollAppListApi().then(res => {
        this.nameArr = res.data || []
        if (this.nameArr.length == 1) {
          if (!id) {
            this.isDisabled = true
            this.form.appletId = this.nameArr[0].id
          } else {
            this.isDisabled = false
          }
        } else {
          this.isDisabled = false
        }
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleClose() {
      this.$emit('onClose')
    },

    handleCommonSubmit(type) {
      let formData = new FormData()
      this.fileList.forEach(item => {
        formData.append('files', item.raw)
      })
      this.btnLoading = true
      if (!type) {
        updateFileApi(formData).then((res) => {
          if (res && res.data && res.data.length) {
            const { id } = res.data[0]
            let listQuery = {
              ...this.form,
              logoId: id,
              state: this.form.state ? 1 : 0
            }
            if (!this.id) {
              addSchoolListApi(listQuery).then(() => {
                this.$sm('添加成功!')
                this.$emit('onConfirm')
                this.btnLoading = false
              }).catch(() => {
                this.btnLoading = false
              })
            } else {
              editSchoolListApi(listQuery).then(() => {
                this.$sm('编辑成功!')
                this.$emit('onConfirm')
                this.btnLoading = false
              }).catch(() => {
                this.btnLoading = false
              })
            }
          } else {
            this.btnLoading = false
          }
        }).catch(() => {
          this.btnLoading = false
        })
      } else {
        let listQuery = {
          ...this.form,
          state: this.form.state ? 1 : 0
        }
        editSchoolListApi(listQuery).then(() => {
          this.$sm('编辑成功!')
          this.$emit('onConfirm')
          this.btnLoading = false
        }).catch(() => {
          this.btnLoading = false
        })
      }
    },
    handleSave() {
      this.$nextTick(() => {
        this.$refs.form.validate((bool) => {
          if (bool) {
            if (!this.fileList.length) {
              this.$fm('请上传营业执照!')
              return
            }
            // 编辑
            if (this.id) {
              if (!this.form.logoUrl) {
                this.handleCommonSubmit()
              } else {
                this.handleCommonSubmit(1)
              }
            } else {
              // 新增
              this.handleCommonSubmit()
            }
          } else {
            this.$nextTick(() => {
              this.$scErr()
            })
          }
        })
      })
    },
    handleCancel() {
      this.$emit('onClose')
    },
    handleRemove(file, files) {
      this.fileList = []
      this.form.logoUrl = null
    },
    onUploadChange(file, files) {
      if (['image/png', 'image/jpeg'].includes(file.raw.type)) {
        if (file.size / 1024 > 5000) {
          setTimeout(() => {
            this.$fm('文件超过5Mb，请压缩后上传!')
          }, 10)
          files.splice(files.length - 1, 1)
          return
        }
        this.fileList = []
        this.fileList.push(file)
      } else {
        this.$fm('不支持此文件类型，请重试！')
      }
    },
  }
}

</script>
<style lang='scss' scoped>
@import '@/assets/styles/edit/index.scss';
</style>