import { listPageV2 } from "@/api/workOrder/orderType";
import { getWebsit } from "@/api/customerManagement.js"
import {
  websitGoodsAlonePList,
  websitGoodsAloneMList
} from '@/api/auxiliaryMaterialBasicInfo.js'
import { required, mobileRequired, mobile, httpUrl, email } from '@/components/template/rules_verify.js'
export default {
  data() {
    return {
      orderSmallTypeData: [],
      goodsList: [],
      websitList: []
    }
  },
  computed: {
    serviceInfo() {
      return [
        {
          md: 6,
          name: 'el-input',
          attributes: { placeholder: '请输入', disabled: true },
          formItemAttributes: {
            label: '所属商户',
            prop: 'companyWechatName',
            rules: []
          }
        },
        {
          name: 'el-select',
          options: this.websitList,
          md: 6,
          attributes: {
            disabled: !!this.id,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '网点名称',
            prop: 'websitId',
            rules: [...required],
          },
          events: {
            change: (val) => {
              this.formData.websitName = this.websitList.find(item => item.value === val)?.label
            }
          }
        },
        {
          name: 'el-select',
          md: 6,
          options: this.orderSmallTypeData,
          attributes: {
            disabled: !!this.id,
            placeholder: '请选择',
            clearable: true,
            filterable: true
          },
          formItemAttributes: {
            label: '工单类型',
            prop: 'orderSmallType',
            rules: [...required]
          },
          events: {
            change: (val) => {
              this.formData.orderSmallTypeText = this.orderSmallTypeData.find(item => item.value === val)?.label
            }
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.id,
          },
          formItemAttributes: {
            label: '工程编号',
            prop: 'projectNo',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.id,
          },
          formItemAttributes: {
            label: '工程名称',
            prop: 'projectName',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.id,
          },
          formItemAttributes: {
            label: '销售单号',
            prop: 'salesNo',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.id,
          },
          formItemAttributes: {
            label: '销售网点',
            prop: 'salesWebsit',
            rules: [...required]
          }
        },
        {
          name: 'el-radio',
          md: 6,
          options: [{ value: "M", label: "辅材" }, { value: "P", label: "配件" }],
          attributes: {
            disabled: !!this.id,
            placeholder: '请选择',
          },
          formItemAttributes: {
            label: '费用类型',
            prop: 'goodsType',
            rules: [...required]
          },
          events: {
            change: (val) => {
              this.formData.items = []
              this.gettGoodsAloneList()
            }
          }
        },
      ]
    }
  },
  created() {
    // 获取网点
    getWebsit({ type: "C", status: true, isAll: this.id ? true : false }).then(res => {
      this.websitList = res.data.map(item => ({
        label: item.name,
        value: item.websitId,
        data: item
      }))
    })
    // 获取工单类型
    listPageV2({ "pageNum": 1, "pageSize": -1, "params": [{ "param": "a.status", "compare": "=", "value": "true" }] }).then(res => {
      this.orderSmallTypeData = res.data.records.map(item => ({
        value: item.id,
        label: item.orderSmallTypeText
      }))
    })
  },
  methods: {
    gettGoodsAloneList() {
      this.goodsList = []
      if (this.formData.goodsType) {
        ({
          M: websitGoodsAloneMList,
          P: websitGoodsAlonePList
        })[this.formData.goodsType]({ "pageNum": 1, "pageSize": -1, "params": [{ "param": "a.status", "compare": "=", "value": "ON" }] }).then(res => {
          this.goodsList = res.data.records
        })
      }
    }
  },
}
