<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.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.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.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.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_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.remark" readonly></el-input>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>产品：
          </el-col>
          <el-col :span="18">
            <el-select style="width: 300px;" v-model="productId">
              <el-option v-for="item in products" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-row>

        <el-row type="flex" align="middle">
          <el-col :span="4" :push="1">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>订单：
          </el-col>
          <el-col :span="18">
            <el-select style="width: 300px;" v-model="orderId">
              <el-option v-for="item in orders" :label="item.no" :value="item.id"></el-option>
            </el-select>
          </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>
            </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="orderId"
      :product-id="productId"
      :customer-id="detail.id"></timeSelect>
  </div>
</template>

<script>
import {parseTime} from '@/utils'
import {customerDetail} 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: () => []
    },
    orders:{
      type: Array,
      default: () => []
    },
    skus:{
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      productId: '',
      orderId:'',
      isShow: false,
      isShowTime:false,
      date:'',

      detail: '',
      dateStartPick:'',
      dateEndPick:'',
      pickType:1,
    }
  },
  methods: {
    datePick(date,data){
      console.log(date,data)
      if(data.type === 'current-month'){
        this.date = data.day
        if(this.orderId && this.productId){
          this.isShowTime = true
        }else{
          this.$message({
            type:'warning',
            message:'请先选择订单与产品'
          })
        }
      }
    },

    async getCustomer() {
      const res = await customerDetail({
        product_id: this.form.product_id,
        id: this.form.customer_id,
      })
      console.log(res)
      this.detail = res.detail
    },

    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.birthday).getFullYear()
    },
    defaultAddress(){
      return function (adds){
        if(!adds instanceof Array){
          return '无地址'
        }
        return adds.filter(item => {
          return item.default === 1
        })[0]?.address || adds[0]?.address || '无地址'
      }
    },
    isInDate(){
      return function (day){
        let startTimer = new Date(this.dateStartPick).getTime()
        let endTimer = new Date(this.dateEndPick).getTime()
        let dayTimer = new Date(day).getTime()
        return dayTimer >= startTimer && dayTimer <= endTimer
      }
    },
    timeFormat(){
      return function (data){
        return parseTime(data,'{dd}')
      }
    },
  },
  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;
  }
}
</style>
