<!-- 维护记录添加窗口 -->
<template>
  <modal v-model="modal" width="600px">
    <div class="title" slot="header">
      添加维护记录
    </div>
    <div slot="footer" class="footer">
      <Button @click="cfm" class="btn" type="primary">提交</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
    <!--  表单主要内容  -->
    <div class="content" ref="print">
      <div class="sub-title border">
        <span>维护记录</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">日期：</div>
          <div class="value">
            {{ $dayjs().format('YYYY-MM-DD') }}
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">维护记录：</div>
          <div class="value">
            <Input v-model="form.title" type="textarea"/>
          </div>
        </div>
      </div>
      <div class="sub-title border">
        <span>附件</span>
      </div>
      <div class="row">
        <div class="col">
          <Upload
            ref="upload"
            :before-upload="upload"
            :format="['jpg','jpeg','png']"
            accept=".jpg,.png,.jpeg"
            action=""
            style="display: inline">
            <Button type="primary">添加</Button>
          </Upload>
        </div>
      </div>
      <div class="row" v-for="(item, index) in picList" :key="index">
        <div class="col">
            {{ item }}<Icon type="ios-close" size="24" style="padding: 0 20px;cursor: pointer" @click="del(index)" />
        </div>
      </div>
    </div>
  </modal>
</template>

<script>
import { GetMaterialSet } from '@/api/baseData'
import { UploadBase64 } from '@/api/upload'
export default {
  name: 'quality-form',
  data () {
    return {
      picList: [],
      form: {
        title: '',
        picUrl: '',
        createdTime: this.$dayjs().format('YYYY-MM-DD')
      },
      modal: false
    }
  },
  components: {
  },
  props: {
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        this.picList = []
        this.form.title = ''
        GetMaterialSet().then(res => {
          if (res.code === 0) {
            this.delayPeriod = res.result.delayPeriod
            this.everyDayCheckCount = res.result.everyDayCheckCount
          }
        })
      }
    }
  },
  methods: {
    // 图片上传
    upload (file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = (event) => {
        UploadBase64({
          fileBase64: event.srcElement.result,
          fileName: file.name,
          floder: 'order'
        }).then(res => {
          if (res.code === 0) {
            this.picList.push(res.result)
            // this.form.picUrl = res.result
          }
        })
      }
      return false
    },
    del (index) {
      this.picList.splice(index, 1)
    },
    cfm () {
      if (this.picList.length < 1) {
        return this.$Message.error('请选择文件！')
      }
      this.form.picUrl = this.picList.join()
      this.$emit('getData', this.form)
      this.modal = false
    },
    cancel () {
      this.form = {
        title: '',
        picUrl: '',
        createdTime: this.$dayjs().format('YYYY-MM-DD')
      }
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .label {
        width: 60px;
      }
      .value {
        flex: 1;
      }
      .table {
        width: 100%;
        table {
          border-collapse:collapse;
          border:none;
          width: 100%;
          tr {
            td,th {
              padding: 5px;
              text-align: center;
              border:solid #dcdee2 1px;
              position: relative;
              font-size: 12px;
              th {
                background-color: #eeeeee;
              }
            }
            &.green {
              color: #4B7902;
            }
            &.red {
              color: red;
            }
            &.orange {
              color: #B8741A;
            }
          }
        }
      }
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #FFFFFF;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
}
</style>
