import { required, mobileRequired, mobile, httpUrl, email } from '@/components/template/rules_verify.js'
import { getWebsit } from '@/api/customerManagement.js'
import ImageUpload from '@/components/file-upload'
import { storageListPageV2 } from '@/api/storage.js'
export default {
  data() {
    return {
      websitList: [],
      warehouseList: []
    }
  },
  computed: {
    formItems() {
      return [
        {
          name: 'el-select',
          md: 6,
          options: [
            { value: 'SAVE', label: '保存' },
            { value: 'SUBMIT', label: '提交' },
            { value: 'OK', label: '通过' },
            { value: 'FAIL', label: '失败' }
          ],
          attributes: {
            placeholder: '请选择',
            disabled: true
          },
          formItemAttributes: {
            label: '单据状态',
            prop: 'flag'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: true
          },
          formItemAttributes: {
            label: '单据编号',
            prop: 'partsRetId'
          }
        },
        {
          name: 'el-select',
          md: 6,
          options: this.websitList,
          attributes: {
            placeholder: '请选择',
            clearable: true,
            filterable: true,
            disabled: this.openType == 1 || !!~['OK', 'FAIL'].indexOf(this.formData.flag)
          },
          formItemAttributes: {
            label: '网点名称',
            prop: 'websitId',
            rules: [...required]
          },
          events: {
            change: val => {
              this.formData.websitName = this.websitList.find(item => item.value == val)?.label || ''
              this.formData.websitPartsRetItems = []
              this.formData.storageName = ''
              this.formData.storageId = ''
              this.getProductSel()
              this.getWarehouseList()
            }
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: this.openType == 1 || !!~['OK', 'FAIL'].indexOf(this.formData.flag)
          },
          formItemAttributes: {
            label: '返厂单位',
            prop: 'factory'
          }
        },
        {
          name: 'el-input',
          md: 12,
          attributes: {
            placeholder: '请输入',
            disabled: this.openType == 1 || !!~['OK', 'FAIL'].indexOf(this.formData.flag)
          },
          formItemAttributes: {
            label: '返厂地址',
            prop: 'factoryAddress'
          }
        },
        {
          name: 'slot-component',
          md: 6,
          formItemAttributes: {
            label: '出库仓库',
            prop: 'storageId',
            rules: [...required]
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return (
              <el-select
                style="width:100%"
                value={this.formData.storageId}
                onInput={val => {
                  this.formData.storageId = val
                }}
                placeholder="请选择"
                disabled={this.openType == 1 || !!~['OK', 'FAIL'].indexOf(this.formData.flag)}
                onChange={val => {
                  if (val) {
                    this.formData['storageName'] = this.warehouseList.find(item => item.storageId == val).storageName
                  } else {
                    this.formData['storageName'] = ''
                  }
                }}
              >
                {this.warehouseList.map(item => (
                  <el-option key={item.storageId} label={item.storageName} value={item.storageId}></el-option>
                ))}
              </el-select>
            )
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: true
          },
          formItemAttributes: {
            label: '制单人',
            prop: 'createBy'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: true
          },
          formItemAttributes: {
            label: '制单时间',
            prop: 'createTime'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: true
          },
          formItemAttributes: {
            label: '审核人',
            prop: 'confirmBy'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            placeholder: '请输入',
            disabled: true
          },
          formItemAttributes: {
            label: '审核时间',
            prop: 'confirmTime'
          }
        },
        {
          md: 24,
          isShow: true,
          name: 'slot-component',
          formItemAttributes: {
            label: '附件图片',
            prop: 'fileUrl',
            rules: []
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return (
              <ImageUpload
                fileList={this.formData.fileUrl}
                uid="imgSrc677766_materials_drawing_images_iuhu"
                limit={1}
                isEdit={this.openType != 1 && !~['OK', 'FAIL'].indexOf(this.formData.flag)}
              />
            )
          }
        },
        {
          name: 'el-input',
          md: 24,
          formItemAttributes: { label: '备注', prop: 'remark' },
          attributes: {
            placeholder: '备注',
            disabled: this.openType == 1 || !!~['OK', 'FAIL'].indexOf(this.formData.flag)
          }
        }
      ]
    }
  },
  methods: {
    getWebsitList() {
      getWebsit({ type: 'C', status: true }).then(res => {
        this.websitList = res.data.map(item => ({
          label: item.name,
          value: item.websitId,
          data: item
        }))
      })
    },
    getWarehouseList() {
      if (!this.formData.websitId) {
        return (this.warehouseList = [])
      }
      storageListPageV2({
        pageNum: 1,
        pageSize: -1,
        params: [
          { param: 'a.type', compare: 'like', value: '配件' },
          { param: 'a.status', compare: '=', value: 'true' },
          { param: 'a.websit_id', compare: '=', value: this.formData.websitId }
        ]
      }).then(res => {
        this.warehouseList = res.data.records
      })
    }
  }
}
