<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="appointmentTimeVisible"
      @close="cancel"
    >
      <!--            <el-row :gutter="30" class="row search" type="flex" style="margin-bottom:0;">-->
      <el-form
        ref="setForm"
        :rules="addRules"
        :model="model"
        label-width="250px"
        label-suffix=":"
        label-position="right"
        size="mini"
        style="width: 100%;"
      >
        <el-form-item
          label="问诊方式"
          prop="diagnoseChannel"
        >
          <el-col :span="15">
            <el-radio-group
              v-model="model.diagnoseChannel"
              size="small"
              style="line-height: 45px;width: 135%;"
            >
              <el-radio :label="1">
                APP问诊（系统发起问诊）
              </el-radio>
              <el-radio :label="2">
                线下问诊（APP不会自动发起问诊）
              </el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>

        <el-form-item
          label="预约时间"
          class="required-label"
          prop="rangeTime"
        >
          <el-col :span="15">
            <el-date-picker
              v-model="rangeTime"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions1"
              style="width: 135%;"
            />
          </el-col>
        </el-form-item>
      </el-form>
      <!--            </el-row>-->
      <span
        slot="footer"
        class="dialog-footer"
        style="text-align: center;"
      >
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          :disabled="isClick"
          @click="confirm"
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {updateDiagnosis} from '../../utils/diagnosis';

  let vm = null;
  export default {
    props: {
      appointmentTimeVisible: {
        type: Boolean,
        default: false
      },
      diagnoseLogId: {
        type: Number,
        default: 0
      },
      bizType: {
        type: Number,
        default: 0
      },
    },
    data() {
      return {
        show: false,
        title: '设置预约时间（问诊时间）',
        confirmTxt: '确定',
        cancleTxt: '',
        _promise: null,
        isClick:false,
        model: {
          diagnoseChannel: 1,
          appointBeginTime: '',
          appointEndTime: ''
        },
        rangeTime: '',
        addRules: {
          diagnoseChannel: [
            {required: true, message: '请选择问诊方式', trigger: 'blur'}
          ],
          rangeTime: [
            {
              type: 'array',
              required: true,
              // message: "预约时间不能为空",
              trigger: 'change',
              fields: {
                0: {required: true, type: 'date', message: '开始时间不能为空', trigger: 'change'},
                1: {required: true, type: 'date', message: '结束时间不能为空', trigger: 'change'}
              }
            }
          ],
        },
        pickerOptions1: {
          disabledDate: time => {
            return (
              time.getTime() < Date.now() - 1 * 24 * 3600 * 1000
            ); // 减去一天的时间代表可以选择同一天;
          }
        },
      };
    },
    created() {
      vm = this;
    },
    methods: {
      confirm() {
        if(!(this.rangeTime != null && this.rangeTime.length > 0)) {
          vm.$message.warning('请选择预约时间');
          return;
        }
        // this.$refs.setForm.validate((valid) => {
        //   if (valid) {
        const req = {
          id: this.diagnoseLogId,
          bizType: this.bizType,
          diagnoseChannel: this.model.diagnoseChannel,
          appointBeginTime: this.rangeTime[0],
          appointEndTime: this.rangeTime[1]
        };
        vm.isClick = true;
        updateDiagnosis(req).then(function (res) {
          vm.isClick = false;
          if (res.code == '000000') {
            vm.cancel();
            vm.$emit('search');
          } else {
            vm.$message.error(res.message);
          }
        }).catch(function (error) {
          vm.isClick = false;
          vm.$message.error(res.message);
        });
      //   }
      // })
      },
      cancel() {
        this.rangeTime = '';
        this.$emit('update:appointmentTimeVisible', false);
      }
    }
  };
</script>

<style lang="scss" scoped>

</style>
