<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="doctorVisible"
      @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="分诊科室"
          class="required-label"
          prop="triageDepartmentId"
        >
          <el-select
            v-model="model.triageDepartmentId"
            placeholder="请选择分诊科室"
            clearable
            filterable
            style="width:56%;height: 32px;line-height: 32px;"
            @change="change"
          >
            <el-option
              v-for="item in depList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="备注">
          <el-col :span="15">
            <el-input
              v-model="model.triageRemark"
              type="textarea"
              style="width: 90%;"
              size="small"
              minlength="2"
              maxlength="500"
              placeholder="请输入备注"
            />
          </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"
          @click="confirm"
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {updateDiagnosis, getDepList} from '../../utils/diagnosis';
  let vm = null;
  export default {
    props: {
      doctorVisible: {
        type: Boolean,
        default: false
      },
      diagnoseLogId: {
        type: Number,
        default: 0
      },
      bizType: {
        type: Number,
        default: 0
      },
      triageDepartmentId: {
        type: Number | String,
        default: 0
      },
    },
    data() {
      return {
        show: false,
        title: '设置分诊科室',
        confirmTxt: '确定',
        cancleTxt: '',
        _promise: null,
        depList: [],
        model: {
          triageRemark: '',
          triageDepartmentId: '',
          triageDepartment:''
        },
        addRules: {
          triageDepartmentId: [
            {required: true, message: '请选择分诊科室', trigger: 'change'}
          ],
        }
      };
    },
    watch:{
      triageDepartmentId:function () {
        if(this.triageDepartmentId == 0 || this.triageDepartmentId == null) {
          this.model.triageDepartmentId = '';
          if(this.$refs.setForm) {
            this.$refs.setForm.resetFields();
          }
        }
        else {
          this.model.triageDepartmentId = this.triageDepartmentId;
          this.change(this.triageDepartmentId);
        }
      }
    },
    created() {
      vm = this;
      this.getDepList();
    },
    methods: {
      getDepList() {
        getDepList().then(function (res) {
          if (res.code == '000000') {
            vm.depList = res.data;
          } else {
            vm.$message.error(res.message);
          }
        }).catch(function (error) {
          vm.$message.error(res.message);
        });
      },
      confirm() {
        this.$refs.setForm.validate((valid) => {
          if (valid) {
            const req = {
              id: this.diagnoseLogId,
              bizType: this.bizType,
              triageDepartmentId:this.model.triageDepartmentId,
              triageDepartment:this.model.triageDepartment,
              triageRemark: this.model.triageRemark
            };
            updateDiagnosis(req).then(function (res) {
              if (res.code == '000000') {
                vm.cancel();
                vm.$emit('search');
              } else {
                vm.$message.error(res.message);
              }
            }).catch(function (error) {
              vm.$message.error(res.message);
            });
          }
        });
      },

      cancel() {
        this.triageDepartmentId = null;
        this.model.triageDepartmentId = '';
        this.model.triageRemark = '';
        this.$emit('update:doctorVisible', false);
      // this.doctorVisible = false;
      },
      change(data) {
        for (let i = 0; i < this.depList.length; i++) {
          if(data == this.depList[i].id) {
            this.model.triageDepartment = this.depList[i].name;
            break;
          }
        }
      }
    }
  };
</script>

<style lang="scss" scoped>

</style>
