<template>
  <transition name="dialog-fade">
    <el-dialog
      v-if="showDialog"
      :title="dialogTitle"
      class="dialog-component"
      v-model="showDialog"
      width="640px"
      @close="closeDialog(0)"
    >
      <el-form
        ref="formInfo"
        :model="formInfo"
        class="demo-form-inline"
        label-width="140px"
        :rules="rules"
      >
        <el-form-item label="工单类型：" prop="workType">
          <el-select
            v-model="formInfo.workType"
            filterable
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="站点：" prop="stationName">
          <el-select
            v-model="formInfo.stationName"
            filterable
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备：" prop="deviceName">
          <el-select
            v-model="formInfo.deviceName"
            filterable
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作描述：" prop="workDes">
          <el-input v-model="formInfo.workDes"></el-input>
        </el-form-item>
       <el-form-item label="图片" prop="delivery">
                <el-upload
                  action="#"
                  list-type="picture-card"
                  :auto-upload="false"
                  :limit="3"
                >
                <!-- slot="default" -->
                  <i  class="el-icon-plus"></i>
                  <!-- :default-slot="{ file }" -->
                  <div  >
                    <!-- <img
                      class="el-upload-list__item-thumbnail"
                      :src="file.url"
                      alt=""
                    /> -->
                    <span class="el-upload-list__item-actions">
                      <span
                        class="el-upload-list__item-preview"
                        @click="handlePictureCardPreview(file)"
                      >
                        <!-- <i class="el-icon-zoom-in"></i> -->
                      </span>
                      <span
                        v-if="!disabled"
                        class="el-upload-list__item-delete"
                        @click="handleRemove(file)"
                      >
                        <i class="el-icon-delete"></i>
                      </span>
                    </span>
                  </div>
                </el-upload>
                <el-dialog v-model="dialogVisible">
                  <img width="100%" :src="dialogImageUrl" alt="" />
                </el-dialog>
              </el-form-item>

        <el-form-item label="期望完成时间：" prop="pointNum">
          <el-date-picker
            v-model="formInfo.date1"
            type="date"
            placeholder="请选择"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注：" prop="deviceNum">
          <el-input v-model="formInfo.deviceNum" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="运维班组/执行人：" prop="siteList">
          <el-select
            v-model="formInfo.siteList"
            multiple
            filterable
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <br />
        <br />
        <br />
        <div style="text-align: right">
          <el-button @click="closeDialog(0)">取消</el-button>
          <el-button type="primary" @click="submitForm('formInfo')">
            保存
          </el-button>
        </div>
      </el-form>
    </el-dialog>
  </transition>
</template>

<script>
export default {
  name: 'DialogComponent',
  props: {
    dialogTitle: {
      type: String,
      default: '新增',
    },
    itemInfo: {
      type: Object,
      default: function () {
        return {}
      },
    },
  },
  data() {
    return {
      showDialog: false,
      formInfo: JSON.parse(JSON.stringify(this.itemInfo)),

      dialogImageUrl: '',
      dialogVisible: false,

      options: [
        {
          value: '选项1',
          label: '站点一',
        },
        {
          value: '选项2',
          label: '站点二',
        },
        {
          value: '选项3',
          label: '站点三',
        },
        {
          value: '选项4',
          label: '站点四',
        },
        {
          value: '选项5',
          label: '站点五',
        },
      ],

      rules: {
        workType: [
          // required  是否为必填项， trigger:表单验证的触发时机，失去焦点进行验证
          { required: false, message: '请输入工单类型', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        stationName: [
          { required: true, message: '请输入站点', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        stationAddress: [
          { required: true, message: '请输入台区地址', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        siteList: [
          {
            required: true,
            message: '请选择运维班组/执行人',
            trigger: 'change',
          },
        ],
        pointNum: [
          { required: true, message: '请输入已选站点个数', trigger: 'blur' },
          { trigger: 'blur' },
        ],
        deviceNum: [
          { required: true, message: '请输入挂载设备个数', trigger: 'blur' },
          { trigger: 'blur' },
        ],
        date1: [
          {
            type: 'date',
            required: true,
            message: 'Please pick a date',
            trigger: 'change',
          },
        ],
      },
    }
  },
  methods: {

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },


    roleValid(rule, value, callback) {
      rule
      if (value.length === 0) {
        callback(new Error('角色不能为空'))
      } else {
        callback()
      }
    },

    // onSelectedDrug(event) {
    //   this.siteList = event;
    //   console.log(this.siteList);
    // },
    // 保存操作
    submitForm(formName) {
      const params = Object.assign(this.formInfo, {})
      params
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 走保存请求
          this.$message({
            message: '操作成功！',
            type: 'success',
          })
          this.closeDialog(1)
        } else {
          return false
        }
      })
    },
    // 关闭弹框
    closeDialog(flag) {
      this.$refs['formInfo'].resetFields()
      this.showDialog = false
      this.$emit('closeDialog', flag)
    },
  },
}
</script>
 
<style scoped lang="scss">
.el-input,
.el-select {
  width: 240px;
}
</style>