<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="main">
      <div class="form_all">
        <el-form ref="select" :model="select" label-width="auto">
          <el-row :gutter="10">
            <el-col :span="4">
              <el-form-item label="供应商名称">
                <el-input v-model="select.custName" clearable />
              </el-form-item>
            </el-col>

            <el-col :span="4">
              <el-button type="primary" @click="getDataList">
                {{ '搜索' }}</el-button>
              <el-button type="" @click="reset"> {{ '重置' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            v-has-buttons="['supplierSetAdd']"
            size="small"
            class="addBtn"
            @click="openDig"
          >{{ '添加' }}</el-button>
          <el-button
            v-has-buttons="['supplierSetDelete']"
            size="small"
            style="background-color: #c9171d"
            :disabled="single"
            class="thebtn1"
            @click="handleDelete"
          >
            {{ '删除' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column align="center" prop="custCode" label="客商编码" />

          <el-table-column align="center" prop="custName" label="客商名称" />
          <el-table-column align="center" prop="shortName" label="客商简称" />
          <el-table-column align="center" prop="legalBody" label="法人姓名" />
          <el-table-column align="center" prop="linkMan" :label="'联系人'" />
          <el-table-column
            align="center"
            prop="taxpayerid"
            label="纳税人登记号"
          />
          <el-table-column
            align="center"
            prop="provinceName"
            :label="'省'"
          />
          <el-table-column
            align="center"
            prop="cityName"
            :label="'市'"
          />
          <el-table-column
            align="center"
            prop="countyName"
            :label="'区/县'"
          />
          <el-table-column align="center" prop="address" :label="'详细地址'" />
          <el-table-column align="center" prop="custTypeVal" label="供应商类型" />
          <el-table-column align="center" prop="tradeScope" label="经营范围" />
          <!-- <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建时间'"
          >
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            min-width="120px"
          >
            <template slot-scope="scope">
              <el-button
                v-has-buttons="['supplierSetUpdate']"
                type="text"
                size="small"
                class="button-s"
                style="color: #009b58"
                @click.native.prevent="update(scope.row)"
              >
                {{ '修改' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />

    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客商编码" prop="custCode">
              <el-input v-model="form.custCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客商名称" prop="custName">
              <el-input v-model="form.custName" clearable />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="客商简称" prop="shortName">
              <el-input v-model="form.shortName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人姓名" prop="legalBody">
              <el-input v-model="form.legalBody" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'联系人'" prop="linkMan">
              <el-input v-model="form.linkMan" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营范围" prop="tradeScope">
              <el-input v-model="form.tradeScope" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纳税人登记号" prop="taxpayerid">
              <el-input v-model="form.taxpayerid" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商类型" prop="custType">
              <el-select
                v-model="form.custType"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in custTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              :label="'收货地址'"
              class="dizhi"
              prop="shoppingAddress"
            >
              <el-cascader
                :key="isResouceShow"
                v-model="form.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                @change="handleChange1"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'详细地址'" class="dizhi" prop="address">
              <el-input v-model="form.address" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as api from '@/api/supply.js'
import * as site from '@/api/site.js'
export default {
  name: 'SupplierSet',
  components: {
    topBar
  },
  filters: {
    isAgree(val) {
      if (!val) {
        return '允许'
      } else {
        return '禁止'
      }
    }
  },
  data() {
    return {
      creationTime: [],
      creationTime1: [],
      select: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      addOrEdit: '',
      total: 0,
      dialogVisible: false,
      tableData: [],
      loading: false,
      form: {
        shoppingAddress: ['', '', '']
      },
      moren: 'supplierSet',
      topList: [
        {
          name: '供应商管理',
          path: 'supplierSet'
        }
      ],
      single: true,
      thepkId: '',
      isResouceShow: 0,
      areaData: [],
      regionParams: {
        label: 'name',
        value: 'id',
        children: 'children',
        expandTrigger: 'hover'
      },
      dialogTitle: '',
      custTypeList: [],
      rules: {
        custCode: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        custName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        shortName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        legalBody: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        linkMan: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        tradeScope: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        taxpayerid: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        custType: [
          { required: true, message: '请选择收货地址', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        shoppingAddress: [
          { required: true, message: '请选择收货地址', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getOthers()
    // 获取列表
    this.getDataList()
  },
  methods: {
    update(row) {
      this.dialogTitle = '修改'
      this.addOrEdit = 2
      this.form = row
      let arr = [
        row.province + '',
        row.city + '',
        (row.county || '') + ''
      ]
      arr = arr.filter((item) => item != '')
      this.$set(this.form, 'shoppingAddress', arr)
      this.$forceUpdate()
      this.dialogVisible = true
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.province = this.form.shoppingAddress[0]
          this.form.city = this.form.shoppingAddress[1]
          this.form.county = this.form.shoppingAddress[2]
          api.addSupplier(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: 'success'
              })

              this.dialogVisible = false
              // 获取列表
              this.getDataList()
              this.$refs[formName].resetFields()
            }
          })
        }
      })
    },
    handleChange1(val) {
      this.form.provinceName = val[0]
      this.form.cityName = val[1]
      this.form.countyName = val[2] || ''
    },
    getOthers() {
      site.areaList().then((res) => {
        this.areaData = res.data
      })
      this.getCustTypeList()
    },
    getCustTypeList() {
      api.getCustTypeList().then((res) => {
        this.custTypeList = res.data
      })
    },
    reset() {
      this.select = {}
      this.getDataList()
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.deleteSupplier(this.thepkId).then((res) => {
          this.getDataList()
        })
      })
    },
    openDig() {
      this.addOrEdit = 1
      this.dialogTitle = '添加'
      this.dialogVisible = true
      this.$refs['form'].resetFields()
    },
    getDataList() {
      this.loading = true
      api
        .getSupplierList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
        })
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    handleSelectionChange(val) {
      this.single = val.length !== 1
      this.thepkId = val[0].pkId
    },

    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .form_all {
      padding: 0px 20px 0 20px;
      // margin-bottom: 20px;
      background: #fff;
      border-radius: 8px;
    }
    .maintop {
      display: flex;
      padding: 0px 0px;
      justify-content: space-between;
      align-items: center;

      background: #f8f8f8;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}
.dizhi {
  ::v-deep .el-cascader {
    width: 100%;
  }
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}
</style>
