<!-- 退料审批 -->
<template>
  <modal v-model="modal" width="1000px">
    <!--  表单主要内容  -->
    <div class="content">
      <div class="title">
        退料审批
      </div>
      <div class="sub-title border">
        <span>申请信息</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">申请人：</div>
          <div class="value">{{ data.applyUserName }}</div>
        </div>
        <div class="col">
          <div class="label">申请时间：</div>
          <div class="value">{{ data.applyTime }}</div>
        </div>
        <div class="col"></div>
      </div>
      <div class="sub-title border">
        <span>申请明细</span>
      </div>
      <div class="row">
        <div class="table" style="width: 100%;padding: 0;height: 400px;max-height: 400px;overflow: hidden">
          <my-table
            :data="data.materialItems"
            :columns="columns"
            :is-show-filter="false"
            :tooltip="false"
            :is-show-page="false">
          </my-table>
        </div>
      </div>
    </div>
    <div class="footer" slot="footer">
      <Button @click="cfm" class="btn" type="primary">确认</Button>
    </div>
  </modal>
</template>

<script>
import MyTable from '_c/my-table/my-table'
import {
  GetApplySceneBack,
  CheckApplySceneBack
} from '@/api/stock'
import { GetContractBatchPageList } from '@/api/baseData'
import { openPic } from '@/libs/util'

export default {
  name: 'contract-form',
  data () {
    return {
      modal: false,
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '供应商',
          key: 'supplierName',
          minWidth: 150
        },
        {
          title: '数量',
          key: 'quantity',
          minWidth: 150
        },
        {
          title: '原因',
          key: 'applyReason',
          minWidth: 150
        },
        {
          title: '图片',
          key: 'imageUrls',
          minWidth: 200,
          render: (h, params) => {
            let arr = params.row.imageUrls.split(',')
            let els = []
            arr.forEach(item => {
              els.push(
                h('p', {
                  style: {
                    cursor: 'pointer',
                    color: 'blue',
                    padding: '3px 0',
                    textDecoration: 'underline'
                  },
                  on: {
                    click () {
                      openPic(item)
                    }
                  }
                }, item.split('/')[item.split('/').length - 1])
              )
            })
            return h('div', els)
          }
        },
        {
          title: '合同批次',
          key: 'batchNo',
          minWidth: 200,
          fixed: 'right',
          render: (h, params) => {
            let _this = this
            let options = params.row.batchNoOption.map(item => {
              return h('Option', {
                props: {
                  value: item.value,
                  transfer: true
                }
              }, item.label)
            })
            return h('Select', {
              on: {
                'on-change' (val) {
                  let obj = params.row.batchNoOption.find(item => item.batchNo === val)
                  _this.data.materialItems[params.row._index].purchaseBillNo = obj.purchaseBillNo
                  _this.data.materialItems[params.row._index].batchNo = obj.batchNo
                  _this.data.materialItems[params.row._index].supplierId = obj.supplierId
                  _this.data.materialItems[params.row._index].price = obj.price
                }
              }
            }, options)
          }
        },
        {
          title: '可用判定',
          key: 'sceneBackType',
          minWidth: 200,
          fixed: 'right',
          render: (h, params) => {
            let _this = this
            let options = this.sceneBackType.map(item => {
              return h('Option', {
                props: {
                  value: item.value,
                  transfer: true
                }
              }, item.label)
            })
            return h('Select', {
              on: {
                'on-change' (val) {
                  _this.data.materialItems[params.row._index].sceneBackType = val
                  console.log(val)
                }
              }
            }, options)
          }
        }
      ],
      data: {},
      sceneBackType: [
        { label: '可用', value: 0 },
        { label: '不可用-退供应商', value: 1 },
        { label: '不可用-报废', value: 2 },
        { label: '驳回', value: 3 }
      ]
    }
  },
  components: {
    MyTable
  },
  props: {
    id: {},
    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.getlist()
      } else {
        this.data = {}
      }
    }
  },
  methods: {
    /* 合同批次 */
    async getContractBatchPageList (materialCode) {
      let list = []
      await GetContractBatchPageList({
        pageIndex: 1,
        pageSize: 9999,
        materialCode
      }).then(res => {
        if (res.code === 0) {
          list = res.result.list.map(item => {
            return {
              ...item,
              label: item.batchNo,
              value: item.batchNo
            }
          })
        }
      })
      return list
    },
    getlist () {
      GetApplySceneBack({
        id: this.id
      }).then(async res => {
        if (res.code === 0) {
          let data = res.result
          let materialItems = res.result.materialItems
          for (const item of materialItems) {
            let list = []
            await this.getContractBatchPageList(item.materialCode).then(res => {
              list = res
            })
            item.batchNoOption = list
          }
          data.materialItems = materialItems
          this.data = data
        }
      })
    },
    cfm (type) {
      console.log(this.data.materialItems)
      let flag = false
      for (const item of this.data.materialItems) {
        if (item.sceneBackType === '' || item.sceneBackType === null) {
          flag = true
          break
        }
      }
      if (flag) {
        return this.$Message.error('请填写完整信息！')
      }
      CheckApplySceneBack({
        id: this.data.id,
        detailItems: this.data.materialItems
      }).then(res => {
        if (res.code === 0) {
          this.cancel()
          this.$Message.info(res.msg || '操作成功！')
          this.$emit('getlist')
        }
      })
    },
    cancel () {
      this.modal = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  padding: 10px 0;
  border-radius: 5px;
  background-color: #FFFFFF;
  &.sub-content {
    background-color: #f2f2f2;
    margin-top: 3px;
    .title {
      padding-top: 0;
    }
  }
  .title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .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;
    }
    .tabs {
      margin-bottom: 1px;
      display: flex;
      justify-content: space-between;
      border: 1px solid #5d6c7b;
      border-radius: 2px;
      font-size: 12px;
      font-weight: normal;
      .tab {
        width: 50px;
        text-align: center;
        border-right: 1px solid #5d6c7b;
        cursor: pointer;
        &:last-child {
          border-right: none;
        }
        &.active {
          background-color: rgba(22, 155, 213, 1);
          color: #FFFFFF;
        }
      }
    }
  }
  .row-wrap {
    position: relative;
    .pic {
      position: absolute;
      right: 20px;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      img {
        width: 100%;
      }
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: flex-start;
      .value {
        flex: 1;
      }
    }
  }
  .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;
    }
  }
  .table {
    padding: 0 20px;
    padding-bottom: 20px;
    max-height: 200px;
    overflow: auto;
    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;
        }
      }
    }
  }
}
/deep/ .ivu-modal-body {
  padding: 0;
  max-height: 80vh;
  overflow: auto;
}
</style>
