<template>
  <el-dialog
    title="委外派单"
    :visible.sync="entrustVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="50%"
  >
    <div class="main_container">
      <el-form
        :model="addForm"
        :rules="addFormRules"
        label-width="120px"
        ref="addFormRef"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="报修记录编号：" prop="repairsRequestCode">
              <el-input v-model="addForm.repairRequestCode" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编码：" prop="equiCode">
              <el-input v-model="addForm.equiCode" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称：" prop="equiName">
              <el-input v-model="addForm.equiName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委外单位：" prop="outsourcUnit">
              <el-input v-model="addForm.outsourcUnit"></el-input>
              <!-- <el-select
                size="small"
                style="width: 100%"
                clearable
                v-model="addForm.outsourcUnit"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                >
                </el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划完成时间：" prop="planCompletionTime">
              <el-date-picker
                v-model="addForm.planCompletionTime"
                type="date"
                style="width: 100%"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="btns">
      <el-button
        type="primary"
        size="small"
        @click="submitAdd"
        :loading="loading"
        >提交</el-button
      >
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addOutsource } from '@/api/maintenance/repair_report';
  export default {
    props: {},
    watch: {},
    data() {
      return {
        addForm: {},
        addFormRules: {
          outsourcUnit: [
            { required: true, message: '请选择委外单位', trigger: 'blur' }
          ],
          planCompletionTime: [
            { required: true, message: '请选择计划完成时间', trigger: 'change' }
          ]
        },
        entrustVisible: false,
        deptList: [],
        loading: false
      };
    },
    created() {},
    methods: {
      init(row) {
        this.addForm = {};
        this.addForm.repairRequestCode = row.code;
        this.addForm.repairRequestId = row.id;
        this.addForm.equiName = row.deviceName;
        this.addForm.equiId = row.deviceId;
        this.addForm.equiCode = row.deviceCode;
        this.addForm.status = 0;
        // this.getgys()
        this.entrustVisible = true;
      },

      // 获取供应商列表
      async getgys() {
        let res = await getAllSupplier();
        this.deptList = res.data;
      },

      handleClose() {
        this.$refs.addFormRef.resetFields();
        this.entrustVisible = false;
      },

      submitAdd() {
        this.$refs.addFormRef.validate((valid) => {
          if (valid) {
            this.loading = true;
            addOutsource(this.addForm)
              .then((res) => {
                this.loading = false;
                if (res.code == '0') {
                  this.handleClose();
                  this.$message.success('委派成功！');
                  this.$emit('refresh');
                }
              })
              .catch((e) => {
                this.loading = false;
              });
          }
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .dialog_top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }
  .btns {
    text-align: right;
    margin: 10px 0;
  }
</style>
