<!--供应商查询-->
<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-row class="page-search" justify="space-between" align="middle">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="" prop="params.account">
              <el-Input v-model:params="queryParams.params.SUPPLIERNO" placeholder="供应商编号ID" clearable
                @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6" style="display: none;">
            <el-form-item label="" prop="params.account">
              <CustomInput v-model:params="queryParams.params.SUPPLIERNO" placeholder="供应商编号ID" clearable
                @keyUpEvent="handleQuery" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="handleQuery">搜索</el-button>

      </div>
    </el-row>

    <div class="vue-element-page-table">
      <div class="vue-element-page-table">
        <div class="page-table-operate">
          <div class="page-table-title">
            <span>供应商信息列表：</span>
          </div>
          <div class="page-table-operateBtns">
            <el-popover :persistent="false" placement="right" popper-class="config-table-wrap" trigger="click">
              <template #reference>
                <el-button class="operateBtns-setting">
                  <svg-icon icon-class="table_title" width="16px" height="16px" />
                </el-button>
              </template>
              <ConfigTable :list="column1" @updateList="updateList" />
            </el-popover>
          </div>
        </div>
        <el-table border v-loading="loading" :data="state.userList" highlight-current-row row-key="id"
          style="width: 100%">
          <template v-for="(col, index) in column" :key="index">
            <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
              <template #default="scope">
                <span v-if="col.field === 'status'">
                  <span v-if="scope.row[col.field]" class="table-status-active">启用</span>
                  <span v-else class="table-status-inActive">禁用</span>
                </span>
                <span v-else>{{ scope.row[col.field] }}</span>
              </template>
            </el-table-column>
          </template>

          <el-table-column width="160" label="操作" align="center" class-name="table-operation" fixed="right">
            <!--<template #default="scope">
              <el-button type="primary" link @click.stop="handleUpdate(scope.row)" v-if="!scope.row.status">
                修改
              </el-button>
              <div class="splitLine" v-if="!scope.row.status" v-has-perm="['sys:user:enable','sys:user:disable']"></div>
              <el-button type="primary" link v-if="!scope.row.status" @click="handleDel(scope.row)">
                删除
              </el-button>
            </template>-->
          </el-table-column>
          <template v-slot:empty>
            <svg-icon icon-class="noData" width="160px" height="160px" />
          </template>
        </el-table>
      </div>
      <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
        v-model:pageSizeTo="queryParams.pageSize" @pagination="handleQuery" />
    </div>
    <!--  新增或编辑  -->
    <el-drawer :title="dialog.title" v-model="dialog.visible" @close="cancel" @opened="">
      <el-form ref="formDataRef" :model="formData" :rules="rules" label-position="top">

        <el-form-item label="供应商编号" prop="SUPPLIERNO">
          <el-input v-model="formData.SUPPLIERNO" placeholder="请输入供应商编号" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="SUPPLIERNAME">
          <el-input v-model="formData.SUPPLIERNAME" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="National NO" prop="NATIONALNO">
          <el-input v-model="formData.NATIONALNO" placeholder="请输入National NO" />
        </el-form-item>
        <el-form-item label="物料描述" prop="DESCRIPTION">
          <el-input v-model="formData.DESCRIPTION" placeholder="物料描述不可以为空" />
        </el-form-item>
        <el-form-item label="地址" prop="ADDRESS">
          <el-input v-model="formData.ADDRESS" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="POSTCODE">
          <el-input v-model="formData.POSTCODE" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="城市" prop="CITY">
          <el-input v-model="formData.CITY" placeholder="城市不为空" />
        </el-form-item>
        <el-form-item label="电话" prop="TEL">
          <el-input v-model="formData.TEL" placeholder="请注意格式" />
        </el-form-item>
        <el-form-item label="传真" prop="FAX">
          <el-input v-model="formData.FAX" placeholder="传真很重要" />
        </el-form-item>
        <el-form-item label="Incharge" prop="INCHARGE">
          <el-input v-model="formData.INCHARGE" placeholder="Incharge不为空" />
        </el-form-item>
        <el-form-item label="邮箱" prop="EMAIL">
          <el-input v-model="formData.EMAIL" placeholder="请注意邮箱" />
        </el-form-item>
        <el-form-item label="最后操作时间" prop="LASTEVENTTIME">
          <el-date-picker v-model="formData.LASTEVENTTIME" type="date" placeholder="Pick a day"
            :disabled-date="disabledDate" :shortcuts="shortcuts" :size="size" />

        </el-form-item>

      </el-form>

      <template #footer>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-drawer>
    <!--  分配角色  -->
    <el-drawer v-model="roleDialog.visible" @close="cancelRoleDialog" @opened="roleDialogOpend"
      class="drawer-show-table" modal-class="userRoleModel">
      <template #header>
        <div class="drawer-show-table-title">
          <svg-icon icon-class="close" width="20px" height="20px" @click="cancelRoleDialog" />
          <span>{{ roleDialog.title }}</span>
        </div>
        <el-button type="primary" @click="">确 定</el-button>
      </template>
      <el-table border ref="roleTableRef" :data="allRole" highlight-current-row row-key="dataId" style="width: 100%">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column prop="code" label="角色编码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

