<template>
  <!-- 成交登记对话框 -->
  <el-dialog title="审核" :visible.sync="modalVisible" width="800px" append-to-body :close-on-click-modal="false">
    <el-tabs v-model="checkTab" @tab-click="handleClick">
      <el-tab-pane label="登记信息" name="first">

      </el-tab-pane>
      <el-tab-pane label="审核" name="second">

      </el-tab-pane>
    </el-tabs>
    <!-- 显示登记内容 -->
    <div v-show="first" style="height:500px;overflow-y:auto;">
      <el-form :model="modalForm" ref="modalForm" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学员联系方式" prop="phone">
              <el-input v-model="modalForm.phone" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员姓名" prop="name">
              <el-input v-model="modalForm.name" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="线索来源" prop="source">
              <el-select v-model="modalForm.source" placeholder="请选择" size="small" disabled>
                <el-option v-for="dict in sourceOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否全款" prop="state">
              <el-radio-group v-model="modalForm.state" disabled>
                <el-radio :label="true">全款</el-radio>
                <el-radio :label="false">非全款</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="成交日期" prop="dealDate">
              <el-date-picker v-model="modalForm.dealDate" size="small" disabled value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名价格(元)" prop="signPrice">
              <el-input v-model="modalForm.signPrice" placeholder="学员报名时需要交纳总共的钱" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="报名驾校" prop="signSchool">
              <el-select v-model="modalForm.signSchool" filterable placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in schoolOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名场地" prop="signPlace">
              <el-select v-model="modalForm.signPlace" filterable placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in placeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="报名班型" prop="signClass">
              <el-select v-model="modalForm.signClass" filterable placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in classTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="对接人" prop="schoolPeople">
              <el-input v-model="modalForm.schoolPeople" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="驾校支付" prop="schoolPay">
              <el-input type="number" v-model="modalForm.schoolPay" placeholder="请输入驾校支付金额" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="驾考宝典款" prop="alipay">
              <el-input v-model="modalForm.alipay" placeholder="请输入驾考宝典款金额" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="额外支出" prop="extraPayType">
              <el-select v-model="modalForm.extraPayType" filterable placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in extraPayTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="额外支出金额" prop="extraPay">
              <el-input placeholder="请输入额外支出金额" v-model="modalForm.extraPay" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="归属人员" prop="followUser">
              <el-select v-model="modalForm.followUser" multiple placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="area">
              <el-select v-model="modalForm.area" placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in areaOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                <el-form-item label="线下接待人员" prop="offlineReceiver">
                  <el-select v-model="modalForm.offlineReceiver" multiple placeholder="请选择" clearable size="small" :disabled="!modalForm.signEdit || modalForm.clueId != undefined">
                    <el-option v-for="dict in userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
                  </el-select>
                </el-form-item>
              </el-col>-->
        </el-row>

        <el-row v-if="admin == 'true'">
          <el-col :span="12">
            <el-form-item label="佣金明细" prop="commission">
              <el-select v-model="modalForm.commission" placeholder="请选择" clearable size="small" disabled>
                <el-option v-for="dict in commissionOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="memo">
              <el-input type="textarea" :rows="2" v-model="modalForm.memo" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="凭据" prop="memo">
              <div class="demo-image__preview">
                <div v-for="(item, index) in modalForm.fileList" :key="index" class="pr dib">
                  <el-image class="image-list-item" fit="contain" :src="preUrl + item.url" :preview-src-list="modalForm.fileList.map(url => preUrl + url)" lazy />
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="修改记录">
              <el-timeline style="max-height:260px;overflow-y:auto;">
                <el-timeline-item v-for="info in updateRecord" :key="info.record" :timestamp="info.operateTime" placement="top" style="padding:5px !important;">
                  <el-card>
                    <span style="display:block;font-weight: bold; font-size:13px;">用户 {{info.operateUserName}}</span>
                    <span v-html="info.centent" style="display:block;padding-left: 10px; font-size:13px;" />
                  </el-card>
                </el-timeline-item>
              </el-timeline>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 录入审核结果 -->
    <div v-show="second" style="height:500px;overflow-y:auto;">
      <el-form :model="modalForm2" ref="modalForm2" label-width="110px">
        <el-row v-if="admin == 'true'">
          <el-col :span="12">
            <el-form-item label="回款状态" prop="moneyState">
              <el-select v-model="modalForm2.moneyState" placeholder="请选择" clearable size="small">
                <el-option v-for="dict in moneyStateOptions" :key="dict.uid" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="驳回理由" prop="rejectReason">
              <el-input type="textarea" :rows="2" v-model="modalForm2.rejectReason"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="审核记录">
              <el-timeline style="max-height:260px;overflow-y:auto;">
                <el-timeline-item v-for="info in checkRecord" :key="info.record" :timestamp="info.operateTime" placement="top" style="padding:5px !important;">
                  <el-card>
                    <span style="display:block;font-weight: bold; font-size:13px;">用户 {{info.operateUserName}}</span>
                    <span v-html="info.centent" style="display:block;padding-left: 10px; font-size:13px;" />
                  </el-card>
                </el-timeline-item>
              </el-timeline>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer">
      <el-button type="primary" v-if="first" :loading="loading" @click="handleNext">下一步</el-button>
      <el-button type="primary" v-if="second" :loading="loading" @click="handleBack">上一步</el-button>
      <el-button type="primary" v-if="second" :loading="loading" @click="handleCheck(2)">通过</el-button>
      <el-button type="primary" v-if="second" :loading="loading" @click="handleCheck(3)">驳回</el-button>
      <el-button @click="modalVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>
    <script>
