<template>
  <div class="n-app-container">
    <el-card shadow="never">
      <el-form>
        <el-form-item label="导入模式">
          <el-switch
            v-model="onCustomer"
            active-text="客户"
            inactive-text="分公司"
            change="changeSwitch"
          />
        </el-form-item>
        <el-form-item v-if="!onCustomer" label="按分公司">
          <el-select v-model="dto.BranchCompanyId" multiple filterable>
            <el-option
              v-for="item in branchCompanyList"
              :key="item.Id"
              :label="item.CompanyName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="onCustomer" label="涉及客户">
          <el-select v-model="dto.CustomerId" multiple filterable>
            <el-option
              v-for="item in customerList"
              :key="item.Id"
              :label="item.CompanyName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="涉及渠道">
          <el-select v-model="dto.ExpressId" filterable>
            <el-option
              v-for="item in channelList"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生效时间">
          <el-date-picker v-model="dto.EnableTime" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="分区模式">
          <el-select v-model="dto.CalculateType" filterable>
            <el-option
              v-for="item in calculateTypeList"
              :key="item.Name"
              :label="item.Name"
              :value="item.Value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="新客户引用">
          <el-checkbox v-model="dto.CanCopy" />
        </el-form-item>
        <el-form-item label="报价文件">
          <el-upload
            class="upload-demo"
            :action="`${apiAddress2}/api/Finance/updateLoadFile`"
            :on-success="changeTempUrl"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-button :loading="inputQuotaLadding" @click="inputQuota">
          调整报价
        </el-button>
        <el-button :loading="outQuotaLadding" @click="outQuota">
          按所选导出文件
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { _getCustomers, _getCalculateTypes, _getAllDeliverChannel } from '@/api/baseInfo'
import { _getAllBranchCompanyCustomers } from '@/api/customer'
import { _batchUpdateFeeBase, _batchSendFeeBase } from '@/api/finance'

const apiAddress2 = 'http://120.24.149.148'

export default {

  data() {
    return {
      inputQuotaLadding: false,
      outQuotaLadding: false,
      apiAddress2: apiAddress2,
      dto: {
        CustomerId: [],
        BranchCompanyId: []
      },
      onCustomer: false,
      customerList: [],
      channelList: [],
      calculateTypeList: [],
      branchCompanyList: []
    }
  },
  created() {
    _getAllBranchCompanyCustomers().then(r => {
      this.branchCompanyList = r
    })
    _getCustomers().then(r => {
      this.customerList = r.filter(x => {
        return x.IsActive
      }
      )
    })
    _getCalculateTypes().then(r => {
      this.calculateTypeList = r
    })
    _getAllDeliverChannel().then(r => {
      this.channelList = r
    })
  },
  methods: {
    changeSwitch(v) {
      if (v) {
        this.dto.BranchCompanyId = []
      } else {
        this.dto.CustomerId = []
      }
    },
    inputQuota() {
      this.inputQuotaLadding = true
      _batchUpdateFeeBase(this.dto).then(r => {
        var message = ''
        if (r.ErrorItems.length > 0) {
          for (var i = 0; i < r.ErrorItems.length; i++) {
            message += r.ErrorItems[i].CustomerName + ' ' + r.ErrorItems[i].ErrorMessage +
            '\r\n'
          }
          this.$alert(message)
        } else {
          this.$alert('导入成功')
        }
      }).finally(r => {
        this.inputQuotaLadding = false
      })
    },
    outQuota(r) {
      this.outQuotaLadding = true
      _batchSendFeeBase(this.dto).then(r => {
        this.$alert('导出成功，请等待钉钉报价')
      }).finally(r => {
        this.outQuotaLadding = false
      })
    },
    changeTempUrl(r) {
      this.dto.FileUrl = r.Data.url
    }
  }

}
</script>
