<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operation="operation()"
  >
    <div class="cartographer_big">
      <el-dialog title="配置" width="100%" :modal="false" :visible.sync="formBool" :before-close="handleClose">
        <zj-page-container v-if="formBool">
          <zj-page-fill class="neibuview">
            <zj-form-container
              ref="formRef"
              :form-data="formData"
              :form-rules="formRules"
              :form-attributes="{ size: 'mini' }"
            >
              <zj-form-module title="基本项" label-width="150px" :form-data="formData" :form-items="items" />
              <zj-form-module
                v-if="isDefaultDisabledShow"
                title="销售价格倍率"
                label-width="150px"
                :form-data="formData"
                :form-items="items2"
              />
            </zj-form-container>
          </zj-page-fill>
          <!-- 操作按钮 -->
          <div style="box-sizing: border-box; padding: 10px; text-align: right">
            <el-button size="mini" @click="handleClose">取 消</el-button>
            <el-button v-if="~[0, 1].indexOf(formType)" size="mini" @click="formConfirm" type="primary"
              >确 定</el-button
            >
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import {
  partsRefundConfigList,
  partsRefundConfigAdd,
  partsRefundConfigEdit,
  partsRefundConfigBatchDel,
  partsRefundConfigDetail,
  partsRefundConfigGetDefault
} from '@/api/parts-config'
import operation_mixin from '@/components/template/operation_mixin.js'
import { required, requiredValueMin } from '@/components/template/rules_verify.js'
import { getWebsit } from '@/api/customerManagement.js'
export default {
  components: { TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      formBool: false,
      formType: 0,
      formData: {
        companyWechatId: '',
        companyWechatName: '',
        isDefault: false,
        newRefundInner: 0,
        newRefundOutside: 0,
        oldRefund: 0,
        priceRate1: 0,
        priceRate2: 0,
        priceRate3: 0,
        websitId: '',
        websitName: ''
      },
      formRules: {},
      websitList: []
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                getWebsit({ type: 'C', status: true, queryPartsWebsit: true }).then(res => {
                  this.websitList = res.data
                  this.formType = 0

                  if (this.websitList.length) {
                    var data = this.websitList?.[0]
                    this.formData.websitId = data?.websitId || ''
                    this.formData.websitName = data?.name || ''
                    this.formData.newRefundInner = ''
                    this.formData.newRefundOutside = ''
                  }

                  this.$nextTick(() => {
                    this.formBool = true
                  })
                })
              }
            })
          ],
          [
            this.optionsEvensAuth('defaultConfig', {
              click: () => {
                getWebsit({ type: 'C', status: true, queryPartsWebsit: true }).then(res => {
                  this.websitList = res.data
                  this.formType = this.$route.name == 'website-refund-config' ? 2 : 1
                  partsRefundConfigGetDefault().then(res => {
                    this.formData = res.data
                    this.$nextTick(() => {
                      this.formBool = true
                    })
                  })
                })
              }
            })
          ]
        ]
      ]
    },
    isDefaultDisabledShow() {
      return this.formData.isDefault || this.formType !== 0
    },
    isDefaultDisabled() {
      return this.formData.isDefault && this.$route.name == 'center-parts-config'
    },
    websiteRefundConfigDisabled() {
      return (
        !this.formData.isDefault &&
        this.$route.name == 'website-refund-config' &&
        (this.formType == 1 || this.formType == 0)
      )
    },
    centerPartsConfigDisabled() {
      return (
        !this.formData.isDefault &&
        this.$route.name == 'center-parts-config' &&
        (this.formType == 1 || this.formType == 0)
      )
    },
    items() {
      return [
        {
          isShow: !this.formData.isDefault,
          name: 'el-select',
          md: 8,
          options: this.websitList.map(item => ({ label: item.name, value: item.websitId })),
          attributes: {
            clearable: true,
            filterable: true,
            disabled: !(this.isDefaultDisabled || this.websiteRefundConfigDisabled || this.centerPartsConfigDisabled),
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '配置网点',
            prop: 'websitId',
            rules: this.formData.isDefault ? [] : [...required]
          },
          events: {
            change: val => {
              var data = this.websitList.find(item => item.websitId === val)
              this.formData.websitName = data?.name || ''
              this.formData.newRefundInner = ''
              this.formData.newRefundOutside = ''
            }
          }
        },
        {
          isShow: !this.formData.isDefault,
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div></div>
          }
        },
        {
          isShow: this.formData.websitId
            ? this.websitList.find(item => item.websitId === this.formData.websitId)?.partsWebsitAttr
            : true,
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !(this.isDefaultDisabled || this.websiteRefundConfigDisabled || this.centerPartsConfigDisabled),
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '新件返还（市区）',
            prop: 'newRefundInner',
            rules: [...required]
          }
        },
        {
          isShow: this.formData.websitId
            ? this.websitList.find(item => item.websitId === this.formData.websitId)?.partsWebsitAttr
            : true,
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>天内返还不使用的新件</div>
          }
        },
        {
          isShow: this.formData.websitId
            ? !this.websitList.find(item => item.websitId === this.formData.websitId)?.partsWebsitAttr
            : true,
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !(this.isDefaultDisabled || this.websiteRefundConfigDisabled || this.centerPartsConfigDisabled),
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '新件返还（外区）',
            prop: 'newRefundOutside',
            rules: [...required]
          }
        },
        {
          isShow: this.formData.websitId
            ? !this.websitList.find(item => item.websitId === this.formData.websitId)?.partsWebsitAttr
            : true,
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>天内返还不使用的新件</div>
          }
        },
        {
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !(this.isDefaultDisabled || this.websiteRefundConfigDisabled || this.centerPartsConfigDisabled),
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '旧件返还',
            prop: 'oldRefund',
            rules: [...required]
          }
        },
        {
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>天内返维修完工时替换的旧件</div>
          }
        }
      ]
    },
    items2() {
      return [
        {
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !this.isDefaultDisabled,
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '空调',
            prop: 'priceRate1',
            rules: this.$route.name == 'website-refund-config' ? [] : requiredValueMin(1)
          }
        },
        {
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>销售价格倍率设置，注需要大于等于 1</div>
          }
        },
        {
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !this.isDefaultDisabled,
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '生活电器',
            prop: 'priceRate3',
            rules: this.$route.name == 'website-refund-config' ? [] : requiredValueMin(1)
          }
        },
        {
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>销售价格倍率设置，注需要大于等于 1</div>
          }
        },
        {
          name: 'el-input',
          md: 8,
          attributes: {
            disabled: !this.isDefaultDisabled,
            placeholder: '请输入',
            type: 'number',
            min: 1
          },
          formItemAttributes: {
            label: '冰箱',
            prop: 'priceRate2',
            rules: this.$route.name == 'website-refund-config' ? [] : requiredValueMin(1)
          }
        },
        {
          name: 'slot-component',
          md: 16,
          attributes: {},
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            return <div>销售价格倍率设置，注需要大于等于 1</div>
          }
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList: partsRefundConfigList,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 获取勾选框数据
    selectionChange(data) {
      this.recordSelected = data
    },
    handleClose() {
      this.$refs?.pageRef?.refreshList()
      this.$data.formData = this.$options.data().formData
      this.formType = 0
      this.formBool = false
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          ;(this.formData.id ? partsRefundConfigEdit : partsRefundConfigAdd)(this.formData).then(res => {
            this.$message({ type: 'success', message: '配置成功!' })
            this.$refs.pageRef.refreshList()
            this.handleClose()
          })
        }
      })
    },
    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            getWebsit({ type: 'C', status: true, queryPartsWebsit: true }).then(res => {
              this.websitList = res.data
            })
            partsRefundConfigDetail({
              id: row.id
            }).then(res => {
              this.formData = res.data
              this.$nextTick(() => {
                this.formType = 2
                this.formBool = true
              })
            })
          }
        },
        edit: {
          click: ({ row, index, column }) => {
            getWebsit({ type: 'C', status: true, queryPartsWebsit: true }).then(res => {
              this.websitList = res.data
            })
            partsRefundConfigDetail({
              id: row.id
            }).then(res => {
              this.formData = res.data
              this.$nextTick(() => {
                this.formType = 1
                this.formBool = true
              })
            })
          }
        },
        del: {
          prompt: '确定删除吗？',
          click: ({ row, index, column }) => {
            partsRefundConfigBatchDel([row.id]).then(res => {
              this.$message({ type: 'success', message: '删除成功!' })
              this.$refs.pageRef.refreshList()
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;

  ::v-deep & > .zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    & > div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>
