<template>
  <el-dialog :title="cons.consNum+'-'+cons.customerNumber+'-'+$t('跟进记录')"
             :before-close="()=>{$emit('update:showConsFollowupEdit',false)}"
             :visible.sync="showConsFollowupEdit">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="状态" prop="status">
        <dict-selector :placeholder="$t('请选择状态')" v-model="form.status"
                       :type="DICT_TYPE.ECW_CONS_FOLLOWUP_STATUS"></dict-selector>
      </el-form-item>
      <el-form-item label="跟进内容" prop="followContent">
        <el-input type="textarea" v-model="form.followContent" :rows="3" />
      </el-form-item>

      <el-form-item label="下次跟进时间" prop="nextFollowTime">
        <el-date-picker clearable v-model="form.nextFollowTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择下次跟进时间" />
      </el-form-item>
      <el-form-item label="下次跟进内容" prop="nextFollowContent">
        <el-input type="textarea" v-model="form.nextFollowContent" :rows="3" />
      </el-form-item>

      <el-form-item :label="$t('图片')">
        <image-upload v-model="form.exceptionUrls"></image-upload>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="跟进人" prop="followUserId">
            <el-select
              v-model="form.followUserId"
              :placeholder="$t('请选择客户经理')"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in userServiceList"
                :key="dict.id"
                :label="dict.nickname"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="跟进时间" prop="followTime">
            <el-date-picker clearable v-model="form.followTime" type="date" value-format="yyyy-MM-dd"
                            placeholder="选择跟进时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="创建人">
            {{form.creatorName}}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间">
            {{form.createTime}}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="最后更新人">
            {{form.updaterName}}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最后更新时间">
            {{form.updateTime}}
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">{{ $t("确 定") }}</el-button>
      <el-button @click="$emit('update:showConsFollowupEdit',false)">{{ $t("取 消") }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import imageUpload from "@/components/ImageUpload/index.vue"
import Editor from "@/components/Editor/index.vue"
import { getCons } from "@/api/ecw/cons"
import { listSimpleUsers } from "@/api/system/user"
import { createConsFollowup, getConsFollowup, updateConsFollowup } from "@/api/ecw/consFollowup"

export default {
  name: "consFollowupEdit",
  components: { Editor, imageUpload },
  props: {
    id: Number,
    showConsFollowupEdit: Boolean,
    consId: Number
  },
  data() {
    return {
      form: {
        exceptionUrls: []
      },
      rules: {
        status: [
          { required: true, message: "请选择状态", trigger: "blur" }
        ],
        followContent: [
          { max: 500, message: "500max", trigger: "blur" }
        ],
        nextFollowContent: [
          { max: 500, message: "500max", trigger: "blur" }
        ]
      },
      cons: {},
      userServiceList: []
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(validate => {
        if (validate) {
          if(this.consId) {
            var parmas = JSON.parse(JSON.stringify(this.form))
            parmas.exceptionUrls = parmas.exceptionUrls.length ? parmas.exceptionUrls.split(",") : []
            parmas.mediaBusinessType = 2
            createConsFollowup(parmas).then(response => {
              this.$emit("update:showConsFollowupEdit", false)
              this.$emit("determine")
              this.$modal.msgSuccess("新增成功")
            })
          } else {
            var parmas = JSON.parse(JSON.stringify(this.form))
            parmas.exceptionUrls = parmas.exceptionUrls.length ? parmas.exceptionUrls.split(",") : []
            parmas.mediaBusinessType = 2
            updateConsFollowup(parmas).then(response => {
              this.$emit("update:showConsFollowupEdit", false)
              this.$emit("determine")
              this.$modal.msgSuccess("修改成功")
            })
          }
        }
      })
    }
  },
  watch: {
    showConsFollowupEdit(val) {
      if (val) {
        listSimpleUsers().then((r) => {
          this.userServiceList = r.data
        })
        if (this.consId) {
          getCons(this.consId).then(res => {
            this.cons = res.data
          })
          // 获取当前时间精确至[年、月、日、时、分、秒]
          var now = new Date()
          var year = now.getFullYear()
          var month = now.getMonth()
          var date = now.getDate()
          month = month + 1
          month = month.toString().padStart(2, '0')
          date = date.toString().padStart(2, '0')
          var defaultDate = `${year}-${month}-${date}`
          this.$set(this.form, "followTime", defaultDate)
          this.$set(this.form, "consId", this.consId)
          this.$set(this.form, "followUserId", this.$store.state.user.id)
        } else if (this.id) {
          getConsFollowup(this.id).then(res => {
            var data = res.data
            data.exceptionUrls = data.exceptionUrls && data.exceptionUrls.length > 0 ? data.exceptionUrls.join(',') : '';
            this.form = data
            this.$set(this.form, "status", data.status+'')
            this.cons.consNum = data.consNum
            this.cons.customerNumber = data.customerNumber
          })
        }
      } else {
        this.form = {
          exceptionUrls: []
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