<script lang="ts">
export default {
  name: 'supplierQuery'
};
</script>

<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance, nextTick } from 'vue';
import { ElForm, ElMessageBox, ElTable } from 'element-plus';
import { getUserRoleList } from '@/api/user';
import { getRSAPublic } from '@/api/auth';
import { JSEncrypt } from 'jsencrypt';
import { getQueryList, getQueryPageList } from '@/api/common';
import { SupplierAdd, SupplierDel, SupplierUpd } from '@/api/basics/supplier'


const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);
const { proxy }: any = getCurrentInstance();
let statuButton = 0;
const roleTableRef = ref<InstanceType<typeof ElTable>>();
//formdata时间设置
const size = ref<'default' | 'large' | 'small'>('default')
const state = reactive({
  rsa: '',
  queryParams: {
    queryId: 'GetSupplierList',
    version: 'WEB10001',
    params: {
      SUPPLIERNO: ''
    },
    pageNum: 1,
    pageSize: 10
  },
  total: 0,
  loading: false,
  userList: [] as any,
  dialog: {
    title: '新增用户',
    visible: false
  },

  formData: {
    SUPPLIERNO: '',
    SUPPLIERNAME: '',
    NATIONALNO: '',
    DESCRIPTION: '',
    ADDRESS: '',
    POSTCODE: '',
    CITY: '',
    TEL: '',
    FAX: '',
    INCHARGE: '',
    EMAIL: '',
    LASTEVENTTIME: '',
  } as any,
  rules: {
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  },
  column: [] as any,
  selectItem: undefined as any,
  currentTime: '',
  roleDialog: {
    title: '分配角色',
    visible: false
  },
  allRole: [] as any

})
const {
  rsa,
  queryParams,
  total,
  loading,
  userList,
  dialog,
  formData,
  rules,
  column,
  selectItem,
  currentTime,
  roleDialog,
  allRole,
} = toRefs(state);

onMounted(() => {
  state.column = [
    { header: '供应商编号', field: 'SUPPLIERNO', hide: false },
    { header: '供应商名称', field: 'SUPPLIERNAME', hide: false },
    { header: 'National NO', field: 'NATIONALNO', hide: false },
    { header: '物料描述', field: 'DESCRIPTION', hide: false },
    { header: '地址', field: 'ADDRESS', hide: false },
    { header: '邮政编码', field: 'POSTCODE', hide: false },
    { header: '城市', field: 'CITY', hide: false },
    { header: '电话', field: 'TEL', hide: false },
    { header: '传真', field: 'FAX', hide: false },
    { header: 'Incharge', field: 'INCHARGE', hide: false },
    { header: '邮箱', field: 'EMAIL', hide: false },
    { header: '最后操作时间', field: 'LASTEVENTTIME', hide: false },
  ]
  //handleQuery();
  //getRSA();
});


