<template>
  <div class="finish-followup" v-if="showThisPage">
    <el-dialog
      title="选择履约开始时间"
      :visible.sync="showAddPatientTime"
      v-if="showThisPage"
      :before-close="clickClose"
      width="35%"
      center>
      <div class="finish-content">
        <el-form
          :model="addPatientData"
          :rules="rules"
          ref="addPatientData"
          label-width="140px">
          <el-form-item label="已选居民：">
            {{addPatientData.patientNames}}
          </el-form-item>
          <el-form-item label="履约开始时间：" prop="joinTime">
            <el-date-picker
              v-model="addPatientData.joinTime"
              type="date"
              placeholder="请选择履约开始时间"
              :editable="false"
              format="yyyy-MM-dd"
              value-format="timestamp"
              clearable>
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="button-while" @click="clickClose" size="small" plain>取 消</el-button>
        <el-button class="button-green" @click="addPatient" size="small" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { createFollowPlan } from '../../../../utils/followup/followapis'
  import { mapState, mapActions} from 'vuex';
  export default {
    name: "add-patient-time",
    props: {
      showThisPage: { //是否显示model
        type: Boolean,
        default: function () {
          return false;
        }
      },
      addPatientData: {
        type: Object,
        default: function () {
          return {};
        }
      },

    },
    data() {
      return {
        showAddPatientTime: true,
        rules: {
          joinTime: [
            { required: true, message: '请选择履约开始时间', trigger: 'change' },
          ],
        }
      }
    },
    mounted() {
      //this.getFollowStartTimeList(this.$route.query.planId);// 获取履约时间列表
    },
    computed: {
      ...mapState('planManage', {
        //followStartTimeList: state => state.followStartTimeList,
      })
    },
    methods: {
      ...mapActions('planManage', ['getFollowStartTimeList', 'getResidentList']),
      clickClose() {
        this.$emit('sendJoinTime',null)
        this.$emit('closeAddPatientTime',false)
      },
      addPatient() {
        // 创建/修改履约计划
        // 4、校验居民/医生是否签署协议
        alert('创建/修改履约计划')
        // return

        this.$refs['addPatientData'].validate(valid => {
          if (valid) {
            if(this.$route.name == 'planModify') {
              this.$emit('sendJoinTime',this.addPatientData.joinTime)
              this.$emit('closeAddPatientTime',false)
            } else {
              const { id, patientIdList, joinTime } = this.addPatientData;
              createFollowPlan({
                id,
                addPatients: true,
                patientIdList,
                joinTime
              }).then((data) => {
                if(data.code == '000000') {
                  this.$message({
                    message: '添加成功！',
                    type: 'success'
                  });
                  this.getResidentList({
                    planId: this.addPatientData.id,
                    status: this.addPatientData.status
                  })
                  this.$emit('closeAddPatientTime',false)
                } else {
                  this.$message({
                    message: `${data.message}`,
                    type: 'error'
                  });
                }
              }).catch((err) => {
                this.$message({
                  message: `${err.message}`,
                  type: 'error'
                });
              });
            }

          } else {
            return false;
          }
        });

      }
    }
  }
</script>
<style scoped>
  .el-input__inner:disabled {
    background-color: #ffffff!important;
  }
</style>
<style lang="scss" scoped>
  @import '../../../../style/followup/followup-common';
  @import '../../../../style/followup/element-reset.css';

</style>
