<template>
  <div>
    <edit-header :title="title">
      <el-button @click="handleClose" plain icon="el-icon-close">取 消</el-button>
      <el-button type="primary" @click="submitForm" :loading="submitLoading" plain icon="el-icon-check">确 定</el-button>
    </edit-header>
    <div class="app-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small" v-loading="loading">
        <el-row>
          <form-col :span="24" label="客户名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入客户名称" />
          </form-col>
          <form-col :span="span" label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%;">
              <el-option
                v-for="dict in dict.type.customer_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="意向强度" prop="intentLevel">
            <el-select v-model="form.intentLevel" placeholder="请选择意向强度" style="width: 100%;">
              <el-option
                v-for="dict in dict.type.customer_intent_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="手机号" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入手机号" maxlength="11" show-word-limit />
          </form-col>
          <form-col :span="span" label="微信号" prop="wechat">
            <el-input v-model="form.wechat" placeholder="请输入微信号" maxlength="50" show-word-limit />
          </form-col>
          <form-col :span="span" label="来源" prop="source">
            <el-select v-model="form.source" placeholder="请选择来源，可自定义" style="width: 100%;" allow-create filterable default-first-option>
              <el-option
                v-for="dict in dict.type.customer_source"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="意向" prop="intents">
            <el-select v-model="form.intents" placeholder="请选择意向，可自定义" style="width: 100%;" allow-create filterable default-first-option multiple>
              <el-option
                v-for="dict in dict.type.customer_intent"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="跟进人" prop="followId">
            <user-select v-model="form.followId" :disabled="disabledFollowUser" />
          </form-col>
          <form-col :span="24" label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200" show-word-limit />
          </form-col>
          <form-col :span="span" label="顾虑点" prop="concern">
            <el-input v-model="form.concern" type="textarea" placeholder="请输入顾虑点" maxlength="200" show-word-limit />
          </form-col>
          <form-col :span="span" label="痛点" prop="pain">
            <el-input v-model="form.pain" type="textarea" placeholder="请输入痛点" maxlength="200" show-word-limit />
          </form-col>
          <form-col :span="span" label="关注点" prop="attention">
            <el-input v-model="form.attention" type="textarea" placeholder="请输入关注点" maxlength="200" show-word-limit   />
          </form-col>
          <form-col :span="span" label="需求点" prop="demand">
            <el-input v-model="form.demand" type="textarea" placeholder="请输入需求点" maxlength="200" show-word-limit />
          </form-col>
        </el-row>

        <template v-if="form.id == null">
          <el-tabs>
            <el-tab-pane label="跟进记录">
              <customer-follow-form :form="form.follow" :hide="['customerId']" prop-prefix="follow." :span="span"/>
            </el-tab-pane>
          </el-tabs>
        </template>

      </el-form>
    </div>
  </div>
</template>

<script>
import { getCustomer, addCustomer, updateCustomer } from "@/api/bst/customer";
import FormCol from "@/components/FormCol/index.vue";
import { CustomerStatus, CustomerIntentLevel, Role } from '@/utils/enums';
import { mapGetters } from 'vuex';
import UserSelect from '@/components/Business/User/UserSelect.vue';
import CustomerFollowForm from '@/views/bst/customerFollow/components/CustomerFollowForm.vue';
import { parseTime } from '@/utils/ruoyi';
import EditHeader from '@/components/EditHeader/index.vue';
import { checkRole } from '@/utils/permission';
export default {
  name: "CustomerEdit",
  components: { FormCol, UserSelect, CustomerFollowForm, EditHeader },
  dicts: ['customer_intent_level', 'customer_status', 'customer_source', 'customer_intent'],
  data() {
    return {
      loading: false,
      submitLoading: false,
      span: 6,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "客户编号不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "客户姓名不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        intentLevel: [
          { required: true, message: "意向强度不能为空", trigger: "change" }
        ],
        source: [
          { required: true, message: "来源不能为空", trigger: "change" }
        ],
        followId: [
          { required: true, message: "跟进人不能为空", trigger: "change" }
        ],
        follow: {
          type: [
            { required: true, message: "跟进方式不能为空", trigger: "change" }
          ],
          content: [
            { required: true, message: "跟进内容不能为空", trigger: "change" }
          ],
          followTime: [
            { required: true, message: "跟进时间不能为空", trigger: "change" }
          ]
        }
      },
      id: null,
    };
  },
  computed: {
    ...mapGetters(['userId']),
    // 禁用选择跟进人的情况
    disabledFollowUser() {
      return !checkRole([Role.SYS_ADMIN, Role.ADMIN]);
    },
    title() {
      return this.id ? '修改客户' : '新增客户';
    },
    dialogVisible: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit('update:show', value);
      }
    }
  },
  created() {
    this.id = this.$route.params.id;
    if (this.id) {
      this.getInfo(this.id);
    } else {
      this.reset();
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true;
      getCustomer(id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        status: CustomerStatus.POTENTIAL,
        intentLevel: CustomerIntentLevel.MEDIUM,
        mobile: null,
        wechat: null,
        source: null,
        intents: [],
        followId: this.userId,
        remark: null,
        follow: {
          followTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
        }
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.submitLoading = true;
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          } else {
            this.submitLoading = true;
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    // 取消按钮
    handleClose() {
      this.$tab.closeBack();
    },
  }
};
</script>

<style lang="scss" scoped>

</style>