<template>
  <!--    modal-->
  <a-modal
    v-model:open="modalVisible"
    :title="modalTitle"
    centered
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :closable="false"
    :keyboard="false"
    :maskClosable="false"
    width="50%"
    okText="确定"
    :cancelText="cancelText"
  >
    <template v-if="modalEnquiryRecordStatus">
      <a-spin :spinning="spinning" :tip="tipTitle">
        <a-divider class="mt-0">历史跟踪记录</a-divider>

        <a-timeline class="time-line-wrap">
          <template v-for="(line, index) in enquiryRecordList" :key="index">
            <a-timeline-item>
              <p class="m-0">
                <a-icon type="history"/>&nbsp;&nbsp;{{line.ctime}}&nbsp;&nbsp;&nbsp;&nbsp;<a-icon type="user"/>
                {{line.realname}}
              </p>
              <p class="mt-5">
                <a-icon type="form"/>&nbsp;&nbsp;{{line.content}}
              </p>
            </a-timeline-item>
          </template>
        </a-timeline>

        <a-divider>添加跟踪记录</a-divider>

        <a-form
          ref="ruleForm"
          :model="enquiryRecordForm"
          :rules="enquiryRecordFormRules"
          :label-col="{span:'3'}"
          :wrapper-col="{span:'21'}"
        >
          <a-form-item label="跟踪记录" name="record">
            <a-textarea
              :auto-size="{ minRows: 6, maxRows: 10 }"
              :defaultValue="enquiryRecordForm.record"
              placeholder="跟踪记录"
              v-model:value="enquiryRecordForm.record"
              allow-clear
            />
          </a-form-item>
        </a-form>
      </a-spin>
    </template>
  </a-modal>
</template>

<script>
import {getAction, postAction, postActionForm} from '/@/api/manage/manage'
import Qs from 'qs'
import {useMessage} from "@/hooks/web/useMessage";
const { createMessage } = useMessage();

export default {
  name: 'enquiryTrackRecord',
  data(){
    return {
      modalVisible: false,
      modalTitle: '',
      confirmLoading:false,
      modalEnquiryRecordStatus:false,
      cancelText: "取消",
      spinning:false,
      enquiryRecordList: [],
      tipTitle:'',
      enquiryInfo:{},
      enquiryRecordForm: {
        record: ''
      },
      enquiryRecordFormRules: {
        record: [
          { required: true, message: '跟踪记录不能为空', trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {

  },
  methods:{
    init(record){
      this.modalVisible = true
      this.modalTitle = '询盘跟踪记录：' + record.no
      this.modalEnquiryRecordStatus = true;
      this.enquiryInfo = record;
      this.cancelText = "关闭"
      this.getEnquiryRecord(record.id);
    },

    handleOk() {
      let that = this
      let modalEnquiryRecordStatus = that.modalEnquiryRecordStatus;

      if (modalEnquiryRecordStatus) {
        that.addEnquiryRecord();
      }
    },


    addEnquiryRecord() {
      let that = this;
      that.spinning = true
      that.tipTitle = '正在添加询盘跟踪记录'
      let params = {
        enquiryId: that.enquiryInfo.id,
        content: that.enquiryRecordForm.record
      }
      postActionForm('/adweb/adwebEnquiryRecord/add', params, 1000 * 60 * 2).then(function(res) {
        that.spinning = false
        that.tipTitle = ''
        if (res.code === 200) {
          createMessage.success('添加成功！')
          that.enquiryRecordForm = {
            record: ''
          }
          that.getEnquiryRecord(that.enquiryInfo.id);
        } else {
          if (res.code === 403) {
            createMessage.warning(res.message)
          } else {
            createMessage.error('添加失败！')
          }
        }
      }).catch(function(err) {
        createMessage.error('网络超时，请重试!')
        that.cleanAllFormData();
        that.loadData()
      })
    },

    getEnquiryRecord(id) {
      let that = this;
      that.spinning = true
      that.tipTitle = '正在获取询盘跟踪记录'
      getAction('/adweb/adwebEnquiryRecord/enquiryRecordList?enquiryId=' + id).then(function(res) {
        that.spinning = false
        that.tipTitle = ''
        if (res.code == 200) {
          that.enquiryRecordList = res.result;
        } else {
          createMessage.error('询盘跟踪记录失败！')
        }
      }).catch(function(err) {
        createMessage.error('网络超时，请重试!')
        that.cleanAllFormData();
      })
    },


    handleCancel() {
      let that = this
      that.cleanAllFormData()
    },


    cleanAllFormData() {
      let that = this
      that.translateStatus = false,
        that.modalVisible = false,
        that.modalTitle = '',
        that.okBtnStatus = {},
        that.cancelBtnStatus = {},
        that.enquiryRecordForm = {
          record: ''
        },
        that.spinning = false,
        that.tipTitle = '',
        that.confirmLoading = false,
        that.enquiryInfo = {},
        that.enquiryRecordList = []
    },

  }
}
</script>

<style scoped>

</style>