import { checkSign, getCheckRecord } from '@/api/zs/sign'
import { validateMoney } from '@/utils/validate'
import { getToken } from '@/utils/auth'
import {
  getEmployee,
  getSchools,
  getPlaces,
  getClassTypes,
} from '@/api/tool/common'

export default {
  name: 'Check',
  data() {
    return {
      admin: localStorage.getItem('admin'),
      preUrl: process.env.VUE_APP_BASE_API,
      userId: localStorage.getItem('userId'),
      modalVisible: false,
      signLoading: false,
      first: true,
      second: false,
      modalForm: {},

      modalRules: {
        name: { required: true, message: '姓名不为空', trigger: 'blur' },
        phone: { required: true, message: '联系方式不为空', trigger: 'blur' },
        source: { required: true, message: '线索来源不为空', trigger: 'blur' },
        area: { required: true, message: '所属区域不为空', trigger: 'blur' },
      },
      userOptions: [],
      clueOptions: [],
      schoolOptions: [],
      placeOptions: [],
      classTypeOptions: [],
      sourceOptions: [],
      commissionOptions: [],
      extraPayTypeOptions: [],
      areaOptions: [],
      moneyStateOptions: [],
      modalForm2: {},
      loading: false,
      updateRecord: [],
      checkRecord: [],
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      picList: [],
    }
  },
  created() {
    //线索来源
    this.getDicts('dm_source').then((response) => {
      this.sourceOptions = response.data
    })
    //佣金明细
    this.getDicts('dm_commission').then((response) => {
      this.commissionOptions = response.data
    })
    //额外支出类型
    this.getDicts('dm_extra_pay').then((response) => {
      this.extraPayTypeOptions = response.data
    })
    //s所属区域
    this.getDicts('dm_area').then((response) => {
      this.areaOptions = response.data
    })
    //回款状态
    this.getDicts('dm_money_state').then((response) => {
      this.moneyStateOptions = response.data
    })
  },
  methods: {
    init(data) {
      console.log('打开弹框')
      this.resetForm('modalForm')
      this.modalForm = data
      this.getSchools()
      this.getPlaces()
      this.getClassTypes()
      this.getEmployee()
      this.fileList = []
      this.picList = []
      if (data.fileList) {
        this.fileList = data.fileList
        for (let i in this.fileList) {
          this.picList.push(this.fileList[i]) //.url
        }
      }
      console.log(this.picList)
      this.getUpdateRecord(this.modalForm.signId)
      this.modalVisible = true
      this.first = true
      this.second = false
      this.resetForm('modalForm2')
      this.modalForm2 = {
        signId: this.modalForm.signId,
        checkState: undefined,
        rejectReason: data.rejectReason,
        moneyState: data.moneyState,
      }
      this.getCheckRecord(this.modalForm.signId)
    },
    getSchools() {
      getSchools().then((resp) => {
        this.schoolOptions = resp.data
      })
    },
    getPlaces() {
      getPlaces({ deptId: this.modalForm.signSchool, status: '0' }).then(
        (resp) => {
          this.placeOptions = resp.data
        }
      )
    },
    getClassTypes() {
      getClassTypes({
        deptId: this.modalForm.signSchool,
        placeId: this.modalForm.signPlace,
        status: '0',
      }).then((resp) => {
        this.classTypeOptions = resp.data
      })
    },

    getEmployee() {
      getEmployee({ coach: false }).then((resp) => {
        if (resp.code == 200) {
          this.userOptions = resp.data
        }
      })
    },
    handleNext() {
      this.first = false
      this.second = true
    },
    handleBack() {
      this.first = true
      this.second = false
    },
    handleCheck(state) {
      this.modalForm2.checkState = state
      this.modalForm2.signId = this.modalForm.signId
      this.loading = true
      checkSign(this.modalForm2).then((resp) => {
        if (resp && resp.code == 200) {
          if (state == 2) {
            this.$message.success('通过成功！')
          } else {
            this.$message.success('驳回成功！')
          }
          this.modalVisible = false
          this.loading = false
          this.$emit('refreshDataList')
        } else {
          this.loading = false
        }
      })
    },
    getCheckRecord(signId) {
      getCheckRecord({ signId: signId, type: 1 }).then((resp) => {
        this.checkRecord = resp.data
      })
    },
    getUpdateRecord(signId) {
      getCheckRecord({ signId: signId, type: 0 }).then((resp) => {
        this.updateRecord = resp.data
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
  },
}
    </script>