<template>
  <div>
    <xy-dialog ref="dialog" :width="60" :is-show.sync="isShow" :type="'form'" :title="type=='show'?'预约查看':'预约审核'"
      :form="form">
      <template v-slot:name>
        <div class="xy-table-item" style="padding:0">
          <div class="xy-table-item-label" style="font-weight: bold;width:0;padding:0;display:none">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>
          </div>
          <div class="xy-table-item-content" style="flex-grow:1;text-align:center">
            <div class="bookInfo">
              <div>
                <span>预约日期：</span>
                <span>{{submitObj.date}}</span>
              </div>
              <div>
                <span>时间段：</span>
                <span>{{formatHH(submitObj.start_time)}}-{{formatHH(submitObj.end_time)}}</span>
              </div>
              <div>
                <span>预约事项：</span>
                <span>{{submitObj.content}}</span>
              </div>
              <div>
                <span>预约场地：</span>
                <span>{{submitObj.appointment_config?submitObj.appointment_config.name:''}}</span>
              </div>
              <div>
                <span>车牌：</span>
                <span>{{submitObj.plate}}</span>
              </div>
              <div>
                <span>同行人数：</span>
                <span>{{submitObj.accompany_total}}</span>
              </div>
              <div>
                <span>同行人：</span>
                <span>
                  <template  v-if="submitObj.appointment_accompany&&submitObj.appointment_accompany.length>0">
                    <template v-for="item in submitObj.appointment_accompany">
                      {{item.name}},
                    </template>
                  </template>

                </span>
              </div>
              <div>
                <span>预约人：</span>
                <span>{{submitObj.user?submitObj.user.username:''}}</span>
              </div>
              <div>
                <span>状态：</span>
                <span>
                  <template v-for="item in book_status">
                    <el-tag style="width:auto" :type="item.type"
                      v-if="submitObj.status===item.id">{{item.value}}</el-tag>
                  </template>
                </span>
              </div>
              <div v-if="submitObj.status===1">
                <span>预约二维码链接：</span>
                <span>
                  <a :href="baseUrl+submitObj.h5_url" target="_blank" style="color:blue">{{baseUrl}}{{submitObj.h5_url}}</a>

                </span>
              </div>
              <div v-if="type==='show'">
                <span>审核意见：</span>
                <span>
                  {{submitObj.reason}}
                </span>
              </div>
              <div style="text-align: left;width:800px;margin: 0 auto;">第三方记录</div>
              <div style="width:100%">
                <xy-table :list="thirdArr" style="width:800px;margin: 0 auto;" :isPage="false" :height="300" :table-item="table_item">
                  <template v-slot:finally>
                    <el-table-column align='center' label="预约结果" width="120" header-align="center">
                      <template slot-scope="scope">
                        <div v-if="scope.row.finally===1">
                          成功
                        </div>
                        <div v-else>
                          失败
                        </div>
                      </template>
                    </el-table-column>
                  </template>
                  <template v-slot:response_data>
                    <el-table-column align='center' label="第三方返回数据" width="120" header-align="center">
                      <template slot-scope="scope">
                        <div v-if="scope.row.response_data">
                          <!-- {{JSON.parse(scope.row.response_data).resMsg}} -->
                          {{JSON.parse(scope.row.response_data)}}
                        </div>
                      </template>
                    </el-table-column>
                  </template>

                  <template v-slot:btns>
                    <el-table-column align='center' label="操作" width="120" header-align="center">
                      <template slot-scope="scope">
                        <!--  v-if="scope.row.finally!=1" -->
                        <el-button type="primary" size="small"
                          @click="sendBook(scope.row.appointment_id)">重新预约</el-button>
                      </template>
                    </el-table-column>
                  </template>
                </xy-table>
              </div>
            </div>

            <div v-if="type=='check'">
              <lx-header icon="" text="审核意见" style="margin-bottom: 10px; border: 0px;text-align: left;">
              </lx-header>
              <div>
                <el-input v-model="submitObj.reason" type="textarea" row="3"></el-input>
              </div>
              <div style="margin-top:15px">
                <el-button type="primary" style='margin:15px;' @click="submit(1)">审核通过</el-button>
                <el-button type="primary" style='margin:15px;' @click="submit(2)">审核不通过</el-button>
                <el-button type="primary" plain style='margin:15px;' @click="submit(3)">取消预约</el-button>
              </div>
            </div>


          </div>
        </div>
      </template>

      <template v-slot:footerContent>
        <div></div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import {
    show,
    save,
    retry
  } from "@/api/book/index.js"
  export default {
    mixins: [myMixins],
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'show',
        id: '',
        submitObj: {},
        thirdArr: [],
        baseUrl:`${process.env.VUE_APP_BASE_API}`,
        form: {
          name: '',
        },
        table_item: [{
          prop: 'remark',
          label: '预约内容',
          width: 200
        }, {
          prop: 'finally',
          label: '预约结果',
          width: 200
        },  {
          prop: 'response_data',
          label: '第三方返回数据',
          width: 200
        },{
          prop: 'created_at',
          label: '发送时间',
          width: 200
        }]
      }
    },
    created() {},
    methods: {
      submit(status) {
        this.submitObj.status = status
        save(this.submitObj).then(res => {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },
      sendBook(id) {
        retry({
          id: id
        }).then(res => {
          this.$message({
            type: 'success',
            message: '重新发送预约成功'
          })
          this.getDetail()
        })
      },
      formatHH(val) {
        if (val) {
          return this.$moment(val).format("HH:mm:ss")
        } else {
          return ''
        }
      },
      async getDetail() {
        await show({
          id: this.id,
          show_relation: ['user', 'appointmentAccompany', 'appointmentConfig', 'third_appointment_logs']
        }).then(res => {
          this.submitObj = this.base.deepCopy(res, this.submitObj)
          this.thirdArr = res.third_appointment_logs ? res.third_appointment_logs : []
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          this.getDetail()
        } else {
          this.id = ''
          this.submitObj = {}
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .name {
    flex-basis: 100%;
  }

  .bookInfo {
    &>div {
      span {
        display: inline-block;
      }

      span:first-child {
        width: 20%;
        text-align: right;
      }

      span:last-child {
        width: 80%;
        text-align: left;
      }
    }
  }
</style>