const shortcuts = [
  {
    text: 'Today',
    value: new Date(),
  },
  {
    text: 'Yesterday',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24)
      return date
    },
  },
  {
    text: 'A week ago',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      return date
    },
  },
]

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}
// 更新列
function updateList(col: any) {
  state.column = col;
}
function getRSA() {
  getRSAPublic().then((res: any) => {
    state.rsa = res.data;
  }).catch(() => { });
}

function handleQuery() {
  state.userList = [];
  getQueryPageList(state.queryParams)
    .then((res: any) => {
      state.userList = res.data.list;
      state.total = res.data.total;
      showCurrentTime();
    })
    .catch(() => { });
}
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
function showCurrentTime() {
  let date = new Date()
  state.currentTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ":" +
    (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ":" + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
}
/**
 *  启用
 */

/**
 *  禁用
 */
function disable(row: any) {
  state.selectItem = row;
  ElMessageBox.confirm(
    '确认禁用?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      disableUser({
        data: {
          dataId: row.dataId
        }
      }).then(() => {
        proxy.$ElMessage.success('禁用成功');
        cancel();
        handleQuery();
      });
    })
    .catch(() => {

    })
}

/**
 * 新增供应商用户 dialog
 */
function handleAdd() {
  selectItem.value = undefined;
  dialog.value = {
    title: '新增供应商',
    visible: true
  };
  state.rules.pwd[0].required = true;
  statuButton = 2;
}

/**
 * 删除供应商用户 dialog
 */
function handleDel(row: any) {
  SupplierDel(row)
    .then((res: any) => {
      showCurrentTime();
      handleQuery();
    }).then(() => proxy.$ElMessage.success('删除成功'))
    .catch(() => { });
}

/**
 * 修改供应商用户 dialog
 */
function handleUpd() {

}
/**
 * 编辑用户 dialog
 */
function handleUpdate(row: any) {
  state.dialog = {
    title: '编辑供应商',
    visible: true
  };
  state.selectItem = row;
  state.rules.pwd[0].required = false;
  statuButton = 1;
}



/**
 * 取消关闭弹窗
 */
function cancel() {
  state.dialog.visible = false;
}

/**
 * 保存
 */
function submitForm() {
  console.log(statuButton);
  switch (statuButton) {
    case 0: break;
    case 1: SupplierUpd(state.formData)
      .then((res: any) => {
        console.log("upd" + res.code + ":" + statuButton);
        dialog.value.visible = false;
        handleQuery();
      }).then(() => proxy.$ElMessage.success('修改成功'))
      .catch(() => { }); break;
    case 2: SupplierAdd(state.formData)
      .then((res: any) => {
        console.log("Add:" + res.code + ":" + statuButton);
        dialog.value.visible = false;
      }).then(() => proxy.$ElMessage.success('创建成功'))
      .catch(() => { }); break;
  }
  statuButton = 0;
}

/****************************** 分配角色 ***************************/
function handleRole(row: any) {
  state.roleDialog.visible = true;
  state.allRole = [];
  state.selectItem = row;
}
function cancelRoleDialog() {
  state.roleDialog.visible = false;
}
function roleDialogOpend() {
  getUserRoleList({
    dataId: state.selectItem.dataId
  })
    .then((res: any) => {
      state.allRole = res.data.list;
      let rows = state.allRole.filter((item: any) => res.data.checked.includes(item.dataId));
      rows.forEach((item: any) => {
        console.log(item);
        nextTick(() => {
          roleTableRef.value!.toggleRowSelection(item, true);
        })
      })
    })
    .catch(() => { });
}


</script>

<style scoped lang="scss">
.el-overlay.userRoleModel {}
</style>