<template>

  <div>
    <el-dialog
      :title="brandName"
      :visible.sync="dialogVisible"
      width="900px"
      :before-close="handleClose()">
      <el-tabs v-model="activeName">
        <el-tab-pane label="未授权客户" name="first">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="关键字">
              <el-input v-model="list1QueryParams.customerKey" placeholder="请填入关键字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="filterCustomer">查询</el-button>
            </el-form-item>
          </el-form>

          <el-table
            :data="list1"
            style="width: 100%">
            <el-table-column
              prop="customerName"
              label="客户名称">
            </el-table-column>
            <el-table-column
              prop="phone"
              label="客户号码">
            </el-table-column>
            <el-table-column
              width="100px"
              label="操作">
              <template v-slot="{row}">
                <el-button type="primary" size="mini" @click="handleAdd(row)">添加</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total1 > 0" :total="total1" :page.sync="list1QueryParams.pageNo" :limit.sync="list1QueryParams.pageSize"
                      @pagination="getList1" style="height: 40px"/>
        </el-tab-pane>
        <el-tab-pane label="已授权客户" name="second">
          <el-table
            :data="list2"
            style="width: 100%">
            <el-table-column
              prop="customerName"
              label="客户名称">
            </el-table-column>
            <el-table-column
              prop="phone"
              label="客户号码">
            </el-table-column>
            <el-table-column
              prop="fileUrl"
              label="授权证明">
              <template v-slot="{row}">
                <div v-if="!!row.fileUrl && row.fileUrl.length > 0">
                  <span v-for="(item, index) in row.fileUrl.split(',')">
                    <a :href="item" target="_blank">附件{{ index + 1 }}</a>,
                  </span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="startTime"
              width="200px"
              :formatter="(row) => parseTime(row.startTime, '{y}-{m}-{d}') + ' - ' + parseTime(row.endTime, '{y}-{m}-{d}')"
              label="授权时间">
            </el-table-column>
            <el-table-column
              prop="feeScale"
              :formatter="(row) => getDictDataLabel(DICT_TYPE.BRAND_CUSTOMER_CHARGING_MODEL, row.feeScale)"
              label="收费标准">
            </el-table-column>
            <el-table-column
              prop="createUsername"
              label="添加人">
            </el-table-column>
            <el-table-column
              width="100px"
              label="操作">
              <template v-slot="{row}">
                <el-button type="danger" size="mini" @click="handleDelete(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total2 > 0" :total="total2" :page.sync="list2QueryParams.pageNo" :limit.sync="list2QueryParams.pageSize"
                      @pagination="getList2" style="height: 40px"/>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

<!--  添加弹窗  -->
    <el-dialog
      title="添加"
      :visible.sync="addDialog.dialogVisible"
      width="500px">
      <el-form ref="form" :model="addDialog.form" label-width="80px">
        <el-form-item label="客户名称">{{ addDialog.form.customerName }}</el-form-item>
        <el-form-item label="授权时间">
          <el-date-picker
            v-model="addDialog.dateRange"
            @chang="handleDatePick"
            type="daterange"
            range-separator="至"
            value-format="timestamp"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收费模式">
          <dict-selector v-model="addDialog.form.feeScale" :type="DICT_TYPE.BRAND_CUSTOMER_CHARGING_MODEL"></dict-selector>
        </el-form-item>
        <el-form-item label="授权证明">
          <file-upload v-model="addDialog.form.fileUrl"></file-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialog.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  createProductBrandEmpower,
  deleteProductBrandEmpower,
  getProductBrandEmpowerPageAuth,
  getProductBrandEmpowerPageUnauth
} from "@/api/ecw/productBrandEmpower"
import DictSelector from "@/components/DictSelector"
import { DICT_TYPE, getDictDataLabel } from '@/utils/dict'
import FileUpload from '@/components/FileUpload'
import { parseTime } from '@/utils/ruoyi'

export default {
  name: "Empower",
  components: {
    DictSelector,
    FileUpload
  },
  props: {
    id: {
      type: Number,
      default: 0
    },
    brandName: String,
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      DICT_TYPE,
      getDictDataLabel,
      parseTime,
      dialogVisible: false,
      activeName: 'first',
      total1: 0,
      total2: 0,
      list1: [],
      list2: [],
      list1QueryParams: {
        pageNo: 1,
        pageSize: 10,
        customerKey: undefined
      },
      list2QueryParams: {
        pageNo: 1,
        pageSize: 10,
      },

      addDialog: {
        dialogVisible: false,
        customerName: '',
        form: {
          "customerContactsId": 0,
          "customerId": 0,
          "endTime": "",
          "feeScale": 0,
          "fileUrl": "",
          "productBrandId": null,
          "startTime": "",
          "status": 0
        },
        dateRange: []
      }
    }
  },
  mounted() {

  },
  methods: {
    handleAddSubmit(){
      this.addDialog.form.productBrandId = this.id
      createProductBrandEmpower(this.addDialog.form).then(() => {
        this.addDialog.dialogVisible = false
        this.getList1()
        this.getList2()

        this.resetForm("form");
      })
    },
    handleDatePick(e){
      console.log(e)
    },
    handleClose() {},
    filterCustomer() {
      this.list1QueryParams.pageNo = 1
      this.getList1()
    },
    handleAdd(row) {
      this.addDialog.form.customerId = row.customerId
      this.addDialog.form.customerName = row.customerName
      this.addDialog.dialogVisible = true
    },
    handleDelete(id) {
      this.$confirm('确认删除该用户的授权?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProductBrandEmpower(id).then(r => {
          this.getList1()
          this.getList2()
          this.$message({
            type: 'success',
            message: '撤销授权成功!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getList1(){
      getProductBrandEmpowerPageUnauth({...this.list1QueryParams, productBrandId: this.id}).then(r => {
        this.list1 = r.data.list
        this.total1 = r.data.total
      })
    },
    getList2() {
      getProductBrandEmpowerPageAuth({...this.list2QueryParams, productBrandId: this.id}).then(r => {
        this.list2 = r.data.list
      })
    }
  },
  watch: {
    visible(v){
      if (v){
        this.list1QueryParams.pageNo = 1
        this.list2QueryParams.pageNo = 1
        this.list1QueryParams.customerKey = ''
        this.getList1()
        this.getList2()
      }
      this.dialogVisible = v
    },
    dialogVisible(v) {
      this.$emit('update:visible', v)
    },

    'addDialog.dateRange'(v) {
      this.addDialog.form.startTime = v[0]
      this.addDialog.form.endTime = v[1]
    }
  }
}
</script>

<style scoped>

</style>
