<template>
  <div v-if="detail">
    <xy-dialog
      ref="dialog"
      :is-show.sync="isShow"
      :width='72'
      title="排班"
      type="normal"
      @reset="pickType = 1,dateStartPick = '',dateEndPick = ''">
      <template>
        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            姓名：
          </el-col>
          <el-col :span="18">
            <el-input v-model="detail.customer.name" readonly style="width: 300px;">
            </el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            性别：
          </el-col>
          <el-col :span="18">
            <el-input v-model="detail.customer.sex" readonly style="width: 300px;">
            </el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            身份证号：
          </el-col>
          <el-col :span="18">
            <el-input v-model="detail.customer.idcard" readonly style="width: 300px;">
            </el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            年龄：
          </el-col>
          <el-col :span="18">
            <el-input :value="ageComputed" readonly style="width: 300px;">
            </el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            评估等级：
          </el-col>
          <el-col :span="18">
            <el-select v-model="detail.customer.level_id" disabled placeholder="请选择失能等级" style="width: 300px">
              <el-option v-for="item in levels" :key="item.id" :label="item.value" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            上门地址：
          </el-col>
          <el-col :span="18">
            <el-input style="width: 300px;" readonly :value="defaultAddress(detail.customer.customer_address)"></el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            备注：
          </el-col>
          <el-col :span="18">
            <el-input style="width: 300px;" v-model="detail.customer.remark" readonly></el-input>
          </el-col>
        </el-row>

        <el-calendar>
          <template v-slot:dateCell="{date, data}">
            <div @click="datePick(date,data)" style="display:flex;flex-direction: column;justify-content: center;align-items: center;width: 100%;height: 100%">
              <div>{{timeFormat(date)}}日</div>
              <template v-if="data.type === 'current-month'">
                <i class="el-icon-plus" style="padding:10px;"></i>
              </template>

              <template v-if="scheduleCount(data.day)">
                <Poptip transfer trigger="hover" title="当日排班" width="520" content="content" >
                  <template v-slot:default>
                    <div class="schedule-num">{{scheduleCount(data.day).length}}</div>
                  </template>

                  <template v-slot:content>
                    <xy-table :is-page="false" :height="220" :list="scheduleCount(data.day)" :table-item="table">
                      <template v-slot:btns>
                        <el-table-column label="操作" width="70">
                          <template v-slot:default="scope">
                            <Poptip
                              :transfer="true"
                              confirm
                              title="确认要删除吗?"
                              @on-ok="destroySchedule(scope.row)">
                              <Button size="small" type="primary" ghost>删除</Button>
                            </Poptip>
                          </template>
                        </el-table-column>
                      </template>
                    </xy-table>
                  </template>
                </Poptip>
              </template>
            </div>
          </template>
        </el-calendar>
      </template>

      <template v-slot:footerContent>
        <Button type="primary" @click="isShow = false">确认</Button>
      </template>
    </xy-dialog>

    <timeSelect
      :is-show.sync="isShowTime"
      :nurses="nurses"
      :skus="skus"
      :date="date"
      :order-id="detail.id"
      :product-id="detail.product.id"
      :customer-id="detail.customer.id"></timeSelect>
  </div>
</template>

<script>
import {parseTime} from '@/utils'
import {customerDetail,scheduleDelete} from '@/api/schedule'

import timeSelect from "@/views/schedule/component/timeSelect";
export default {
  components:{
    timeSelect
  },
  props: {
    customers: {
      type: Array,
      default: () => []
    },
    products: {
      type: Array,
      default: () => []
    },
    nurses: {
      type: Array,
      default: () => []
    },
    levels: {
      type: Array,
      default: () => []
    },
    skus:{
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      productId: '',
      isShow: false,
      isShowTime:false,
      date:'',

      detail: '',
      schedules:[],
      dateStartPick:'',
      dateEndPick:'',
      pickType:1,

      table:[
        {
          label:'护工',
          prop:'nurse_id',
          minWidth:120,
          sortable:false
        },
        {
          label:'护理时间',
          width:310,
          sortable: false,
          customFn:(row)=>{
            return (
              <div>{row.start_time} 至 {row.end_time}</div>
            )
          }
        }
      ]
    }
  },
  methods: {

    datePick(date,data){
      console.log(date,data)
      if(data.type === 'current-month'){
        this.date = data.day
        this.isShowTime = true
      }
    },

    async getCustomer() {
      const res = await customerDetail({
        product_id: this.detail.product.id,
        id: this.detail.customer.id,
      })
      console.log(res)
      this.schedules = res.detail.schedule
    },

    destroySchedule(row){
      scheduleDelete({id:row.id}).then(res => {
        this.$successMessage('destroy','排班')
        this.getCustomer()
      })
    },
    datePicked(data,date){
      if(this.pickType === 1){
        this.dateStartPick = data.day
      }
      if(this.pickType === 2){
        this.dateEndPick = data.day
      }
    },

  },
  computed: {
    ageComputed() {
      return new Date().getFullYear() - new Date(this.detail.customer.birthday).getFullYear()
    },
    defaultAddress(){
      return function (adds){
        if(!adds instanceof Array){
          return '无地址'
        }
        return adds.filter(item => {
          return item.default === 1
        })[0]?.address || adds[0]?.address || '无地址'
      }
    },
    timeFormat(){
      return function (data,format='{dd}'){
        return parseTime(data,format)
      }
    },
    scheduleCount(){
      return function (day){
        let arr = this.schedules.filter(item => {
          return day === item.date
        })
        if(arr.length > 0){
          return arr
        }
      }
    }
  },
  watch: {
    isShow(val) {
      if (val) {
        this.getCustomer()
      } else {
        this.orderId = ''
        this.productId = ''
        this.date = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../styles/index";
.date-select{
  background: $primaryColor;
  color: #fff;
}
::v-deep .el-calendar-day{
  padding: 0 !important;
}
::v-deep .el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.schedule-num{
  width: 18px;
  height: 18px;
  line-height: 18px;
  text-align: center;
  color: #fff;
  background: $primaryColor;
  border-radius: 100%;
}
</style>
